freedombone-app-syncthing 19KB

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