From e137a9974e55221c619f2cb4804120ee1efbb15d Mon Sep 17 00:00:00 2001 From: David Runge Date: Mon, 26 Oct 2015 21:07:10 +0100 Subject: bin/crypted-backups: Adding function for database backups. Generalizing the compression function (compress_to_tmp_file). Making the script even more strict. --- bin/crypted-backups | 102 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 15 deletions(-) (limited to 'bin') diff --git a/bin/crypted-backups b/bin/crypted-backups index 429d3db..e2dc887 100755 --- a/bin/crypted-backups +++ b/bin/crypted-backups @@ -1,15 +1,12 @@ #!/usr/bin/env bash -set -e -u +set -euo pipefail +IFS=$'\n\t' . /etc/crypted-backups user_mode="" source_mode="" verbose='' -test_source="/tmp/test" -test_destination="/home/dave/Downloads/test" - - function notification_source_to_destination () { local source_directory=$1 local destination_directory=$2 @@ -51,6 +48,30 @@ function sanitize_pathname () { fi } +function check_database_server () { + if [ ! -x /usr/bin/mysql ]; then + echo "/usr/bin/mysql is not available. Is MariaDB or MySQL actually installed?" + return 1 + elif [ !$(systemctl is-active mysqld) = "active" ]; then + echo "No MariaDB or MySQL service is currently running. Start it with 'systemctl start mysqld'." + return 1 + fi +} + +function check_database_settings () { + if [ -z "$database_destination" ]; then + echo "The \"database_destination\" variable can not be empty." + return 1 + elif [ -z "$database_user" ]; then + echo "The \"database_user\" variable can not be empty." + return 1 + elif [ -z "$database_password" ]; then + echo "The \"database_password\" variable can not be empty." + return 1 + fi + return 0 +} + function check_directory_exists () { local destination=$1 if [ ! -d $destination ]; then @@ -118,22 +139,22 @@ function get_basename_directory () { return 0 } -function compress_directory () { - local source_directory=$1 +function compress_to_tmp_file () { + local source_file=$1 local tmp_file=$2 - echo "Compressing source directory ($source_directory) to temporary file ($tmp_file)." + echo "Compressing source ($source_file) to temporary file ($tmp_file)." case $tar_suffix in ".tar.tbz") - tar cfj "$tmp_file" $source_directory + tar cfj "$tmp_file" $source_file ;; ".tar.tgz") - tar cfz "$tmp_file" $source_directory + tar cfz "$tmp_file" $source_file ;; ".tar.tlz") - tar --lzma -cf "$tmp_file" $source_directory + tar --lzma -cf "$tmp_file" $source_file ;; ".tar.xz") - tar cfJ "$tmp_file" $source_directory + tar cfJ "$tmp_file" $source_file ;; *) echo "Using \"$tar_suffix\" as \$tar_suffix is not supported." @@ -147,6 +168,7 @@ function encrypt_tmp_file () { local tmp_file=$1 local destination_file=$2 echo "Encrypting $tmp_file to $destination_file." + #TODO: Failover for still deleting $tmp_file, if encryption fails gpg -e \ -r "$gpg_public_key" \ -o "$destination_file" \ @@ -172,7 +194,7 @@ function backup_single_directory () { check_directory_exists $source_directory "check_"$user_mode"_directory" $tmp_directory "check_"$user_mode"_directory" $destination_directory - compress_directory $source_directory_basename $tmp_file + compress_to_tmp_file $source_directory_basename $tmp_file encrypt_tmp_file $tmp_file $destination_file } @@ -199,6 +221,56 @@ function backup_multiple_directories () { } +function dump_database () { + local db=$1 + local tmp_file=$2 + mysqldump --force \ + --opt \ + -u$database_user \ + -p$database_password \ + --databases $db > $tmp_file +} + +function backup_all_databases () { + check_database_server + check_database_settings + local databases=( ) + local destination=$(sanitize_pathname $database_destination) + local database + set +eu + databases=$(mysql -u$database_user \ + -p$database_password \ + -e "SHOW DATABASES;" \ + | grep -Ev "(Database|information_schema|performance_schema|tmp)") + set -eu + if [ ${#databases} -eq 0 ];then + echo "There are actually no databases on this server. If you've set wrong user or password variables MariaDB/ MySQL will by now have complained about it." + return 1 + else + echo "Databases for which backups will be created: ${databases[@]}" + for database in $databases; do + backup_database $database $destination + done + fi +} + +function backup_database () { + local db=$1 + local destination_directory=$2 + local timestamp=$(generate_timestamp) + local tmp_directory=$(sanitize_pathname $tmp) + local sql_file="$timestamp$db$sql_suffix" + local tmp_file="$tmp_directory$sql_file$tar_suffix" + local destination_file="$destination_directory$timestamp$db$sql_suffix$tar_suffix$gpg_suffix" + "check_"$user_mode"_directory" $tmp_directory + "check_"$user_mode"_directory" $destination_directory + echo "Going to temporary directory ($tmp_directory)." + cd $tmp_directory + dump_database $db $sql_file + compress_to_tmp_file $sql_file $tmp_file + encrypt_tmp_file $tmp_file $destination_file +} + function set_user_mode () { if [ $(id -u) -eq 0 ]; then user_mode="root" @@ -221,7 +293,6 @@ function print_help () { } #TODO: Add function to delete compressed data in working directory (also after fail) -#TODO: Add function for database backups #TODO: Add function to cleanup backups #TODO: Add function to mirror backups #TODO: Add function to automatically add key to keyring, if not found @@ -287,7 +358,8 @@ if [ -n "$source_mode" ];then mailman) backup_single_directory $mailman_source $mailman_destination ;; - mariadb) + databases) + backup_all_databases ;; logs) backup_single_directory $logs_source $logs_destination -- cgit v1.2.3-70-g09d2