Skip to content
Snippets Groups Projects
freedombone-utils-backup 23.9 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Backup functions
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2014-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# 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/>.

Bob Mottram's avatar
Bob Mottram committed
# whether a given site is being suspended during backup
SUSPENDED_SITE=

Bob Mottram's avatar
Bob Mottram committed
# Dummy password used for the backup key
BACKUP_DUMMY_PASSWORD='backup'

BACKUP_TEMP_DIRECTORY=/root/.backuptemp
Bob Mottram's avatar
Bob Mottram committed
BACKUP_GPG_OPTIONS="--pinentry-mode loopback"
Bob Mottram's avatar
Bob Mottram committed
# Stores a list of drives when the system was first installed.
# This is used to detect drives later connected
DRIVES_BASELINE_FILE=/root/.drives_baseline

# timeout used for backups to prevent hangs
BACKUP_TIMEOUT_SEC=7200

# Files which contain the percentage progress
backup_progress_file=/root/.backup_progress.txt
Bob Mottram's avatar
Bob Mottram committed
restore_progress_file=/root/.restore_progress.txt
function create_backups_temp_directory {
    if [ ! -d $BACKUP_TEMP_DIRECTORY ]; then
        mkdir $BACKUP_TEMP_DIRECTORY
    fi
}

function remove_backups_temp_directory {
    if [ -d $BACKUP_TEMP_DIRECTORY ]; then
        rm -rf $BACKUP_TEMP_DIRECTORY
    fi
}

Bob Mottram's avatar
Bob Mottram committed
function suspend_site {
    # suspends a given website
    SUSPENDED_SITE="$1"
Bob Mottram's avatar
Bob Mottram committed
    nginx_dissite "$SUSPENDED_SITE"
Bob Mottram's avatar
Bob Mottram committed
    systemctl reload nginx
Bob Mottram's avatar
Bob Mottram committed
}

function restart_site {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$SUSPENDED_SITE" ]; then
Bob Mottram's avatar
Bob Mottram committed
    nginx_ensite "$SUSPENDED_SITE"
Bob Mottram's avatar
Bob Mottram committed
    systemctl reload nginx
Bob Mottram's avatar
Bob Mottram committed
function configure_backup_key {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
    $INSTALL_PACKAGES gnupg dirmngr
    printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > ~/.gnupg/S.dirmngr

    BACKUP_KEY_EXISTS=$(gpg_key_exists "root" "$MY_NAME (backup key)")
    if [[ $BACKUP_KEY_EXISTS == "yes" ]]; then
        return
    fi

Bob Mottram's avatar
Bob Mottram committed
    gpg_agent_setup "$MY_USERNAME"
    # Generate a GPG key for backups
    BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
    if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        { echo 'Key-Type: rsa';
          echo 'Key-Length: 2048';
Bob Mottram's avatar
Bob Mottram committed
          echo 'Subkey-Type: rsa';
          echo 'Subkey-Length: 2048';
Bob Mottram's avatar
Bob Mottram committed
          echo "Name-Real:  $MY_NAME";
          echo "Name-Email: $MY_EMAIL_ADDRESS";
          echo "Name-Comment: backup key";
          echo 'Expire-Date: 0'; } > "/home/$MY_USERNAME/gpg-genkey.conf"
        cat "/home/$MY_USERNAME/gpg-genkey.conf"
        echo "Passphrase: $BACKUP_DUMMY_PASSWORD" >> "/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.'
Bob Mottram's avatar
Bob Mottram committed
        su -m root -c "gpg --homedir /home/$MY_USERNAME/.gnupg --batch --full-gen-key /home/$MY_USERNAME/gpg-genkey.conf" - "$MY_USERNAME"
        chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg"
Bob Mottram's avatar
Bob Mottram committed
        rm "/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'
Bob Mottram's avatar
Bob Mottram committed
    MY_BACKUP_KEY_ID=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\"" - "$MY_USERNAME" | sed -n '2p' | sed 's/^[ \t]*//')
    echo "Backup key: $MY_BACKUP_KEY_ID"
    MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
Bob Mottram's avatar
Bob Mottram committed
    su -m root -c "gpg --homedir /home/$MY_USERNAME/.gnupg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - "$MY_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "${MY_BACKUP_KEY}_public.asc" ]; then
        echo 'Public backup key could not be exported'
Bob Mottram's avatar
Bob Mottram committed
    su -m root -c "echo '$BACKUP_DUMMY_PASSWORD' | gpg --homedir /home/$MY_USERNAME/.gnupg --output ${MY_BACKUP_KEY}_private.asc --armor --batch --passphrase-fd 0 --export-secret-key $MY_BACKUP_KEY_ID" - "$MY_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "${MY_BACKUP_KEY}_private.asc" ]; then
        echo 'Private backup key could not be exported'
Bob Mottram's avatar
Bob Mottram committed
    gpg --import --import "${MY_BACKUP_KEY}_public.asc"
    echo "$BACKUP_DUMMY_PASSWORD" | gpg --batch --passphrase-fd 0 --allow-secret-key-import --import "${MY_BACKUP_KEY}_private.asc"
Bob Mottram's avatar
Bob Mottram committed
    rm "${MY_BACKUP_KEY}_public.asc"
    rm "${MY_BACKUP_KEY}_private.asc"
Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
Bob Mottram's avatar
Bob Mottram committed
function backup_mount_drive {
Bob Mottram's avatar
Bob Mottram committed
    if [ "$1" ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [[ "$1" == "/dev/"* ]]; then
            USB_DRIVE="$1"
Bob Mottram's avatar
Bob Mottram committed
        else
            USB_DRIVE=/dev/${1}1
        fi
Bob Mottram's avatar
Bob Mottram committed
    ADMIN_USERNAME=$(get_completion_param "Admin user")
Bob Mottram's avatar
Bob Mottram committed
    if [ "$2" ]; then
        ADMIN_USERNAME="$2"
Bob Mottram's avatar
Bob Mottram committed
    ADMIN_NAME=$(getent passwd "$ADMIN_USERNAME" | cut -d: -f5 | cut -d, -f1)
Bob Mottram's avatar
Bob Mottram committed
    if [ "$3" ]; then
        RESTORE_APP="$3"
    fi

    # check that the backup destination is available
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -b "$USB_DRIVE" ]; then
        echo $"Please attach a USB drive"
        exit 1
    fi

    # unmount if already mounted
Bob Mottram's avatar
Bob Mottram committed
    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
Bob Mottram's avatar
Bob Mottram committed
    cryptsetup close encrypted_usb

    # mount the encrypted backup drive
    # shellcheck disable=SC2154,SC2086
    if [ ! $simple_backup ]; then
        if cryptsetup open --type luks "$USB_DRIVE" encrypted_usb; then
            USB_DRIVE=/dev/mapper/encrypted_usb
        fi
Bob Mottram's avatar
Bob Mottram committed
    if ! mount "$USB_DRIVE" "$USB_MOUNT"; then
        echo $"There was a problem mounting the USB drive to $USB_MOUNT"
Bob Mottram's avatar
Bob Mottram committed
        rm -rf "$USB_MOUNT"
Bob Mottram's avatar
Bob Mottram committed
}

function backup_unmount_drive {
Bob Mottram's avatar
Bob Mottram committed
    #if [ $1 ]; then
    #    USB_DRIVE=${1}
    #    if [ $2 ]; then
    #        USB_MOUNT=${2}
    #    fi
    #fi
Bob Mottram's avatar
Bob Mottram committed
    if ! umount "$USB_MOUNT"; then
        echo $"Unable to unmount the drive."
Bob Mottram's avatar
Bob Mottram committed
        rm -rf "$USB_MOUNT"
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "$USB_MOUNT"
    if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
        echo $"Unmount encrypted USB"
Bob Mottram's avatar
Bob Mottram committed
        cryptsetup close encrypted_usb
    fi
    if [ -f /dev/mapper/encrypted_usb ]; then
        rm -rf /dev/mapper/encrypted_usb
    fi
Bob Mottram's avatar
Bob Mottram committed
function backup_database_local_usb {
    if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
        echo $"No MariaDB password was given"
        function_check restart_site
        restart_site
        exit 10
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$USB_MOUNT/backup/${1}" ]; then
        mkdir -p "$USB_MOUNT/backup/${1}"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$USB_MOUNT/backup/${1}data" ]; then
        mkdir -p "$USB_MOUNT/backup/${1}data"
    local_database_dir=/root/temp${1}data
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${local_database_dir}" ]; then
        mkdir -p "${local_database_dir}"
    fi
    echo $"Obtaining ${1} database backup"
Bob Mottram's avatar
Bob Mottram committed
    database_file_extension='sql'
Bob Mottram's avatar
Bob Mottram committed
    if [ "$USE_MONGODB" ]; then
Bob Mottram's avatar
Bob Mottram committed
        database_file_extension='mdb'
        USE_POSTGRESQL=
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! $USE_POSTGRESQL ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [ ! "$USE_MONGODB" ]; then
Bob Mottram's avatar
Bob Mottram committed
            USE_MONGODB=
            USE_POSTGRESQL=
            keep_database_running
Bob Mottram's avatar
Bob Mottram committed
            mysqldump --lock-tables --password="$DATABASE_PASSWORD" "${1}" > "${local_database_dir}/${1}.${database_file_extension}"
Bob Mottram's avatar
Bob Mottram committed
        else
            USE_MONGODB=
            USE_POSTGRESQL=
Bob Mottram's avatar
Bob Mottram committed
            mongodump --db "${1}" --archive="${local_database_dir}/${1}.${database_file_extension}" --gzip
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        USE_MONGODB=
Bob Mottram's avatar
Bob Mottram committed
        USE_POSTGRESQL=
        cd /etc/postgresql || exit 67
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=2024
        sudo -u postgres pg_dump "${1}" > "${local_database_dir}/${1}.${database_file_extension}"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "${local_database_dir}/${1}.${database_file_extension}" ]; then
        if [ ! -s "${local_database_dir}/${1}.${database_file_extension}" ]; then
            echo $"${1} database could not be saved"
Bob Mottram's avatar
Bob Mottram committed
            rm -rf "${local_database_dir}"
            umount "$USB_MOUNT"
            rm -rf "$USB_MOUNT"
            restart_site
        fi
    else
        echo $"${1} database could not be dumped"
Bob Mottram's avatar
Bob Mottram committed
        rm -rf "${local_database_dir}"
        umount "$USB_MOUNT"
        rm -rf "$USB_MOUNT"
    echo $"Database dump was created for ${1}"
function backup_directory_to_usb_duplicity {
    create_backups_temp_directory
Bob Mottram's avatar
Bob Mottram committed
    echo "$BACKUP_DUMMY_PASSWORD" | duplicity full --use-agent --gpg-options "$BACKUP_GPG_OPTIONS" --tempdir "$BACKUP_TEMP_DIRECTORY" --encrypt-key "$MY_BACKUP_KEY_ID" --full-if-older-than 4W --exclude-other-filesystems "${1}" "file://$USB_MOUNT/backup/${2}"
    # shellcheck disable=SC2181
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$?" = "0" ]; then
Bob Mottram's avatar
Bob Mottram committed
        umount "$USB_MOUNT"
        rm -rf "$USB_MOUNT"
Bob Mottram's avatar
Bob Mottram committed
        if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
Bob Mottram's avatar
Bob Mottram committed
            rm -rf "${1}"
Bob Mottram's avatar
Bob Mottram committed
        fi
        function_check restart_site
        restart_site
        remove_backups_temp_directory
    if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        echo "$BACKUP_DUMMY_PASSWORD" | duplicity verify --use-agent --gpg-options "$BACKUP_GPG_OPTIONS" --tempdir "$BACKUP_TEMP_DIRECTORY" --encrypt-key "$MY_BACKUP_KEY_ID" --full-if-older-than 4W --exclude-other-filesystems "${1}" "file://$USB_MOUNT/backup/${2}"
        # shellcheck disable=SC2181
        if [ ! "$?" = "0" ]; then
Bob Mottram's avatar
Bob Mottram committed
            umount "$USB_MOUNT"
            rm -rf "$USB_MOUNT"
            if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
Bob Mottram's avatar
Bob Mottram committed
                rm -rf "${1}"
            fi
            function_check restart_site
            restart_site
            remove_backups_temp_directory
    remove_backups_temp_directory
function increment_backup_progress {
    backup_counter=$((backup_counter + 1))
    echo -n "$backup_counter" > "$backup_progress_file"
}

function increment_restore_progress {
    restore_counter=$((restore_counter + 1))
    echo -n "$restore_counter" > "$restore_progress_file"
}

Bob Mottram's avatar
Bob Mottram committed
function backup_directory_to_usb {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${1}" ]; then
        echo $"WARNING: directory does not exist: ${1}"
    else
        BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=SC2181
        if [ ! "$?" = "0" ]; then
            echo $"Backup key could not be found"
            function_check restart_site
            restart_site
            exit 6
        fi

        MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d "$USB_MOUNT/backup/${2}" ]; then
            mkdir -p "$USB_MOUNT/backup/${2}"
Bob Mottram's avatar
Bob Mottram committed
        backup_directory_to_usb_duplicity "${1}" "${2}"
        if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
Bob Mottram's avatar
Bob Mottram committed
            rm -rf "${1}"

        increment_backup_progress
function restore_directory_from_usb_duplicity {
    create_backups_temp_directory
Bob Mottram's avatar
Bob Mottram committed
    PASSPHRASE="$BACKUP_DUMMY_PASSWORD" duplicity restore --gpg-options "$BACKUP_GPG_OPTIONS" --tempdir "$BACKUP_TEMP_DIRECTORY" --force "file://$USB_MOUNT/backup/${2}" "${1}"
    # shellcheck disable=SC2181
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$?" = "0" ]; then
        echo "WARNING: failed to restore $USB_MOUNT/backup/${2} to ${1}"
    fi
    remove_backups_temp_directory
function restore_directory_from_usb {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "${1}" ]; then
        echo "$USB_MOUNT/backup/${2} -> ${1}"
Bob Mottram's avatar
Bob Mottram committed
        echo $'No restore destination given'
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "${2}" ]; then
        echo "$USB_MOUNT/backup/${2} -> ${1}"
Bob Mottram's avatar
Bob Mottram committed
        echo $'No restore source given'
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${1}" ]; then
        mkdir "${1}"
Bob Mottram's avatar
Bob Mottram committed
    restore_directory_from_usb_duplicity "${1}" "${2}"
    increment_restore_progress
function backup_database_to_usb {
    local_database_dir=/root/temp${1}data
Bob Mottram's avatar
Bob Mottram committed
    backup_database_local_usb "${database_name}"
    if [ ! -f "${local_database_dir}/${1}.sql" ]; then
        echo $"Error backing up ${1} database to ${local_database_dir}/${1}.sql"
    increment_backup_progress

Bob Mottram's avatar
Bob Mottram committed
    backup_directory_to_usb "${local_database_dir}" "${database_name}data"
# after user files have been restored permissions may need to be set
function set_user_permissions {
    echo $"Setting permissions"
    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
Bob Mottram's avatar
Bob Mottram committed
        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
            chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME"
function backup_database_remote {
    if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
        echo $"No MariaDB password was given"
        function_check restart_site
        restart_site
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$SERVER_DIRECTORY/backup/${1}" ]; then
        mkdir -p "$SERVER_DIRECTORY/backup/${1}"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$SERVER_DIRECTORY/backup/${1}data" ]; then
        mkdir -p "$SERVER_DIRECTORY/backup/${1}data"
    local_database_dir=/root/temp${1}data
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${local_database_dir}" ]; then
        mkdir -p "${local_database_dir}"
    echo "Obtaining ${1} database backup"
Bob Mottram's avatar
Bob Mottram committed
    database_file_extension='sql'
    if [ $USE_MONGODB ]; then
        database_file_extension='mdb'
        USE_POSTGRESQL=
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! $USE_POSTGRESQL ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [ ! $USE_MONGODB ]; then
            USE_MONGODB=
            USE_POSTGRESQL=
            keep_database_running
Bob Mottram's avatar
Bob Mottram committed
            mysqldump --lock-tables --password="$DATABASE_PASSWORD" "${1}" > "${local_database_dir}/${1}.${database_file_extension}"
Bob Mottram's avatar
Bob Mottram committed
        else
            USE_MONGODB=
            USE_POSTGRESQL=
Bob Mottram's avatar
Bob Mottram committed
            mongodump --db "${1}" --archive="${local_database_dir}/${1}.${database_file_extension}" --gzip
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        USE_MONGODB=
Bob Mottram's avatar
Bob Mottram committed
        USE_POSTGRESQL=
        cd /etc/postgresql || exit 78
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=SC2024
        sudo -u postgres pg_dump "${1}" > "${local_database_dir}/${1}.${database_file_extension}"
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "${local_database_dir}/${1}.${database_file_extension}" ]; then
        if [ ! -s "${local_database_dir}/${1}.${database_file_extension}" ]; then
            echo $"${1} database could not be saved"
Bob Mottram's avatar
Bob Mottram committed
            rm -rf "${local_database_dir}"
            # Send a warning email
Bob Mottram's avatar
Bob Mottram committed
            /bin/bash "/usr/local/bin/${PROJECT_NAME}-notification" -m $"Unable to export ${1} database" -s $"${PROJECT_NAME} backup to friends"
            function_check restart_site
            restart_site
        fi
    else
        echo $"${1} database could not be dumped"
Bob Mottram's avatar
Bob Mottram committed
        rm -rf "${local_database_dir}"
        "${PROJECT_NAME}-notification" -m $"Unable to dump ${1} database" -s $"${PROJECT_NAME} backup to friends"
        function_check restart_site
        restart_site
Bob Mottram's avatar
Bob Mottram committed
function backup_apps {
    detect_installable_apps
    get_apps_installed_names

Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2068
    for app_name in ${APPS_INSTALLED_NAMES[@]}
        echo $"Backup ${app_name}"
Bob Mottram's avatar
Bob Mottram committed
        app_load_variables "${app_name}"
        function_check "backup_${localremote}_${app_name}"
        "backup_${localremote}_${app_name}"
        BACKUP_APPS_COMPLETED+=("${app_name}")
        echo $"Backup ${app_name} completed"
function restore_apps {
Bob Mottram's avatar
Bob Mottram committed
    RESTORE_APP=$2
    detect_installable_apps
    get_apps_installed_names

Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2068
    for app_name in ${APPS_INSTALLED_NAMES[@]}
Bob Mottram's avatar
Bob Mottram committed
        confirm_restore=
Bob Mottram's avatar
Bob Mottram committed
        if [ ! "$2" ]; then
Bob Mottram's avatar
Bob Mottram committed
            confirm_restore=1
        else
Bob Mottram's avatar
Bob Mottram committed
            if [[ "$RESTORE_APP" == "$app_name" || "$RESTORE_APP" == "all" ]]; then
Bob Mottram's avatar
Bob Mottram committed
                confirm_restore=1
            fi
        fi
        if [ $confirm_restore ]; then
            echo $"Restoring ${app_name}"
Bob Mottram's avatar
Bob Mottram committed
            app_load_variables "${app_name}"
            function_check "restore_${localremote}_${app_name}"
            "restore_${localremote}_${app_name}"
Bob Mottram's avatar
Bob Mottram committed
            RESTORE_APPS_COMPLETED+=("${app_name}")
            echo $"Restored ${app_name}"
        fi
}

function restore_database {
Bob Mottram's avatar
Bob Mottram committed
    restore_app_name=$1
Bob Mottram's avatar
Bob Mottram committed
    restore_app_domain=$2
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$USB_MOUNT/backup/${restore_app_name}data" ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $"Restoring ${restore_app_name} database"
Bob Mottram's avatar
Bob Mottram committed
        local_database_dir=/root/temp${restore_app_name}data
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "${local_database_dir}" ]; then
            rm -rf "${local_database_dir}"
        function_check restore_directory_from_usb
Bob Mottram's avatar
Bob Mottram committed
        restore_directory_from_usb "${local_database_dir}" "${restore_app_name}data"
Bob Mottram's avatar
Bob Mottram committed
        database_file_extension='sql'
        if [ $USE_MONGODB ]; then
            database_file_extension='mdb'
            USE_POSTGRESQL=
        fi
Bob Mottram's avatar
Bob Mottram committed
        database_file="${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.${database_file_extension}"
        if [ ! -f "$database_file" ]; then
            database_file="${local_database_dir}/${restore_app_name}.${database_file_extension}"
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -f "$database_file" ]; then
Bob Mottram's avatar
Bob Mottram committed
            echo $"Unable to restore ${restore_app_name} database"
Bob Mottram's avatar
Bob Mottram committed
            rm -rf "${local_database_dir}"
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
        if [ ! $USE_POSTGRESQL ]; then
Bob Mottram's avatar
Bob Mottram committed
            if [ ! $USE_MONGODB ]; then
                USE_MONGODB=
                USE_POSTGRESQL=
                keep_database_running
Bob Mottram's avatar
Bob Mottram committed
                cp "$database_file" ~/test.sql
                mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" "${restore_app_name}" -o < "$database_file")
Bob Mottram's avatar
Bob Mottram committed
            else
                USE_MONGODB=
                USE_POSTGRESQL=
Bob Mottram's avatar
Bob Mottram committed
                mongorestore --gzip --archive="$database_file" --db "${restore_app_name}"
Bob Mottram's avatar
Bob Mottram committed
            fi
Bob Mottram's avatar
Bob Mottram committed
            USE_MONGODB=
            USE_POSTGRESQL=
            cd /etc/postgresql || exit 63
Bob Mottram's avatar
Bob Mottram committed
            mysqlsuccess=$(sudo -u postgres pg_restore "$database_file")
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=SC2181
        if [ ! "$?" = "0" ]; then
            echo "$mysqlsuccess"
            function_check set_user_permissions
            set_user_permissions
            function_check set_user_permissions
            backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data" ]; then
Bob Mottram's avatar
Bob Mottram committed
            rm "${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/"*
Bob Mottram's avatar
Bob Mottram committed
            rm "${local_database_dir}/*.${database_file_extension}"
Bob Mottram's avatar
Bob Mottram committed
        rm -rf "${local_database_dir}"
Bob Mottram's avatar
Bob Mottram committed
        echo $"Restoring ${restore_app_name} installation"
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d "/root/temp${restore_app_name}" ]; then
            mkdir "/root/temp${restore_app_name}"
        fi
        function_check restore_directory_from_usb
Bob Mottram's avatar
Bob Mottram committed
        restore_directory_from_usb "/root/temp${restore_app_name}" "${restore_app_name}"
Bob Mottram's avatar
Bob Mottram committed
        if [ "${restore_app_domain}" ]; then
            # create directory to restore to
Bob Mottram's avatar
Bob Mottram committed
            if [ ! -d "/var/www/${restore_app_domain}/htdocs" ]; then
                mkdir -p "/var/www/${restore_app_domain}/htdocs"
                chown www-data:www-data "/var/www/${restore_app_domain}/htdocs"
Bob Mottram's avatar
Bob Mottram committed
            if [ -d "/var/www/${restore_app_domain}/htdocs" ]; then
                restore_from_dir="/root/temp${restore_app_name}/${RESTORE_SUBDIR}/www/${restore_app_domain}/htdocs"
                if [ ! -d "$restore_from_dir" ]; then
                    restore_from_dir=/root/temp${restore_app_name}
                fi
Bob Mottram's avatar
Bob Mottram committed
                if [ -d "$restore_from_dir" ]; then
                    if [ -d "/root/temp${restore_app_name}/${RESTORE_SUBDIR}/www/${restore_app_domain}/htdocs" ]; then
                        rm -rf "/var/www/${restore_app_domain}/htdocs"
Bob Mottram's avatar
Bob Mottram committed
                        # shellcheck disable=SC2086
                        mv $restore_from_dir /var/www/${restore_app_domain}/
Bob Mottram's avatar
Bob Mottram committed
                        cp -r "$restore_from_dir/"* "/var/www/${restore_app_domain}/htdocs/"
Bob Mottram's avatar
Bob Mottram committed
                    # shellcheck disable=SC2181
                    if [ ! "$?" = "0" ]; then
                        set_user_permissions
                        backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
                    if [ -d "/etc/letsencrypt/live/${restore_app_domain}" ]; then
                        ln -s "/etc/letsencrypt/live/${restore_app_domain}/privkey.pem" "/etc/ssl/private/${restore_app_domain}.key"
                        ln -s "/etc/letsencrypt/live/${restore_app_domain}/fullchain.pem" "/etc/ssl/certs/${restore_app_domain}.pem"
                    else
                        # Ensure that the bundled SSL cert is being used
Bob Mottram's avatar
Bob Mottram committed
                        if [ -f "/etc/ssl/certs/${restore_app_domain}.bundle.crt" ]; then
                            sed -i "s|${restore_app_domain}.crt|${restore_app_domain}.bundle.crt|g" "/etc/nginx/sites-available/${restore_app_domain}"
    else
        echo $"No database backup found for ${restore_app_name}"
Bob Mottram's avatar
Bob Mottram committed
        set_user_permissions
        backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
function valid_backup_destination {
    # used to check whether any additional backup directories clash with
    # exiting apps
    destination_dir="$1"
    is_valid="yes"

    available_variants_list=()
    available_system_variants

Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2068
    if ! item_in_array "${destination_dir}" ${available_variants_list[@]}; then
Bob Mottram's avatar
Bob Mottram committed
        is_valid="no"
    fi

    echo $is_valid
}

function backup_extra_directories {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$BACKUP_EXTRA_DIRECTORIES" ]; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi

    backup_type="$1"

    echo $"Backing up some additional directories"
Bob Mottram's avatar
Bob Mottram committed
    while read -r backup_line
Bob Mottram's avatar
Bob Mottram committed
    do
        backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
        if [ -d "$backup_dir" ]; then
            destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
            if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
                if [[ $backup_type == "local" ]]; then
                    backup_directory_to_usb "$backup_dir" "$destination_dir"
                fi
            else
                echo $"WARNING: The backup directory $destination_dir is already used."
                echo $"Choose a different destination name for backing up $backup_dir"
            fi
        else
            echo $"WARNING: Directory $backup_dir does not exist"
        fi
Bob Mottram's avatar
Bob Mottram committed
    done <"$BACKUP_EXTRA_DIRECTORIES"
function store_drives_baseline {
    if [ -f $DRIVES_BASELINE_FILE ]; then
        return
    fi
    # shellcheck disable=SC2012
    ls -gA /dev/sd* | awk -F '/dev/' '{print $2}' > $DRIVES_BASELINE_FILE
}

function detect_connected_drives {
    if [ ! -f $DRIVES_BASELINE_FILE ]; then
        # shellcheck disable=SC2012
        ls -gA /dev/sd* | awk -F '/dev/' '{print $2}' > $DRIVES_BASELINE_FILE
        return
    fi
    # shellcheck disable=SC2012
    ls -gA /dev/sd* | awk -F '/dev/' '{print $2}' > /tmp/.drives_current

    diff /tmp/.drives_current ${DRIVES_BASELINE_FILE} | awk -F '< ' '{print $2}'  | sed '/^$/d'  | sed 's/[0-9]*//g' | uniq | head -n 1
}

Bob Mottram's avatar
Bob Mottram committed
# NOTE: deliberately no exit 0