2016-07-03 17:13:34 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
# Backup functions
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
function configure_backup_key {
|
|
|
|
if grep -Fxq "configure_backup_key" $COMPLETION_FILE; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
apt-get -y install gnupg
|
|
|
|
|
|
|
|
BACKUP_KEY_EXISTS=$(gpg_key_exists "root" "$MY_NAME (backup key)")
|
|
|
|
if [[ $BACKUP_KEY_EXISTS == "yes" ]]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Generate a GPG key for backups
|
|
|
|
BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
|
|
|
|
if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
|
|
|
|
echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo "Name-Real: $MY_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo "Name-Comment: backup key" >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo $'Backup key does not exist. Creating it.'
|
|
|
|
su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
|
|
|
|
shred -zu /home/$MY_USERNAME/gpg-genkey.conf
|
|
|
|
echo $'Checking that the Backup key was created'
|
|
|
|
BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
|
|
|
|
if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
|
|
|
|
echo $'Backup key could not be created'
|
|
|
|
exit 43382
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
MY_BACKUP_KEY_ID=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\" | grep 'pub '" - $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
|
|
|
|
echo "Backup key: $MY_BACKUP_KEY_ID"
|
|
|
|
MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
|
|
|
|
su -c "gpg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - $MY_USERNAME
|
|
|
|
su -c "gpg --output ${MY_BACKUP_KEY}_private.asc --armor --export-secret-key $MY_BACKUP_KEY_ID" - $MY_USERNAME
|
|
|
|
if [ ! -f ${MY_BACKUP_KEY}_public.asc ]; then
|
|
|
|
echo 'Public backup key could not be exported'
|
|
|
|
exit 36829
|
|
|
|
fi
|
|
|
|
if [ ! -f ${MY_BACKUP_KEY}_private.asc ]; then
|
|
|
|
echo 'Private backup key could not be exported'
|
|
|
|
exit 29235
|
|
|
|
fi
|
|
|
|
|
|
|
|
# import backup key to root user
|
|
|
|
gpg --import --import ${MY_BACKUP_KEY}_public.asc
|
|
|
|
gpg --allow-secret-key-import --import ${MY_BACKUP_KEY}_private.asc
|
|
|
|
|
|
|
|
shred -zu ${MY_BACKUP_KEY}_public.asc
|
|
|
|
shred -zu ${MY_BACKUP_KEY}_private.asc
|
|
|
|
|
|
|
|
echo 'configure_backup_key' >> $COMPLETION_FILE
|
|
|
|
}
|
|
|
|
|
|
|
|
function backup_to_friends_servers {
|
|
|
|
# update crontab
|
|
|
|
echo '#!/bin/bash' > /etc/cron.daily/backuptofriends
|
|
|
|
echo "if [ -f /usr/local/bin/${PROJECT_NAME}-backup-remote ]; then" >> /etc/cron.daily/backuptofriends
|
|
|
|
echo " /usr/local/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
|
|
|
|
echo 'else' >> /etc/cron.daily/backuptofriends
|
|
|
|
echo " /usr/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
|
|
|
|
echo 'fi' >> /etc/cron.daily/backuptofriends
|
|
|
|
chmod +x /etc/cron.daily/backuptofriends
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:59:39 +02:00
|
|
|
function backup_mount_drive {
|
|
|
|
if [ $1 ]; then
|
|
|
|
USB_DRIVE=/dev/${1}1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get the admin user
|
|
|
|
ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
|
|
|
|
if [ $2 ]; then
|
|
|
|
ADMIN_USERNAME=$2
|
|
|
|
fi
|
|
|
|
ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
|
|
|
|
|
|
|
|
# check that the backup destination is available
|
|
|
|
if [ ! -b $USB_DRIVE ]; then
|
|
|
|
echo $"Please attach a USB drive"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# unmount if already mounted
|
|
|
|
umount -f $USB_MOUNT
|
|
|
|
if [ ! -d $USB_MOUNT ]; then
|
|
|
|
mkdir $USB_MOUNT
|
|
|
|
fi
|
|
|
|
if [ -f /dev/mapper/encrypted_usb ]; then
|
|
|
|
rm -rf /dev/mapper/encrypted_usb
|
|
|
|
fi
|
|
|
|
cryptsetup luksClose encrypted_usb
|
|
|
|
|
|
|
|
# mount the encrypted backup drive
|
|
|
|
cryptsetup luksOpen $USB_DRIVE encrypted_usb
|
|
|
|
if [ "$?" = "0" ]; then
|
|
|
|
USB_DRIVE=/dev/mapper/encrypted_usb
|
|
|
|
fi
|
|
|
|
mount $USB_DRIVE $USB_MOUNT
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
echo $"There was a problem mounting the USB drive to $USB_MOUNT"
|
|
|
|
rm -rf $USB_MOUNT
|
|
|
|
exit 783452
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function backup_unmount_drive {
|
|
|
|
if [ $1 ]; then
|
|
|
|
USB_DRIVE=${1}
|
|
|
|
if [ $2 ]; then
|
|
|
|
USB_MOUNT=${2}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
sync
|
|
|
|
umount $USB_MOUNT
|
|
|
|
if [ ! "$?" = "0" ]; then
|
|
|
|
echo $"Unable to unmount the drive. This means that the backup did not work"
|
|
|
|
rm -rf $USB_MOUNT
|
|
|
|
exit 9
|
|
|
|
fi
|
|
|
|
rm -rf $USB_MOUNT
|
|
|
|
if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
|
|
|
|
echo $"Unmount encrypted USB"
|
|
|
|
cryptsetup luksClose encrypted_usb
|
|
|
|
fi
|
|
|
|
if [ -f /dev/mapper/encrypted_usb ]; then
|
|
|
|
rm -rf /dev/mapper/encrypted_usb
|
|
|
|
fi
|
|
|
|
echo $"Backup to USB drive is complete. You can now unplug it."
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:13:34 +02:00
|
|
|
# NOTE: deliberately no exit 0
|