freedombone-app-syncthing 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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@robotics.uk.to>
  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 cloud'
  31. SYNCTHING_ID=
  32. SYNCTHING_CONFIG_PATH=/root/.config/syncthing
  33. SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
  34. SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
  35. SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
  36. SYNCTHING_PORT=22000
  37. SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
  38. SYNCTHING_USER_IDS_FILE='.syncthingids'
  39. function change_password_syncthing {
  40. echo -n ''
  41. }
  42. function reconfigure_syncthing {
  43. echo -n ''
  44. }
  45. function upgrade_syncthing {
  46. echo -n ''
  47. }
  48. function backup_local_syncthing {
  49. if [ -d /var/lib/syncthing/SyncShared ]; then
  50. echo $"Backing up syncthing"
  51. function_check backup_directory_to_usb
  52. backup_directory_to_usb /var/lib/syncthing/SyncShared syncthingshared
  53. backup_directory_to_usb /root/.config/syncthing syncthingconfig
  54. echo $"Backup to syncthing complete"
  55. fi
  56. }
  57. function restore_local_syncthing {
  58. if [ -f /etc/systemd/system/syncthing.service ]; then
  59. systemctl stop syncthing
  60. systemctl stop cron
  61. fi
  62. temp_restore_dir=/root/tempsyncthing
  63. if [ -d $USB_MOUNT/backup/syncthingconfig ]; then
  64. echo $"Restoring syncthing configuration"
  65. function_check restore_directory_from_usb
  66. restore_directory_from_usb ${temp_restore_dir}config syncthingconfig
  67. cp -r ${temp_restore_dir}config/* /
  68. if [ ! "$?" = "0" ]; then
  69. set_user_permissions
  70. backup_unmount_drive
  71. systemctl start syncthing
  72. systemctl start cron
  73. exit 6833
  74. fi
  75. rm -rf ${temp_restore_dir}config
  76. fi
  77. if [ -d $USB_MOUNT/backup/syncthingshared ]; then
  78. echo $"Restoring syncthing shared files"
  79. restore_directory_from_usb ${temp_restore_dir}shared syncthingshared
  80. cp -r ${temp_restore_dir}shared/* /
  81. if [ ! "$?" = "0" ]; then
  82. set_user_permissions
  83. backup_unmount_drive
  84. systemctl start syncthing
  85. systemctl start cron
  86. exit 37904
  87. fi
  88. rm -rf ${temp_restore_dir}shared
  89. fi
  90. if [ -d $USB_MOUNT/backup/syncthing ]; then
  91. for d in $USB_MOUNT/backup/syncthing/*/ ; do
  92. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  93. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" && $USERNAME != "tahoelafs" ]]; then
  94. if [ ! -d /home/$USERNAME ]; then
  95. ${PROJECT_NAME}-adduser $USERNAME
  96. fi
  97. echo $"Restoring syncthing files for $USERNAME"
  98. restore_directory_from_usb ${temp_restore_dir} syncthing/$USERNAME
  99. cp -r ${temp_restore_dir}/home/$USERNAME/Sync /home/$USERNAME/
  100. if [ ! "$?" = "0" ]; then
  101. rm -rf ${temp_restore_dir}
  102. set_user_permissions
  103. backup_unmount_drive
  104. systemctl start syncthing
  105. systemctl start cron
  106. exit 68438
  107. fi
  108. rm -rf ${temp_restore_dir}
  109. # restore device IDs from config settings
  110. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  111. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  112. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  113. fi
  114. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  115. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  116. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  117. fi
  118. fi
  119. done
  120. fi
  121. if [ -f /etc/systemd/system/syncthing.service ]; then
  122. systemctl start syncthing
  123. systemctl start cron
  124. fi
  125. }
  126. function backup_remote_syncthing {
  127. if [ -d /root/.config/syncthing ]; then
  128. echo $"Backing up syncthing configuration"
  129. function_check backup_directory_to_friend
  130. backup_directory_to_friend /root/.config/syncthing syncthingconfig
  131. echo $"Backup of syncthing configuration complete"
  132. fi
  133. if [ -d /var/lib/syncthing/SyncShared ]; then
  134. echo $"Backing up syncthing shared files"
  135. function_check backup_directory_to_friend
  136. backup_directory_to_friend /var/lib/syncthing/SyncShared syncthingshared
  137. echo $"Backup of syncthing shared files complete"
  138. fi
  139. }
  140. function restore_remote_syncthing {
  141. if [ -f /etc/systemd/system/syncthing.service ]; then
  142. systemctl stop syncthing
  143. systemctl stop cron
  144. fi
  145. if [ -d $SERVER_DIRECTORY/backup/syncthingconfig ]; then
  146. echo $"Restoring syncthing configuration"
  147. temp_restore_dir=/root/tempsyncthingconfig
  148. function_check restore_directory_from_friend
  149. restore_directory_from_friend $temp_restore_dir syncthingconfig
  150. cp -r $temp_restore_dir/* /
  151. if [ ! "$?" = "0" ]; then
  152. systemctl start syncthing
  153. systemctl start cron
  154. exit 6833
  155. fi
  156. rm -rf $temp_restore_dir
  157. fi
  158. if [ -d $SERVER_DIRECTORY/backup/syncthingshared ]; then
  159. echo $"Restoring syncthing shared files"
  160. temp_restore_dir=/root/tempsyncthingshared
  161. function_check restore_directory_from_friend
  162. restore_directory_from_friend $temp_restore_dir syncthingshared
  163. cp -r $temp_restore_dir/* /
  164. if [ ! "$?" = "0" ]; then
  165. systemctl start syncthing
  166. systemctl start cron
  167. exit 37904
  168. fi
  169. rm -rf $temp_restore_dir
  170. fi
  171. if [ -d $SERVER_DIRECTORY/backup/syncthing ]; then
  172. for d in $SERVER_DIRECTORY/backup/syncthing/*/ ; do
  173. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  174. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" && $USERNAME != "tahoelafs" ]]; then
  175. if [ ! -d /home/$USERNAME ]; then
  176. ${PROJECT_NAME}-adduser $USERNAME
  177. fi
  178. echo $"Restoring syncthing files for $USERNAME"
  179. temp_restore_dir=/root/tempsyncthing
  180. function_check restore_directory_from_friend
  181. restore_directory_from_friend $temp_restore_dir syncthing/$USERNAME
  182. cp -r $temp_restore_dir/home/$USERNAME/Sync /home/$USERNAME/
  183. if [ ! "$?" = "0" ]; then
  184. rm -rf $temp_restore_dir
  185. systemctl start syncthing
  186. systemctl start cron
  187. exit 68438
  188. fi
  189. rm -rf $temp_restore_dir
  190. # restore device IDs from config settings
  191. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  192. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  193. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  194. fi
  195. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  196. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  197. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  198. fi
  199. echo $"Restore of syncthing files for $USERNAME complete"
  200. fi
  201. done
  202. fi
  203. if [ -f /etc/systemd/system/syncthing.service ]; then
  204. systemctl start syncthing
  205. systemctl start cron
  206. fi
  207. }
  208. function remove_syncthing {
  209. if ! grep -Fxq "install_syncthing" $COMPLETION_FILE; then
  210. return
  211. fi
  212. iptables -D INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
  213. iptables -D INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
  214. function_check save_firewall_settings
  215. save_firewall_settings
  216. systemctl stop syncthing
  217. systemctl disable syncthing
  218. apt-get -y remove --purge syncthing
  219. rm /etc/systemd/system/syncthing.service
  220. sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
  221. sed -i '/install_syncthing/d' $COMPLETION_FILE
  222. sed -i '/configure_firewall_for_syncthing/d' $COMPLETION_FILE
  223. systemctl restart cron
  224. }
  225. function configure_firewall_for_syncthing {
  226. if grep -Fxq "configure_firewall_for_syncthing" $COMPLETION_FILE; then
  227. return
  228. fi
  229. iptables -A INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
  230. iptables -A INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
  231. function_check save_firewall_settings
  232. save_firewall_settings
  233. OPEN_PORTS+=("Syncthing $SYNCTHING_PORT")
  234. echo 'configure_firewall_for_syncthing' >> $COMPLETION_FILE
  235. }
  236. function mesh_install_syncthing {
  237. chroot "$rootdir" wget -q https://syncthing.net/release-key.txt -O- | apt-key add -
  238. echo "deb http://apt.syncthing.net/ syncthing release" | tee $rootdir/etc/apt/sources.list.d/syncthing.list
  239. chroot "$rootdir" apt-get update
  240. chroot "$rootdir" apt-get -y --force-yes install syncthing
  241. # This probably does need to run as root so that it can access the Sync directories
  242. # in each user's home directory
  243. echo '[Unit]' > $rootdir/etc/systemd/system/syncthing.service
  244. echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> $rootdir/etc/systemd/system/syncthing.service
  245. echo 'Documentation=man:syncthing(1)' >> $rootdir/etc/systemd/system/syncthing.service
  246. echo 'After=network.target' >> $rootdir/etc/systemd/system/syncthing.service
  247. echo 'Wants=syncthing-inotify@.service' >> $rootdir/etc/systemd/system/syncthing.service
  248. echo '' >> $rootdir/etc/systemd/system/syncthing.service
  249. echo '[Service]' >> $rootdir/etc/systemd/system/syncthing.service
  250. echo 'User=root' >> $rootdir/etc/systemd/system/syncthing.service
  251. echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> $rootdir/etc/systemd/system/syncthing.service
  252. echo 'Restart=on-failure' >> $rootdir/etc/systemd/system/syncthing.service
  253. echo 'SuccessExitStatus=3 4' >> $rootdir/etc/systemd/system/syncthing.service
  254. echo 'RestartForceExitStatus=3 4' >> $rootdir/etc/systemd/system/syncthing.service
  255. echo '' >> $rootdir/etc/systemd/system/syncthing.service
  256. echo '[Install]' >> $rootdir/etc/systemd/system/syncthing.service
  257. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/syncthing.service
  258. chroot "$rootdir" systemctl enable syncthing
  259. echo 'mesh_install_syncthing'
  260. }
  261. function install_syncthing {
  262. if [ $INSTALLING_MESH ]; then
  263. mesh_install_syncthing
  264. return
  265. fi
  266. if grep -Fxq "install_syncthing" $COMPLETION_FILE; then
  267. return
  268. fi
  269. apt-get -y install curl
  270. curl -s https://syncthing.net/release-key.txt | apt-key add -
  271. echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list
  272. apt-get update
  273. apt-get -y install syncthing
  274. # This probably does need to run as root so that it can access the Sync directories
  275. # in each user's home directory
  276. echo '[Unit]' > /etc/systemd/system/syncthing.service
  277. echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> /etc/systemd/system/syncthing.service
  278. echo 'Documentation=man:syncthing(1)' >> /etc/systemd/system/syncthing.service
  279. echo 'After=network.target' >> /etc/systemd/system/syncthing.service
  280. echo 'Wants=syncthing-inotify@.service' >> /etc/systemd/system/syncthing.service
  281. echo '' >> /etc/systemd/system/syncthing.service
  282. echo '[Service]' >> /etc/systemd/system/syncthing.service
  283. echo 'User=root' >> /etc/systemd/system/syncthing.service
  284. echo "Environment='all_proxy=socks5://localhost:9050'" >> /etc/systemd/system/syncthing.service
  285. echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> /etc/systemd/system/syncthing.service
  286. echo 'Restart=on-failure' >> /etc/systemd/system/syncthing.service
  287. echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/syncthing.service
  288. echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/syncthing.service
  289. echo '' >> /etc/systemd/system/syncthing.service
  290. echo '[Install]' >> /etc/systemd/system/syncthing.service
  291. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/syncthing.service
  292. systemctl enable syncthing
  293. systemctl daemon-reload
  294. systemctl start syncthing
  295. function_check cron_add_mins
  296. cron_add_mins 1 "/usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null"
  297. function_check configure_firewall_for_syncthing
  298. configure_firewall_for_syncthing
  299. echo 'install_syncthing' >> $COMPLETION_FILE
  300. }
  301. # NOTE: deliberately no exit 0