freedomboneeee/src/freedombone-app-syncthing

567 lines
21 KiB
Plaintext
Raw Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Syncthing application
#
# License
# =======
#
2017-07-02 18:48:31 +02:00
# Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# 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-10-12 11:49:12 +02:00
VARIANTS='full full-vim cloud'
2016-07-06 17:47:55 +02:00
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=0
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-10-05 23:33:41 +02:00
syncthing_variables=(SYNCTHING_ID
SYNCTHING_CONFIG_PATH
SYNCTHING_CONFIG_FILE
SYNCTHING_RELAY_SERVER
SYNCTHING_RELEASES
SYNCTHING_PORT
SYNCTHING_SHARED_DATA
USB_MOUNT)
function logging_on_syncthing {
echo -n ''
}
function logging_off_syncthing {
echo -n ''
}
2016-09-30 15:59:37 +02:00
function syncthing_create_ids_file {
if [ ! -f ~/.syncthing-server-id ]; then
return
fi
SYNCTHING_ID=$(cat ~/.syncthing-server-id)
if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
echo $'# Your syncthing configuration file' > $SYNCTHING_CONFIG_FILE
echo '#' >> $SYNCTHING_CONFIG_FILE
echo $"# The ${PROJECT_NAME} syncthing ID is: $SYNCTHING_ID" >> $SYNCTHING_CONFIG_FILE
echo '#' >> $SYNCTHING_CONFIG_FILE
echo '# Paste the IDs of your devices below' >> $SYNCTHING_CONFIG_FILE
echo '#' >> $SYNCTHING_CONFIG_FILE
fi
}
function syncthing_manual_edit {
if [ ! -f ~/.syncthing-server-id ]; then
return
fi
syncthing_create_ids_file
editor $SYNCTHING_CONFIG_FILE
# force an update of the configuration
touch ~/.syncthing-update
}
function syncthing_show_id {
if [ ! -f ~/.syncthing-server-id ]; then
return
fi
SYNCTHING_ID=$(cat ~/.syncthing-server-id)
dialog --title $"Device ID for ${PROJECT_NAME}" \
--backtitle $"Freedombone User Control Panel" \
2017-09-03 11:52:30 +02:00
--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\n\nAlternatively press Enter to display a QR code which can be scanned." 13 78
clear
echo $'Your Syncthing ID code'
echo ''
2017-09-03 12:02:50 +02:00
echo -n "$SYNCTHING_ID" | qrencode -t UTF8
echo ''
read -n1 -rsp $"Press any key to continue..." key
2016-09-30 15:59:37 +02:00
}
function syncthing_add_id {
if [ ! -f ~/.syncthing-server-id ]; then
return
fi
syncthing_create_ids_file
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"Add a Syncthing device ID" \
--form $"Paste the device ID for your laptop/desktop/netbook/phone/tablet below" 9 80 2 \
$"Device ID:" 1 1 "" 1 26 80 80 \
$"Description (optional):" 2 1 "" 2 26 80 80 \
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
SYNCTHING_DESCRIPTION=$(cat $data | sed -n 2p)
if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
return
fi
if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
dialog --title $"Add a Syncthing device ID" \
--backtitle $"Freedombone User Control Panel" \
--msgbox $"That doesn't look like a device ID" 6 50
return
fi
if grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
dialog --title $"Add a Syncthing device ID" \
--backtitle $"Freedombone User Control Panel" \
--msgbox $"That ID has already been added" 6 50
return
fi
if [ ${#SYNCTHING_DESCRIPTION} -gt 0 ]; then
echo "# $SYNCTHING_DESCRIPTION" >> $SYNCTHING_CONFIG_FILE
fi
echo "$SYNCTHING_DEVICE_ID" >> $SYNCTHING_CONFIG_FILE
# force an update of the configuration
touch ~/.syncthing-update
dialog --title $"Add a Syncthing device ID" \
--backtitle $"Freedombone User Control Panel" \
--msgbox $"The ID was added" 6 50
}
function syncthing_remove_id {
if [ ! -f ~/.syncthing-server-id ]; then
return
fi
syncthing_create_ids_file
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"Remove a Syncthing device ID" \
--form $"Paste the device ID which is to be removed below" 8 80 1 \
$"Device ID:" 1 1 "" 1 14 80 80 \
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
return
fi
if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
dialog --title $"Remove a Syncthing device ID" \
--backtitle $"Freedombone User Control Panel" \
--msgbox $"That doesn't look like a device ID" 6 50
return
fi
if ! grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
dialog --title $"Remove a Syncthing device ID" \
--backtitle $"Freedombone User Control Panel" \
--msgbox $"That ID wasn't registered anyway" 6 50
return
fi
sed -i "/$SYNCTHING_DEVICE_ID/d" $SYNCTHING_CONFIG_FILE
# force an update of the configuration
touch ~/.syncthing-update
dialog --title $"Remove a Syncthing device ID" \
--backtitle $"Freedombone User Control Panel" \
--msgbox $"The ID was removed" 6 50
}
function run_client_syncthing {
SYNCTHING_CONFIG_FILE=~/.syncthingids
SYNCTHING_ID=$(cat ~/.syncthing-server-id)
while true
do
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"File Synchronization" \
--radiolist $"Choose an operation:" 12 70 6 \
1 $"Show device ID for ${PROJECT_NAME}" off \
2 $"Add an ID for another machine or device" off \
3 $"Remove an ID for another machine or device" off \
4 $"Manually edit device IDs" off \
5 $"Back to main menu" on 2> $data
sel=$?
case $sel in
1) break;;
255) break;;
esac
case $(cat $data) in
1) syncthing_show_id;;
2) syncthing_add_id;;
3) syncthing_remove_id;;
4) syncthing_manual_edit;;
5) break;;
esac
done
}
function install_interactive_syncthing {
echo -n ''
APP_INSTALLED=1
}
2016-07-09 12:36:12 +02:00
function reconfigure_syncthing {
echo -n ''
2016-07-09 12:36:12 +02:00
}
2016-07-06 16:01:28 +02:00
function upgrade_syncthing {
echo -n ''
2016-07-06 16:01:28 +02:00
}
2016-07-06 15:55:09 +02:00
function backup_local_syncthing {
2017-09-03 12:20:52 +02:00
if [ -d $SYNCTHING_SHARED_DATA ]; then
function_check backup_directory_to_usb
2017-09-03 12:20:52 +02:00
backup_directory_to_usb $SYNCTHING_SHARED_DATA syncthingshared
2017-09-03 12:22:41 +02:00
backup_directory_to_usb $SYNCTHING_CONFIG_PATH syncthingconfig
fi
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
if [ -d /home/$USERNAME/Sync ]; then
echo $"Backing up syncthing files for $USERNAME"
backup_directory_to_usb /home/$USERNAME/Sync syncthing/$USERNAME
# ensure that device IDs will be backed up as part of user config settings
if [ ! -d /home/$USERNAME/.config/syncthing ]; then
mkdir -p /home/$USERNAME/.config/syncthing
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
fi
if [ -f /home/$USERNAME/.syncthing-server-id ]; then
cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
fi
if [ -f /home/$USERNAME/.syncthingids ]; then
cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
fi
fi
fi
done
2016-07-06 15:55:09 +02:00
}
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/* /
2017-09-03 12:22:41 +02:00
if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
mkdir -p $SYNCTHING_CONFIG_PATH
fi
2017-09-03 12:22:41 +02:00
cp -r ${temp_restore_dir}config/* $SYNCTHING_CONFIG_PATH/
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/* /
2017-09-03 12:20:52 +02:00
if [ ! -d $SYNCTHING_SHARED_DATA ]; then
mkdir -p $SYNCTHING_SHARED_DATA
fi
2017-09-03 12:20:52 +02:00
cp -r ${temp_restore_dir}shared/* $SYNCTHING_SHARED_DATA/
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}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; 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
if [ -d ${temp_restore_dir}/home/$USERNAME/Sync ]; then
cp -r ${temp_restore_dir}/home/$USERNAME/Sync /home/$USERNAME/
else
cp -r ${temp_restore_dir}/* /home/$USERNAME/Sync/
fi
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
}
2016-07-06 15:55:09 +02:00
function backup_remote_syncthing {
2017-09-03 12:22:41 +02:00
if [ -d $SYNCTHING_CONFIG_PATH ]; then
echo $"Backing up syncthing configuration"
function_check backup_directory_to_friend
2017-09-03 12:22:41 +02:00
backup_directory_to_friend $SYNCTHING_CONFIG_PATH syncthingconfig
echo $"Backup of syncthing configuration complete"
fi
2017-09-03 12:20:52 +02:00
if [ -d $SYNCTHING_SHARED_DATA ]; then
echo $"Backing up syncthing shared files"
function_check backup_directory_to_friend
2017-09-03 12:20:52 +02:00
backup_directory_to_friend $SYNCTHING_SHARED_DATA syncthingshared
echo $"Backup of syncthing shared files complete"
fi
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
if [ -d /home/$USERNAME/Sync ]; then
echo $"Backing up syncthing files for $USERNAME"
backup_directory_to_friend /home/$USERNAME/Sync syncthing/$USERNAME
# ensure that device IDs will be backed up as part of user config settings
if [ ! -d /home/$USERNAME/.config/syncthing ]; then
mkdir -p /home/$USERNAME/.config/syncthing
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
fi
if [ -f /home/$USERNAME/.syncthing-server-id ]; then
cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
fi
if [ -f /home/$USERNAME/.syncthingids ]; then
cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
fi
fi
fi
done
2016-07-04 22:02:22 +02:00
}
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/* /
2017-09-03 12:22:41 +02:00
if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
mkdir -p $SYNCTHING_CONFIG_PATH
fi
2017-09-03 12:22:41 +02:00
cp -r ${temp_restore_dir}config/* $SYNCTHING_CONFIG_PATH/
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/* /
2017-09-03 12:20:52 +02:00
if [ ! -d $SYNCTHING_SHARED_DATA ]; then
mkdir -p $SYNCTHING_SHARED_DATA
fi
2017-09-03 12:20:52 +02:00
cp -r ${temp_restore_dir}shared/* $SYNCTHING_SHARED_DATA/
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}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; 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
if [ -d $temp_restore_dir/home/$USERNAME/Sync ]; then
cp -r $temp_restore_dir/home/$USERNAME/Sync /home/$USERNAME/
else
cp -r $temp_restore_dir/* /home/$USERNAME/Sync/
fi
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
}
2016-07-04 14:19:05 +02:00
function remove_syncthing {
2016-10-22 11:39:34 +02:00
firewall_remove ${SYNCTHING_PORT}
systemctl stop syncthing
systemctl disable syncthing
rm /etc/systemd/system/syncthing.service
2017-06-10 22:37:50 +02:00
systemctl daemon-reload
apt-get -yq remove --purge syncthing
sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
2016-10-17 15:44:49 +02:00
remove_completion_param install_syncthing
remove_completion_param configure_firewall_for_syncthing
systemctl restart cron
2016-07-04 14:19:05 +02:00
}
function configure_firewall_for_syncthing {
2016-10-16 20:50:56 +02:00
if [[ $(is_completed $FUNCNAME) == "1" ]]; then
return
fi
2016-10-05 23:33:41 +02:00
2016-10-22 11:39:34 +02:00
firewall_add Syncthing ${SYNCTHING_PORT}
2016-10-16 20:50:56 +02:00
mark_completed $FUNCNAME
}
2017-07-02 18:48:31 +02:00
function install_syncthing_repo {
if [ -f /etc/apt/sources.list.d/syncthing.list ]; then
return
fi
2017-07-02 18:48:31 +02:00
apt-get -yq 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
2017-07-02 18:48:31 +02:00
}
function install_syncthing {
install_syncthing_repo
2016-10-23 20:38:14 +02:00
apt-get -yq 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
APP_INSTALLED=1
2016-07-03 17:13:34 +02:00
}
# NOTE: deliberately no exit 0