2015-12-08 15:56:57 +01:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
# Backup to remote servers - the web of backups
|
|
|
|
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
2016-10-31 17:24:49 +01:00
|
|
|
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
|
2015-12-08 15:56:57 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2016-02-13 23:09:27 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2015-12-08 15:56:57 +01:00
|
|
|
# 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
|
2016-02-13 23:09:27 +01:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2015-12-08 15:56:57 +01:00
|
|
|
#
|
2016-02-13 23:09:27 +01:00
|
|
|
# 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/>.
|
2015-12-08 15:56:57 +01:00
|
|
|
|
|
|
|
PROJECT_NAME='freedombone'
|
|
|
|
COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
|
2016-10-13 12:03:10 +02:00
|
|
|
CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
|
2017-11-20 12:15:54 +01:00
|
|
|
MONGODB_APPS_FILE=$HOME/.mongodbapps
|
2015-12-11 17:06:28 +01:00
|
|
|
BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
|
2015-12-23 00:39:06 +01:00
|
|
|
ENABLE_VERIFICATION="no"
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-08 17:03:06 +01:00
|
|
|
export TEXTDOMAIN=${PROJECT_NAME}-backup-remote
|
2015-12-08 15:56:57 +01:00
|
|
|
export TEXTDOMAINDIR="/usr/share/locale"
|
|
|
|
|
2016-10-13 12:03:10 +02:00
|
|
|
PROJECT_INSTALL_DIR=/usr/local/bin
|
|
|
|
if [ -f /usr/bin/${PROJECT_NAME} ]; then
|
|
|
|
PROJECT_INSTALL_DIR=/usr/bin
|
|
|
|
fi
|
|
|
|
|
|
|
|
source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
|
|
|
|
|
2016-07-10 10:42:39 +02:00
|
|
|
# utilities needed for backup commands
|
2016-07-09 21:47:01 +02:00
|
|
|
UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
|
|
|
|
for f in $UTILS_FILES
|
|
|
|
do
|
|
|
|
source $f
|
|
|
|
done
|
|
|
|
|
2015-12-08 15:56:57 +01:00
|
|
|
# Temporary location for data to be backed up to other servers
|
|
|
|
SERVER_DIRECTORY=/root/remotebackup
|
|
|
|
|
2016-04-29 14:55:17 +02:00
|
|
|
# get the version of Go being used
|
2016-07-06 21:27:18 +02:00
|
|
|
GO_VERSION=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-go | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
|
2016-04-29 14:55:17 +02:00
|
|
|
|
2016-10-16 20:50:56 +02:00
|
|
|
ADMIN_USERNAME=$(get_completion_param "Admin user")
|
2015-12-08 15:56:57 +01:00
|
|
|
ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
|
|
|
|
ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
|
|
|
|
if [ ! -f /etc/ssl/private/backup.key ]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
echo $"Creating backup key"
|
|
|
|
${PROJECT_NAME}-addcert -h backup --dhkey 2048
|
2015-12-08 15:56:57 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
exit 1
|
2015-12-08 15:56:57 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# MariaDB password
|
2016-11-20 21:39:14 +01:00
|
|
|
DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
|
2015-12-08 15:56:57 +01:00
|
|
|
|
|
|
|
# local directory where the backup will be made
|
|
|
|
if [ ! -d $SERVER_DIRECTORY ]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
mkdir $SERVER_DIRECTORY
|
2015-12-08 15:56:57 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d $SERVER_DIRECTORY/backup ]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
mkdir -p $SERVER_DIRECTORY/backup
|
2015-12-08 15:56:57 +01:00
|
|
|
fi
|
|
|
|
|
2015-12-23 00:08:15 +01:00
|
|
|
# The name of a currently suspended site
|
|
|
|
# Sites are suspended so that verification should work
|
|
|
|
SUSPENDED_SITE=
|
|
|
|
|
|
|
|
function suspend_site {
|
2016-08-29 11:48:49 +02:00
|
|
|
# suspends a given website
|
|
|
|
if [[ $ENABLE_VERIFICATION != "yes" ]]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
SUSPENDED_SITE="$1"
|
|
|
|
nginx_dissite $SUSPENDED_SITE
|
2017-06-01 14:01:37 +02:00
|
|
|
systemctl reload nginx
|
2015-12-23 00:08:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function restart_site {
|
2016-08-29 11:48:49 +02:00
|
|
|
# restarts a given website
|
|
|
|
if [ ! $SUSPENDED_SITE ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
nginx_ensite $SUSPENDED_SITE
|
2017-06-01 14:01:37 +02:00
|
|
|
systemctl reload nginx
|
2016-08-29 11:48:49 +02:00
|
|
|
SUSPENDED_SITE=
|
2015-12-23 00:08:15 +01:00
|
|
|
}
|
|
|
|
|
2017-06-26 12:08:38 +02:00
|
|
|
function backup_blocklist {
|
|
|
|
if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
echo $"Backing up ${PROJECT_NAME} blocklist"
|
|
|
|
temp_backup_dir=/root/tempbackupblocklist
|
|
|
|
if [ ! -d $temp_backup_dir ]; then
|
|
|
|
mkdir -p $temp_backup_dir
|
|
|
|
fi
|
|
|
|
if [ -f $NODEJS_INSTALLED_APPS_FILE ]; then
|
|
|
|
cp -f /root/${PROJECT_NAME}-firewall-domains.cfg $temp_backup_dir
|
|
|
|
fi
|
|
|
|
backup_directory_to_friend $temp_backup_dir blocklist
|
|
|
|
rm -rf $temp_backup_dir
|
|
|
|
}
|
|
|
|
|
2016-10-24 23:25:56 +02:00
|
|
|
function backup_configfiles {
|
2016-08-29 11:48:49 +02:00
|
|
|
echo $"Backing up ${PROJECT_NAME} configuration files"
|
|
|
|
temp_backup_dir=/root/tempbackupconfig
|
|
|
|
if [ ! -d $temp_backup_dir ]; then
|
|
|
|
mkdir -p $temp_backup_dir
|
|
|
|
fi
|
2016-11-04 15:18:45 +01:00
|
|
|
if [ -f $NODEJS_INSTALLED_APPS_FILE ]; then
|
|
|
|
cp -f $NODEJS_INSTALLED_APPS_FILE $temp_backup_dir
|
|
|
|
fi
|
2016-11-23 13:36:01 +01:00
|
|
|
if [ -f /root/.nostore ]; then
|
|
|
|
cp -f /root/.nostore $temp_backup_dir
|
|
|
|
else
|
|
|
|
if [ -f $temp_backup_dir/.nostore ]; then
|
|
|
|
rm $temp_backup_dir/.nostore
|
|
|
|
fi
|
|
|
|
fi
|
2016-10-13 12:03:10 +02:00
|
|
|
cp -f $CONFIGURATION_FILE $temp_backup_dir
|
2016-08-29 11:48:49 +02:00
|
|
|
cp -f $COMPLETION_FILE $temp_backup_dir
|
|
|
|
if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
|
|
|
|
cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
|
|
|
|
fi
|
2017-11-20 12:15:54 +01:00
|
|
|
if [ -f $MONGODB_APPS_FILE ]; then
|
|
|
|
cp -f $MONGODB_APPS_FILE $temp_backup_dir
|
|
|
|
fi
|
2016-08-29 11:48:49 +02:00
|
|
|
# nginx password hashes
|
|
|
|
if [ -f /etc/nginx/.htpasswd ]; then
|
|
|
|
cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
|
|
|
|
fi
|
2016-10-24 23:25:56 +02:00
|
|
|
backup_directory_to_friend $temp_backup_dir configfiles
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_users {
|
2016-08-29 11:48:49 +02:00
|
|
|
for d in /home/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
|
2016-10-01 11:48:07 +02:00
|
|
|
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
|
|
|
|
# personal settings
|
|
|
|
if [ -d /home/$USERNAME/personal ]; then
|
|
|
|
echo $"Backing up personal settings for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# gpg keys
|
|
|
|
if [ -d /home/$USERNAME/.gnupg ]; then
|
|
|
|
echo $"Backing up gpg keys for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ssh keys
|
|
|
|
if [ -d /home/$USERNAME/.ssh ]; then
|
|
|
|
echo $"Backing up ssh keys for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# syncthing files
|
|
|
|
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
|
|
|
|
|
|
|
|
# config files
|
|
|
|
if [ -d /home/$USERNAME/.config ]; then
|
|
|
|
echo $"Backing up config files for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# monkeysphere files
|
|
|
|
if [ -d /home/$USERNAME/.monkeysphere ]; then
|
|
|
|
echo $"Backing up monkeysphere files for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# fin files
|
|
|
|
if [ -d /home/$USERNAME/.fin ]; then
|
|
|
|
echo $"Backing up fin files for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.fin fin/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# local files
|
|
|
|
if [ -d /home/$USERNAME/.local ]; then
|
|
|
|
echo $"Backing up local files for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.local local/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# mutt settings
|
|
|
|
if [ -f /home/$USERNAME/.muttrc ]; then
|
|
|
|
echo $"Backing up Mutt settings for $USERNAME"
|
|
|
|
if [ ! -d /home/$USERNAME/tempbackup ]; then
|
|
|
|
mkdir -p /home/$USERNAME/tempbackup
|
|
|
|
fi
|
|
|
|
cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
|
|
|
|
if [ -f /etc/Muttrc ]; then
|
|
|
|
cp /etc/Muttrc /home/$USERNAME/tempbackup
|
|
|
|
fi
|
|
|
|
backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# procmail settings
|
|
|
|
if [ -f /home/$USERNAME/.procmailrc ]; then
|
|
|
|
echo $"Backing up procmail settings for $USERNAME"
|
|
|
|
if [ ! -d /home/$USERNAME/tempbackup ]; then
|
|
|
|
mkdir -p /home/$USERNAME/tempbackup
|
|
|
|
fi
|
|
|
|
cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
|
|
|
|
backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# spamassassin settings
|
|
|
|
if [ -d /home/$USERNAME/.spamassassin ]; then
|
|
|
|
echo $"Backing up spamassassin settings for $USERNAME"
|
|
|
|
backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
# email
|
|
|
|
if [ -d /home/$USERNAME/Maildir ]; then
|
|
|
|
echo $"Stopping mail server"
|
|
|
|
systemctl stop exim4
|
|
|
|
echo $"Creating an email archive"
|
|
|
|
if [ ! -d /root/backupemail/$USERNAME ]; then
|
|
|
|
mkdir -p /root/backupemail/$USERNAME
|
|
|
|
fi
|
|
|
|
tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
|
|
|
|
echo $"Restarting mail server"
|
|
|
|
systemctl start exim4
|
|
|
|
echo $"Backing up emails for $USERNAME"
|
|
|
|
backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_letsencrypt {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ -d /etc/letsencrypt ]; then
|
|
|
|
echo $"Backing up Lets Encrypt settings"
|
|
|
|
backup_directory_to_friend /etc/letsencrypt letsencrypt
|
|
|
|
fi
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2016-11-19 15:28:07 +01:00
|
|
|
function backup_passwordstore {
|
|
|
|
if [ -d /root/.passwords ]; then
|
|
|
|
echo $"Backing up password store"
|
|
|
|
backup_directory_to_friend /root/.passwords passwordstore
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-12-29 18:41:22 +01:00
|
|
|
function backup_tor {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ -d /etc/letsencrypt ]; then
|
|
|
|
echo $"Backing up Tor settings"
|
|
|
|
backup_directory_to_friend /var/lib/tor tor
|
|
|
|
fi
|
2015-12-29 18:41:22 +01:00
|
|
|
}
|
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_certs {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ -d /etc/ssl ]; then
|
|
|
|
echo $"Backing up certificates"
|
|
|
|
backup_directory_to_friend /etc/ssl ssl
|
|
|
|
fi
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_mailing_list {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ -d /var/spool/mlmmj ]; then
|
|
|
|
echo $"Backing up the public mailing list"
|
|
|
|
backup_directory_to_friend /var/spool/mlmmj mailinglist
|
|
|
|
fi
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_web_server {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ -d /etc/nginx ]; then
|
|
|
|
echo $"Backing up web settings"
|
|
|
|
backup_directory_to_friend /etc/nginx/sites-available web
|
|
|
|
fi
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_admin_readme {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ -f /home/$ADMIN_USERNAME/README ]; then
|
|
|
|
echo $"Backing up README"
|
|
|
|
if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
|
|
|
|
mkdir -p /home/$ADMIN_USERNAME/tempbackup
|
|
|
|
fi
|
|
|
|
cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
|
|
|
|
backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
|
|
|
|
fi
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
2015-12-08 15:56:57 +01:00
|
|
|
|
2015-12-11 10:27:51 +01:00
|
|
|
function backup_mariadb {
|
2016-08-29 11:48:49 +02:00
|
|
|
if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
|
|
|
|
temp_backup_dir=/root/tempmariadb
|
|
|
|
if [ ! -d $temp_backup_dir ]; then
|
|
|
|
mkdir $temp_backup_dir
|
|
|
|
fi
|
2017-06-11 11:56:46 +02:00
|
|
|
keep_database_running
|
2016-08-29 11:48:49 +02:00
|
|
|
mysqldump --password=$DATABASE_PASSWORD mysql user > $temp_backup_dir/mysql.sql
|
|
|
|
if [ ! -s $temp_backup_dir/mysql.sql ]; then
|
|
|
|
echo $"Unable to backup MariaDB settings"
|
|
|
|
rm -rf $temp_backup_dir
|
|
|
|
# Send a warning email
|
|
|
|
echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
|
|
|
|
exit 653
|
|
|
|
fi
|
|
|
|
echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
|
|
|
|
chmod 400 $temp_backup_dir/db
|
|
|
|
backup_directory_to_friend $temp_backup_dir mariadb
|
|
|
|
fi
|
2015-12-11 10:27:51 +01:00
|
|
|
}
|
|
|
|
|
2017-11-05 17:21:13 +01:00
|
|
|
function backup_postgresql {
|
|
|
|
if [ ! -d /etc/postgresql ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
temp_backup_dir=/root/temppostgresql
|
|
|
|
if [ ! -d $temp_backup_dir ]; then
|
|
|
|
mkdir $temp_backup_dir
|
|
|
|
fi
|
|
|
|
sudo -u postgres pg_dumpall --roles-only > $temp_backup_dir/postgresql.sql
|
|
|
|
if [ ! -s $temp_backup_dir/postgresql.sql ]; then
|
|
|
|
echo $"Unable to backup postgresql settings"
|
|
|
|
rm -rf $temp_backup_dir
|
|
|
|
umount $USB_MOUNT
|
|
|
|
rm -rf $USB_MOUNT
|
|
|
|
exit 684365
|
|
|
|
fi
|
|
|
|
echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
|
|
|
|
chmod 400 $temp_backup_dir/db
|
|
|
|
backup_directory_to_friend $temp_backup_dir postgresql
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:17:37 +01:00
|
|
|
# Returns the filename of a key share
|
|
|
|
function get_key_share {
|
2016-08-29 11:48:49 +02:00
|
|
|
no_of_shares=$1
|
|
|
|
USERNAME="$2"
|
|
|
|
REMOTE_DOMAIN="$3"
|
2015-12-11 11:17:37 +01:00
|
|
|
|
2016-08-29 11:48:49 +02:00
|
|
|
# Get a share index based on the supplied domain name
|
|
|
|
# This ensures that the same share is always given to the same domain
|
|
|
|
sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
|
|
|
|
share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
|
2015-12-11 11:17:37 +01:00
|
|
|
|
2016-08-29 11:48:49 +02:00
|
|
|
# get the filename
|
|
|
|
share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
|
|
|
|
share_filename=${share_files[share_index]}
|
2015-12-11 11:17:37 +01:00
|
|
|
|
2016-08-29 11:48:49 +02:00
|
|
|
echo "$share_filename"
|
2015-12-11 11:17:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function disperse_key_shares {
|
2016-08-29 11:48:49 +02:00
|
|
|
USERNAME=$1
|
|
|
|
REMOTE_DOMAIN=$2
|
|
|
|
REMOTE_SSH_PORT=$3
|
|
|
|
REMOTE_PASSWORD=$4
|
|
|
|
REMOTE_SERVER=$5
|
|
|
|
|
|
|
|
if [ -d /home/$USERNAME/.gnupg_fragments ]; then
|
|
|
|
if [ $REMOTE_DOMAIN ]; then
|
|
|
|
cd /home/$USERNAME/.gnupg_fragments
|
|
|
|
no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
|
|
|
|
if (( no_of_shares > 1 )); then
|
|
|
|
share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
|
|
|
|
|
|
|
|
# create a temp directory containing the share
|
|
|
|
temp_key_share_dir=/home/$USERNAME/tempkey
|
|
|
|
temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
|
|
|
|
mkdir -p $temp_key_share_fragments
|
|
|
|
cp $share_filename $temp_key_share_fragments/
|
|
|
|
|
|
|
|
# copy the fragments directory to the remote server
|
|
|
|
/usr/bin/sshpass -p "$REMOTE_PASSWORD" \
|
|
|
|
scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
# Send a warning email
|
|
|
|
echo "Key share to $REMOTE_SERVER failed" | \
|
|
|
|
mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
|
|
|
|
else
|
|
|
|
# Send a confirmation email
|
|
|
|
echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
|
|
|
|
mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
|
|
|
|
fi
|
|
|
|
|
|
|
|
# remove the temp file/directory
|
|
|
|
shred -zu $temp_key_share_fragments/*
|
|
|
|
rm -rf $temp_key_share_dir
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2015-12-11 11:17:37 +01:00
|
|
|
}
|
|
|
|
|
2015-12-23 11:29:55 +01:00
|
|
|
TEST_MODE="no"
|
|
|
|
if [[ "$1" == "test" ]]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
TEST_MODE="yes"
|
2015-12-23 11:29:55 +01:00
|
|
|
fi
|
|
|
|
|
2016-10-24 23:25:56 +02:00
|
|
|
backup_configfiles
|
2015-12-23 11:29:55 +01:00
|
|
|
if [[ $TEST_MODE == "no" ]]; then
|
2017-06-26 12:08:38 +02:00
|
|
|
backup_blocklist
|
2016-08-29 11:48:49 +02:00
|
|
|
backup_users
|
|
|
|
backup_letsencrypt
|
2016-11-19 15:28:07 +01:00
|
|
|
backup_passwordstore
|
2016-08-29 11:48:49 +02:00
|
|
|
backup_tor
|
|
|
|
backup_web_server
|
|
|
|
backup_admin_readme
|
|
|
|
backup_mariadb
|
2017-11-05 17:21:13 +01:00
|
|
|
backup_postgresql
|
2016-08-29 11:48:49 +02:00
|
|
|
backup_certs
|
|
|
|
backup_mailing_list
|
|
|
|
backup_apps remote
|
2016-10-13 12:03:10 +02:00
|
|
|
backup_extra_directories remote
|
2015-12-23 11:22:47 +01:00
|
|
|
fi
|
2015-12-08 15:56:57 +01:00
|
|
|
|
|
|
|
# For each remote server
|
|
|
|
while read remote_server
|
|
|
|
do
|
2016-08-29 11:48:49 +02:00
|
|
|
# Get the server and its password
|
|
|
|
# Format is:
|
|
|
|
# username@domain <port number> /home/username <ssh password>
|
|
|
|
REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
|
|
|
|
if [ $REMOTE_SERVER ]; then
|
|
|
|
REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
|
|
|
|
REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
|
|
|
|
REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
|
|
|
|
REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
|
|
|
|
NOW=$(date +"%Y-%m-%d %H:%M:%S")
|
|
|
|
REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
|
|
|
|
|
|
|
|
echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
|
|
|
|
|
|
|
|
# Social key management
|
|
|
|
for d in /home/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
|
2016-10-01 11:48:07 +02:00
|
|
|
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
|
2016-08-29 11:48:49 +02:00
|
|
|
disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $TEST_MODE == "yes" ]]; then
|
|
|
|
echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
|
|
|
|
fi
|
|
|
|
rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
|
|
|
|
# Send a warning email
|
|
|
|
echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
|
|
|
|
else
|
|
|
|
echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
|
|
|
|
fi
|
|
|
|
fi
|
2015-12-08 15:56:57 +01:00
|
|
|
|
|
|
|
done < /home/${ADMIN_USERNAME}/backup.list
|
|
|
|
|
|
|
|
exit 0
|