freedombone-app-syncthing 12KB

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