freedombone-app-syncthing 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Syncthing application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim cloud'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=0
  33. SYNCTHING_ID=
  34. SYNCTHING_CONFIG_PATH=/root/.config/syncthing
  35. SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
  36. SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
  37. SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
  38. SYNCTHING_PORT=22000
  39. SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
  40. SYNCTHING_USER_IDS_FILE='.syncthingids'
  41. syncthing_variables=(SYNCTHING_ID
  42. SYNCTHING_CONFIG_PATH
  43. SYNCTHING_CONFIG_FILE
  44. SYNCTHING_RELAY_SERVER
  45. SYNCTHING_RELEASES
  46. SYNCTHING_PORT
  47. SYNCTHING_SHARED_DATA
  48. USB_MOUNT)
  49. function logging_on_syncthing {
  50. echo -n ''
  51. }
  52. function logging_off_syncthing {
  53. echo -n ''
  54. }
  55. function syncthing_create_ids_file {
  56. if [ ! -f ~/.syncthing-server-id ]; then
  57. return
  58. fi
  59. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  60. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  61. echo $'# Your syncthing configuration file' > $SYNCTHING_CONFIG_FILE
  62. echo '#' >> $SYNCTHING_CONFIG_FILE
  63. echo $"# The ${PROJECT_NAME} syncthing ID is: $SYNCTHING_ID" >> $SYNCTHING_CONFIG_FILE
  64. echo '#' >> $SYNCTHING_CONFIG_FILE
  65. echo '# Paste the IDs of your devices below' >> $SYNCTHING_CONFIG_FILE
  66. echo '#' >> $SYNCTHING_CONFIG_FILE
  67. fi
  68. }
  69. function syncthing_manual_edit {
  70. if [ ! -f ~/.syncthing-server-id ]; then
  71. return
  72. fi
  73. syncthing_create_ids_file
  74. editor $SYNCTHING_CONFIG_FILE
  75. # force an update of the configuration
  76. touch ~/.syncthing-update
  77. }
  78. function syncthing_show_id {
  79. if [ ! -f ~/.syncthing-server-id ]; then
  80. return
  81. fi
  82. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  83. dialog --title $"Device ID for ${PROJECT_NAME}" \
  84. --backtitle $"Freedombone User Control Panel" \
  85. --msgbox $"In a desktop terminal press shift and select the ID below,\nthen right click and copy.\n\nWithin Connectbot select Menu/Copy and then highlight the ID below\n\n$SYNCTHING_ID" 12 78
  86. }
  87. function syncthing_add_id {
  88. if [ ! -f ~/.syncthing-server-id ]; then
  89. return
  90. fi
  91. syncthing_create_ids_file
  92. data=$(tempfile 2>/dev/null)
  93. trap "rm -f $data" 0 1 2 5 15
  94. dialog --backtitle $"Freedombone User Control Panel" \
  95. --title $"Add a Syncthing device ID" \
  96. --form $"Paste the device ID for your laptop/desktop/netbook/phone/tablet below" 9 80 2 \
  97. $"Device ID:" 1 1 "" 1 26 80 80 \
  98. $"Description (optional):" 2 1 "" 2 26 80 80 \
  99. 2> $data
  100. sel=$?
  101. case $sel in
  102. 1) return;;
  103. 255) return;;
  104. esac
  105. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  106. SYNCTHING_DESCRIPTION=$(cat $data | sed -n 2p)
  107. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  108. return
  109. fi
  110. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  111. dialog --title $"Add a Syncthing device ID" \
  112. --backtitle $"Freedombone User Control Panel" \
  113. --msgbox $"That doesn't look like a device ID" 6 50
  114. return
  115. fi
  116. if grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  117. dialog --title $"Add a Syncthing device ID" \
  118. --backtitle $"Freedombone User Control Panel" \
  119. --msgbox $"That ID has already been added" 6 50
  120. return
  121. fi
  122. if [ ${#SYNCTHING_DESCRIPTION} -gt 0 ]; then
  123. echo "# $SYNCTHING_DESCRIPTION" >> $SYNCTHING_CONFIG_FILE
  124. fi
  125. echo "$SYNCTHING_DEVICE_ID" >> $SYNCTHING_CONFIG_FILE
  126. # force an update of the configuration
  127. touch ~/.syncthing-update
  128. dialog --title $"Add a Syncthing device ID" \
  129. --backtitle $"Freedombone User Control Panel" \
  130. --msgbox $"The ID was added" 6 50
  131. }
  132. function syncthing_remove_id {
  133. if [ ! -f ~/.syncthing-server-id ]; then
  134. return
  135. fi
  136. syncthing_create_ids_file
  137. data=$(tempfile 2>/dev/null)
  138. trap "rm -f $data" 0 1 2 5 15
  139. dialog --backtitle $"Freedombone User Control Panel" \
  140. --title $"Remove a Syncthing device ID" \
  141. --form $"Paste the device ID which is to be removed below" 8 80 1 \
  142. $"Device ID:" 1 1 "" 1 14 80 80 \
  143. 2> $data
  144. sel=$?
  145. case $sel in
  146. 1) return;;
  147. 255) return;;
  148. esac
  149. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  150. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  151. return
  152. fi
  153. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  154. dialog --title $"Remove a Syncthing device ID" \
  155. --backtitle $"Freedombone User Control Panel" \
  156. --msgbox $"That doesn't look like a device ID" 6 50
  157. return
  158. fi
  159. if ! grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  160. dialog --title $"Remove a Syncthing device ID" \
  161. --backtitle $"Freedombone User Control Panel" \
  162. --msgbox $"That ID wasn't registered anyway" 6 50
  163. return
  164. fi
  165. sed -i "/$SYNCTHING_DEVICE_ID/d" $SYNCTHING_CONFIG_FILE
  166. # force an update of the configuration
  167. touch ~/.syncthing-update
  168. dialog --title $"Remove a Syncthing device ID" \
  169. --backtitle $"Freedombone User Control Panel" \
  170. --msgbox $"The ID was removed" 6 50
  171. }
  172. function run_client_syncthing {
  173. SYNCTHING_CONFIG_FILE=~/.syncthingids
  174. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  175. while true
  176. do
  177. data=$(tempfile 2>/dev/null)
  178. trap "rm -f $data" 0 1 2 5 15
  179. dialog --backtitle $"Freedombone User Control Panel" \
  180. --title $"File Synchronization" \
  181. --radiolist $"Choose an operation:" 12 70 6 \
  182. 1 $"Show device ID for ${PROJECT_NAME}" off \
  183. 2 $"Add an ID for another machine or device" off \
  184. 3 $"Remove an ID for another machine or device" off \
  185. 4 $"Manually edit device IDs" off \
  186. 5 $"Back to main menu" on 2> $data
  187. sel=$?
  188. case $sel in
  189. 1) break;;
  190. 255) break;;
  191. esac
  192. case $(cat $data) in
  193. 1) syncthing_show_id;;
  194. 2) syncthing_add_id;;
  195. 3) syncthing_remove_id;;
  196. 4) syncthing_manual_edit;;
  197. 5) break;;
  198. esac
  199. done
  200. }
  201. function install_interactive_syncthing {
  202. echo -n ''
  203. APP_INSTALLED=1
  204. }
  205. function reconfigure_syncthing {
  206. echo -n ''
  207. }
  208. function upgrade_syncthing {
  209. echo -n ''
  210. }
  211. function backup_local_syncthing {
  212. if [ -d /var/lib/syncthing/SyncShared ]; then
  213. function_check backup_directory_to_usb
  214. backup_directory_to_usb /var/lib/syncthing/SyncShared syncthingshared
  215. backup_directory_to_usb /root/.config/syncthing syncthingconfig
  216. fi
  217. for d in /home/*/ ; do
  218. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  219. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  220. if [ -d /home/$USERNAME/Sync ]; then
  221. echo $"Backing up syncthing files for $USERNAME"
  222. backup_directory_to_usb /home/$USERNAME/Sync syncthing/$USERNAME
  223. # ensure that device IDs will be backed up as part of user config settings
  224. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  225. mkdir -p /home/$USERNAME/.config/syncthing
  226. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  227. fi
  228. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  229. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  230. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  231. fi
  232. if [ -f /home/$USERNAME/.syncthingids ]; then
  233. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  234. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  235. fi
  236. fi
  237. fi
  238. done
  239. }
  240. function restore_local_syncthing {
  241. if [ -f /etc/systemd/system/syncthing.service ]; then
  242. systemctl stop syncthing
  243. systemctl stop cron
  244. fi
  245. temp_restore_dir=/root/tempsyncthing
  246. if [ -d $USB_MOUNT/backup/syncthingconfig ]; then
  247. echo $"Restoring syncthing configuration"
  248. function_check restore_directory_from_usb
  249. restore_directory_from_usb ${temp_restore_dir}config syncthingconfig
  250. cp -r ${temp_restore_dir}config/* /
  251. if [ ! "$?" = "0" ]; then
  252. set_user_permissions
  253. backup_unmount_drive
  254. systemctl start syncthing
  255. systemctl start cron
  256. exit 6833
  257. fi
  258. rm -rf ${temp_restore_dir}config
  259. fi
  260. if [ -d $USB_MOUNT/backup/syncthingshared ]; then
  261. echo $"Restoring syncthing shared files"
  262. restore_directory_from_usb ${temp_restore_dir}shared syncthingshared
  263. cp -r ${temp_restore_dir}shared/* /
  264. if [ ! "$?" = "0" ]; then
  265. set_user_permissions
  266. backup_unmount_drive
  267. systemctl start syncthing
  268. systemctl start cron
  269. exit 37904
  270. fi
  271. rm -rf ${temp_restore_dir}shared
  272. fi
  273. if [ -d $USB_MOUNT/backup/syncthing ]; then
  274. for d in $USB_MOUNT/backup/syncthing/*/ ; do
  275. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  276. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  277. if [ ! -d /home/$USERNAME ]; then
  278. ${PROJECT_NAME}-adduser $USERNAME
  279. fi
  280. echo $"Restoring syncthing files for $USERNAME"
  281. restore_directory_from_usb ${temp_restore_dir} syncthing/$USERNAME
  282. cp -r ${temp_restore_dir}/home/$USERNAME/Sync /home/$USERNAME/
  283. if [ ! "$?" = "0" ]; then
  284. rm -rf ${temp_restore_dir}
  285. set_user_permissions
  286. backup_unmount_drive
  287. systemctl start syncthing
  288. systemctl start cron
  289. exit 68438
  290. fi
  291. rm -rf ${temp_restore_dir}
  292. # restore device IDs from config settings
  293. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  294. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  295. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  296. fi
  297. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  298. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  299. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  300. fi
  301. fi
  302. done
  303. fi
  304. if [ -f /etc/systemd/system/syncthing.service ]; then
  305. systemctl start syncthing
  306. systemctl start cron
  307. fi
  308. }
  309. function backup_remote_syncthing {
  310. if [ -d /root/.config/syncthing ]; then
  311. echo $"Backing up syncthing configuration"
  312. function_check backup_directory_to_friend
  313. backup_directory_to_friend /root/.config/syncthing syncthingconfig
  314. echo $"Backup of syncthing configuration complete"
  315. fi
  316. if [ -d /var/lib/syncthing/SyncShared ]; then
  317. echo $"Backing up syncthing shared files"
  318. function_check backup_directory_to_friend
  319. backup_directory_to_friend /var/lib/syncthing/SyncShared syncthingshared
  320. echo $"Backup of syncthing shared files complete"
  321. fi
  322. for d in /home/*/ ; do
  323. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  324. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  325. if [ -d /home/$USERNAME/Sync ]; then
  326. echo $"Backing up syncthing files for $USERNAME"
  327. backup_directory_to_friend /home/$USERNAME/Sync syncthing/$USERNAME
  328. # ensure that device IDs will be backed up as part of user config settings
  329. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  330. mkdir -p /home/$USERNAME/.config/syncthing
  331. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  332. fi
  333. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  334. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  335. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  336. fi
  337. if [ -f /home/$USERNAME/.syncthingids ]; then
  338. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  339. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  340. fi
  341. fi
  342. fi
  343. done
  344. }
  345. function restore_remote_syncthing {
  346. if [ -f /etc/systemd/system/syncthing.service ]; then
  347. systemctl stop syncthing
  348. systemctl stop cron
  349. fi
  350. if [ -d $SERVER_DIRECTORY/backup/syncthingconfig ]; then
  351. echo $"Restoring syncthing configuration"
  352. temp_restore_dir=/root/tempsyncthingconfig
  353. function_check restore_directory_from_friend
  354. restore_directory_from_friend $temp_restore_dir syncthingconfig
  355. cp -r $temp_restore_dir/* /
  356. if [ ! "$?" = "0" ]; then
  357. systemctl start syncthing
  358. systemctl start cron
  359. exit 6833
  360. fi
  361. rm -rf $temp_restore_dir
  362. fi
  363. if [ -d $SERVER_DIRECTORY/backup/syncthingshared ]; then
  364. echo $"Restoring syncthing shared files"
  365. temp_restore_dir=/root/tempsyncthingshared
  366. function_check restore_directory_from_friend
  367. restore_directory_from_friend $temp_restore_dir syncthingshared
  368. cp -r $temp_restore_dir/* /
  369. if [ ! "$?" = "0" ]; then
  370. systemctl start syncthing
  371. systemctl start cron
  372. exit 37904
  373. fi
  374. rm -rf $temp_restore_dir
  375. fi
  376. if [ -d $SERVER_DIRECTORY/backup/syncthing ]; then
  377. for d in $SERVER_DIRECTORY/backup/syncthing/*/ ; do
  378. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  379. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  380. if [ ! -d /home/$USERNAME ]; then
  381. ${PROJECT_NAME}-adduser $USERNAME
  382. fi
  383. echo $"Restoring syncthing files for $USERNAME"
  384. temp_restore_dir=/root/tempsyncthing
  385. function_check restore_directory_from_friend
  386. restore_directory_from_friend $temp_restore_dir syncthing/$USERNAME
  387. cp -r $temp_restore_dir/home/$USERNAME/Sync /home/$USERNAME/
  388. if [ ! "$?" = "0" ]; then
  389. rm -rf $temp_restore_dir
  390. systemctl start syncthing
  391. systemctl start cron
  392. exit 68438
  393. fi
  394. rm -rf $temp_restore_dir
  395. # restore device IDs from config settings
  396. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  397. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  398. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  399. fi
  400. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  401. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  402. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  403. fi
  404. echo $"Restore of syncthing files for $USERNAME complete"
  405. fi
  406. done
  407. fi
  408. if [ -f /etc/systemd/system/syncthing.service ]; then
  409. systemctl start syncthing
  410. systemctl start cron
  411. fi
  412. }
  413. function remove_syncthing {
  414. firewall_remove ${SYNCTHING_PORT}
  415. systemctl stop syncthing
  416. systemctl disable syncthing
  417. rm /etc/systemd/system/syncthing.service
  418. systemctl daemon-reload
  419. apt-get -yq remove --purge syncthing
  420. sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
  421. remove_completion_param install_syncthing
  422. remove_completion_param configure_firewall_for_syncthing
  423. systemctl restart cron
  424. }
  425. function configure_firewall_for_syncthing {
  426. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  427. return
  428. fi
  429. firewall_add Syncthing ${SYNCTHING_PORT}
  430. mark_completed $FUNCNAME
  431. }
  432. function install_syncthing_repo {
  433. if [ -f /etc/apt/sources.list.d/syncthing.list ]; then
  434. return
  435. fi
  436. apt-get -yq install curl
  437. curl -s https://syncthing.net/release-key.txt | apt-key add -
  438. echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list
  439. apt-get update
  440. }
  441. function install_syncthing {
  442. install_syncthing_repo
  443. apt-get -yq install syncthing
  444. # This probably does need to run as root so that it can access the Sync directories
  445. # in each user's home directory
  446. echo '[Unit]' > /etc/systemd/system/syncthing.service
  447. echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> /etc/systemd/system/syncthing.service
  448. echo 'Documentation=man:syncthing(1)' >> /etc/systemd/system/syncthing.service
  449. echo 'After=network.target' >> /etc/systemd/system/syncthing.service
  450. echo 'Wants=syncthing-inotify@.service' >> /etc/systemd/system/syncthing.service
  451. echo '' >> /etc/systemd/system/syncthing.service
  452. echo '[Service]' >> /etc/systemd/system/syncthing.service
  453. echo 'User=root' >> /etc/systemd/system/syncthing.service
  454. echo "Environment='all_proxy=socks5://localhost:9050'" >> /etc/systemd/system/syncthing.service
  455. echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> /etc/systemd/system/syncthing.service
  456. echo 'Restart=on-failure' >> /etc/systemd/system/syncthing.service
  457. echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/syncthing.service
  458. echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/syncthing.service
  459. echo '' >> /etc/systemd/system/syncthing.service
  460. echo '[Install]' >> /etc/systemd/system/syncthing.service
  461. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/syncthing.service
  462. systemctl enable syncthing
  463. systemctl daemon-reload
  464. systemctl start syncthing
  465. function_check cron_add_mins
  466. cron_add_mins 1 "/usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null"
  467. function_check configure_firewall_for_syncthing
  468. configure_firewall_for_syncthing
  469. APP_INSTALLED=1
  470. }
  471. # NOTE: deliberately no exit 0