#!/usr/bin/env bash # Restore your Sendy database. set -e set -o pipefail backup_path="/var/lib/sendy-backup" file_name="${1}" restore_path="${backup_path}/${file_name}" sendy_config="/var/www/html/includes/config.php" sendy_db_user="$(grep -o "dbUser = '[^']*[^']'" "${sendy_config}" | cut -d "=" -f 2 | sed "s/'//g;s/ //g")" sendy_db_password="$(grep -o "dbPass = '[^']*[^']'" "${sendy_config}" | cut -d "=" -f 2 | sed "s/'//g;s/ //g")" sendy_db_name="$(grep -o "dbName = '[^']*[^']'" "${sendy_config}" | cut -d "=" -f 2 | sed "s/'//g;s/ //g")" if [ -z "${file_name}" ]; then echo "You must supply a file name, example: ${0} $(date +%F)_sendy.sql.gz" exit 1 fi if [ ! -f "${restore_path}" ]; then echo "'${restore_path}' not found" exit 1 fi read -p "Restoring is going to wipe your current database, are you sure (y/n)? " -n 1 -r echo if [[ ! "${REPLY}" =~ ^[yY]$ ]]; then echo "The '${sendy_db_name}' database was not restored because you didn't type y or Y." exit 1 fi # MySQL / MariaDB. zcat "${restore_path}" | mysql -u "${sendy_db_user}" -p"${sendy_db_password}" "${sendy_db_name}" # PostgreSQL (not using Sendy's values since it's just an example). # # Repeat either option 1 or option 2 from the backup script. # psql -U foouser -h localhost -d mydatabase < mydatabase.sql.dump echo "The '${sendy_db_name}' database was successfully restored from '${file_name}'"