freedombone/src/freedombone-app-syncthing

335 lines
13 KiB
Bash
Executable File

#!/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/>.
VARIANTS='full cloud'
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'
function reconfigure_syncthing {
echo -n ''
}
function upgrade_syncthing {
echo -n ''
}
function backup_local_syncthing {
if [ -d /var/lib/syncthing/SyncShared ]; then
echo $"Backing up syncthing"
function_check backup_directory_to_usb
backup_directory_to_usb /var/lib/syncthing/SyncShared syncthingshared
backup_directory_to_usb /root/.config/syncthing syncthingconfig
echo $"Backup to syncthing complete"
fi
}
function restore_local_syncthing {
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
fi
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
fi
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" && $USERNAME != "tahoelafs" ]]; 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/
if [ ! "$?" = "0" ]; then
rm -rf ${temp_restore_dir}
set_user_permissions
backup_unmount_drive
systemctl start syncthing
systemctl start cron
exit 68438
fi
rm -rf ${temp_restore_dir}
# 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
if [ -f /etc/systemd/system/syncthing.service ]; then
systemctl start syncthing
systemctl start cron
fi
}
function backup_remote_syncthing {
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
}
function restore_remote_syncthing {
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
fi
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
fi
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" && $USERNAME != "tahoelafs" ]]; then
if [ ! -d /home/$USERNAME ]; then
${PROJECT_NAME}-adduser $USERNAME
fi
echo $"Restoring syncthing files for $USERNAME"
temp_restore_dir=/root/tempsyncthing
function_check restore_directory_from_friend
restore_directory_from_friend $temp_restore_dir syncthing/$USERNAME
cp -r $temp_restore_dir/home/$USERNAME/Sync /home/$USERNAME/
if [ ! "$?" = "0" ]; then
rm -rf $temp_restore_dir
systemctl start syncthing
systemctl start cron
exit 68438
fi
rm -rf $temp_restore_dir
# 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
if [ -f /etc/systemd/system/syncthing.service ]; then
systemctl start syncthing
systemctl start cron
fi
}
function remove_syncthing {
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
}
function configure_firewall_for_syncthing {
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
}
function mesh_install_syncthing {
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 '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
echo 'mesh_install_syncthing'
}
function install_syncthing {
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
}
# NOTE: deliberately no exit 0