2016-07-03 17:13:34 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
# Syncthing application
|
|
|
|
#
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-07-06 17:47:55 +02:00
|
|
|
VARIANTS='full cloud'
|
|
|
|
|
2016-07-03 17:13:34 +02:00
|
|
|
SYNCTHING_ID=
|
|
|
|
SYNCTHING_CONFIG_PATH=/root/.config/syncthing
|
|
|
|
SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
|
|
|
|
SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
|
|
|
|
SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
|
|
|
|
SYNCTHING_PORT=22000
|
|
|
|
SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
|
|
|
|
SYNCTHING_USER_IDS_FILE='.syncthingids'
|
|
|
|
|
2016-07-09 12:36:12 +02:00
|
|
|
function reconfigure_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
echo -n ''
|
2016-07-09 12:36:12 +02:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:01:28 +02:00
|
|
|
function upgrade_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
echo -n ''
|
2016-07-06 16:01:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:55:09 +02:00
|
|
|
function backup_local_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ -d /var/lib/syncthing/SyncShared ]; then
|
|
|
|
echo $"Backing up syncthing"
|
2016-07-07 21:42:25 +02:00
|
|
|
|
2016-07-24 14:19:56 +02:00
|
|
|
function_check backup_directory_to_usb
|
|
|
|
backup_directory_to_usb /var/lib/syncthing/SyncShared syncthingshared
|
|
|
|
backup_directory_to_usb /root/.config/syncthing syncthingconfig
|
2016-07-07 21:42:25 +02:00
|
|
|
|
2016-07-24 14:19:56 +02:00
|
|
|
echo $"Backup to syncthing complete"
|
|
|
|
fi
|
2016-07-06 15:55:09 +02:00
|
|
|
}
|
|
|
|
|
2016-07-09 11:28:53 +02:00
|
|
|
function restore_local_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ -f /etc/systemd/system/syncthing.service ]; then
|
|
|
|
systemctl stop syncthing
|
|
|
|
systemctl stop cron
|
|
|
|
fi
|
|
|
|
|
|
|
|
temp_restore_dir=/root/tempsyncthing
|
|
|
|
if [ -d $USB_MOUNT/backup/syncthingconfig ]; then
|
|
|
|
echo $"Restoring syncthing configuration"
|
|
|
|
function_check restore_directory_from_usb
|
|
|
|
restore_directory_from_usb ${temp_restore_dir}config syncthingconfig
|
|
|
|
cp -r ${temp_restore_dir}config/* /
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
set_user_permissions
|
|
|
|
backup_unmount_drive
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
exit 6833
|
2016-07-09 11:28:53 +02:00
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf ${temp_restore_dir}config
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $USB_MOUNT/backup/syncthingshared ]; then
|
|
|
|
echo $"Restoring syncthing shared files"
|
|
|
|
restore_directory_from_usb ${temp_restore_dir}shared syncthingshared
|
|
|
|
cp -r ${temp_restore_dir}shared/* /
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
set_user_permissions
|
|
|
|
backup_unmount_drive
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
exit 37904
|
2016-07-09 11:28:53 +02:00
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf ${temp_restore_dir}shared
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $USB_MOUNT/backup/syncthing ]; then
|
|
|
|
for d in $USB_MOUNT/backup/syncthing/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
|
|
|
|
if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
|
|
|
|
if [ ! -d /home/$USERNAME ]; then
|
|
|
|
${PROJECT_NAME}-adduser $USERNAME
|
|
|
|
fi
|
|
|
|
echo $"Restoring syncthing files for $USERNAME"
|
|
|
|
restore_directory_from_usb ${temp_restore_dir} syncthing/$USERNAME
|
|
|
|
cp -r ${temp_restore_dir}/home/$USERNAME/Sync /home/$USERNAME/
|
2016-07-09 11:28:53 +02:00
|
|
|
if [ ! "$?" = "0" ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf ${temp_restore_dir}
|
|
|
|
set_user_permissions
|
|
|
|
backup_unmount_drive
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
exit 68438
|
2016-07-09 11:28:53 +02:00
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf ${temp_restore_dir}
|
2016-07-09 11:28:53 +02:00
|
|
|
|
2016-07-24 14:19:56 +02:00
|
|
|
# restore device IDs from config settings
|
|
|
|
if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
|
|
|
|
cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
|
|
|
|
chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
|
|
|
|
fi
|
|
|
|
if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
|
|
|
|
cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
|
|
|
|
chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2016-07-09 11:28:53 +02:00
|
|
|
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ -f /etc/systemd/system/syncthing.service ]; then
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
fi
|
2016-07-09 11:28:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:55:09 +02:00
|
|
|
function backup_remote_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ -d /root/.config/syncthing ]; then
|
|
|
|
echo $"Backing up syncthing configuration"
|
|
|
|
function_check backup_directory_to_friend
|
|
|
|
backup_directory_to_friend /root/.config/syncthing syncthingconfig
|
|
|
|
echo $"Backup of syncthing configuration complete"
|
|
|
|
fi
|
|
|
|
if [ -d /var/lib/syncthing/SyncShared ]; then
|
|
|
|
echo $"Backing up syncthing shared files"
|
|
|
|
function_check backup_directory_to_friend
|
|
|
|
backup_directory_to_friend /var/lib/syncthing/SyncShared syncthingshared
|
|
|
|
echo $"Backup of syncthing shared files complete"
|
|
|
|
fi
|
2016-07-04 22:02:22 +02:00
|
|
|
}
|
|
|
|
|
2016-07-09 11:28:53 +02:00
|
|
|
function restore_remote_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ -f /etc/systemd/system/syncthing.service ]; then
|
|
|
|
systemctl stop syncthing
|
|
|
|
systemctl stop cron
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $SERVER_DIRECTORY/backup/syncthingconfig ]; then
|
|
|
|
echo $"Restoring syncthing configuration"
|
|
|
|
temp_restore_dir=/root/tempsyncthingconfig
|
|
|
|
function_check restore_directory_from_friend
|
|
|
|
restore_directory_from_friend $temp_restore_dir syncthingconfig
|
|
|
|
cp -r $temp_restore_dir/* /
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
exit 6833
|
2016-07-10 15:15:40 +02:00
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf $temp_restore_dir
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $SERVER_DIRECTORY/backup/syncthingshared ]; then
|
|
|
|
echo $"Restoring syncthing shared files"
|
|
|
|
temp_restore_dir=/root/tempsyncthingshared
|
|
|
|
function_check restore_directory_from_friend
|
|
|
|
restore_directory_from_friend $temp_restore_dir syncthingshared
|
|
|
|
cp -r $temp_restore_dir/* /
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
exit 37904
|
2016-07-10 15:15:40 +02:00
|
|
|
fi
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf $temp_restore_dir
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $SERVER_DIRECTORY/backup/syncthing ]; then
|
|
|
|
for d in $SERVER_DIRECTORY/backup/syncthing/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
|
|
|
|
if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
|
|
|
|
if [ ! -d /home/$USERNAME ]; then
|
|
|
|
${PROJECT_NAME}-adduser $USERNAME
|
|
|
|
fi
|
|
|
|
echo $"Restoring syncthing files for $USERNAME"
|
|
|
|
temp_restore_dir=/root/tempsyncthing
|
2016-07-10 15:15:40 +02:00
|
|
|
function_check restore_directory_from_friend
|
2016-07-24 14:19:56 +02:00
|
|
|
restore_directory_from_friend $temp_restore_dir syncthing/$USERNAME
|
|
|
|
cp -r $temp_restore_dir/home/$USERNAME/Sync /home/$USERNAME/
|
2016-07-10 15:15:40 +02:00
|
|
|
if [ ! "$?" = "0" ]; then
|
2016-07-24 14:19:56 +02:00
|
|
|
rm -rf $temp_restore_dir
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
exit 68438
|
2016-07-10 15:15:40 +02:00
|
|
|
fi
|
|
|
|
rm -rf $temp_restore_dir
|
|
|
|
|
2016-07-24 14:19:56 +02:00
|
|
|
# restore device IDs from config settings
|
|
|
|
if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
|
|
|
|
cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
|
|
|
|
chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
|
|
|
|
fi
|
|
|
|
if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
|
|
|
|
cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
|
|
|
|
chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
|
|
|
|
fi
|
|
|
|
echo $"Restore of syncthing files for $USERNAME complete"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2016-07-10 15:15:40 +02:00
|
|
|
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ -f /etc/systemd/system/syncthing.service ]; then
|
|
|
|
systemctl start syncthing
|
|
|
|
systemctl start cron
|
|
|
|
fi
|
2016-07-09 11:28:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 14:19:05 +02:00
|
|
|
function remove_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if ! grep -Fxq "install_syncthing" $COMPLETION_FILE; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
iptables -D INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
|
|
|
|
iptables -D INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
|
|
|
|
function_check save_firewall_settings
|
|
|
|
save_firewall_settings
|
|
|
|
|
|
|
|
systemctl stop syncthing
|
|
|
|
systemctl disable syncthing
|
|
|
|
apt-get -y remove --purge syncthing
|
|
|
|
rm /etc/systemd/system/syncthing.service
|
|
|
|
sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
|
|
|
|
sed -i '/install_syncthing/d' $COMPLETION_FILE
|
|
|
|
sed -i '/configure_firewall_for_syncthing/d' $COMPLETION_FILE
|
|
|
|
systemctl restart cron
|
2016-07-04 14:19:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 19:52:57 +02:00
|
|
|
function configure_firewall_for_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if grep -Fxq "configure_firewall_for_syncthing" $COMPLETION_FILE; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
iptables -A INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
|
|
|
|
iptables -A INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
|
|
|
|
function_check save_firewall_settings
|
|
|
|
save_firewall_settings
|
|
|
|
|
|
|
|
OPEN_PORTS+=("Syncthing $SYNCTHING_PORT")
|
|
|
|
echo 'configure_firewall_for_syncthing' >> $COMPLETION_FILE
|
2016-07-03 19:52:57 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 09:27:11 +02:00
|
|
|
function mesh_install_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
chroot "$rootdir" wget -q https://syncthing.net/release-key.txt -O- | apt-key add -
|
|
|
|
|
|
|
|
echo "deb http://apt.syncthing.net/ syncthing release" | tee $rootdir/etc/apt/sources.list.d/syncthing.list
|
|
|
|
chroot "$rootdir" apt-get update
|
|
|
|
chroot "$rootdir" apt-get -y --force-yes install syncthing
|
|
|
|
|
|
|
|
# This probably does need to run as root so that it can access the Sync directories
|
|
|
|
# in each user's home directory
|
|
|
|
echo '[Unit]' > $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'Documentation=man:syncthing(1)' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'After=network.target' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'Wants=syncthing-inotify@.service' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo '' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo '[Service]' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'User=root' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo "Environment='all_proxy=socks5://localhost:9050'" >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'Restart=on-failure' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'SuccessExitStatus=3 4' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'RestartForceExitStatus=3 4' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo '' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo '[Install]' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/syncthing.service
|
|
|
|
chroot "$rootdir" systemctl enable syncthing
|
|
|
|
|
|
|
|
if ! grep -q "syncthing" $rootdir/etc/crontab; then
|
|
|
|
echo "*/1 * * * * root /usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null" >> $rootdir/etc/crontab
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo 'mesh_install_syncthing'
|
2016-07-12 09:27:11 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:13:34 +02:00
|
|
|
function install_syncthing {
|
2016-07-24 14:19:56 +02:00
|
|
|
if [ $INSTALLING_MESH ]; then
|
|
|
|
mesh_install_syncthing
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if grep -Fxq "install_syncthing" $COMPLETION_FILE; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
apt-get -y install curl
|
|
|
|
|
|
|
|
curl -s https://syncthing.net/release-key.txt | apt-key add -
|
|
|
|
echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list
|
|
|
|
apt-get update
|
|
|
|
apt-get -y install syncthing
|
|
|
|
|
|
|
|
# This probably does need to run as root so that it can access the Sync directories
|
|
|
|
# in each user's home directory
|
|
|
|
echo '[Unit]' > /etc/systemd/system/syncthing.service
|
|
|
|
echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'Documentation=man:syncthing(1)' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'After=network.target' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'Wants=syncthing-inotify@.service' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo '' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo '[Service]' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'User=root' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo "Environment='all_proxy=socks5://localhost:9050'" >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'Restart=on-failure' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo '' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo '[Install]' >> /etc/systemd/system/syncthing.service
|
|
|
|
echo 'WantedBy=multi-user.target' >> /etc/systemd/system/syncthing.service
|
|
|
|
systemctl enable syncthing
|
|
|
|
systemctl daemon-reload
|
|
|
|
systemctl start syncthing
|
|
|
|
|
|
|
|
function_check cron_add_mins
|
|
|
|
cron_add_mins 1 "/usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null"
|
|
|
|
|
|
|
|
function_check configure_firewall_for_syncthing
|
|
|
|
configure_firewall_for_syncthing
|
|
|
|
echo 'install_syncthing' >> $COMPLETION_FILE
|
2016-07-03 17:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# NOTE: deliberately no exit 0
|