diff options
-rwxr-xr-x | bin/crypted-backups | 85 |
1 files changed, 51 insertions, 34 deletions
diff --git a/bin/crypted-backups b/bin/crypted-backups index 5de0b7c..3019c7e 100755 --- a/bin/crypted-backups +++ b/bin/crypted-backups @@ -10,7 +10,7 @@ else fi user_mode="" source_mode="" -verbose='' +verbose=0 function notification_source_to_destination () { local source_directory=$1 @@ -53,7 +53,16 @@ function sanitize_pathname () { fi } +function check_gpg_set () { + [ $verbose -gt 0 ] && echo "Checking, if \"gpg_public_key\" is set." + if [ -z "$gpg_public_key" ];then + echo "Error. \"gpg_public_key\" not set!" + exit 1 + fi +} + function check_database_server () { + [ $verbose -gt 0 ] && echo "Checking database server." if [ ! -x /usr/bin/mysql ]; then echo "/usr/bin/mysql is not available. Is MariaDB or MySQL actually installed?" return 1 @@ -64,6 +73,7 @@ function check_database_server () { } function check_database_settings () { + [ $verbose -gt 0 ] && echo "Checking database settings." if [ -z "$database_destination" ]; then echo "The \"database_destination\" variable can not be empty." return 1 @@ -78,9 +88,10 @@ function check_database_settings () { } function check_directory_exists () { - local destination=$1 - if [ ! -d $destination ]; then - echo "Directory \"$destination\" does not exist!" + local directory=$1 + [ $verbose -gt 0 ] && echo "Checking if directory exists: $directory" + if [ ! -d $directory ]; then + echo "Directory \"$directory\" does not exist!" return 1 else return 0 @@ -88,36 +99,43 @@ function check_directory_exists () { } function check_directory_exists_and_autocreate () { - local destination=$1 - if [ ! -d $destination ]; then - echo "Directory \"$destination\" does not exist yet. Creating..." - mkdir -p $destination + local directory=$1 + [ $verbose -gt 0 ] && echo "Checking if directory exists and creating it, if it doesn't: $directory" + if [ ! -d $directory ]; then + echo "Directory \"$directory\" does not exist yet. Creating..." + mkdir -p $directory else return 0 fi } function check_directory_permission_root () { - if [ -w $1 ]; then + local directory=$1 + [ $verbose -gt 0 ] && echo "Checking root's permission on directory: $directory" + if [ -w $directory ]; then return 0 else - echo "Directory not writable: $1." + echo "Directory not writable: $directory." return 1 fi } function check_directory_writable_user () { - if [ -w $1 ]; then + local directory=$1 + [ $verbose -gt 0 ] && echo "Checking the user's permission on directory: $directory" + if [ -w $directory ]; then return 0 else - echo "Directory not writable: $1." + echo "Directory not writable: $directory." return 1 fi } function check_directory_owner_user() { - if [ ! -O $1 ]; then - echo "Directory not owned by user $(whoami): $1" + local directory=$1 + [ $verbose -gt 0 ] && echo "Checking ownership of directory ($directory) by user $(whoami)." + if [ ! -O $directory ]; then + echo "Directory not owned by user $(whoami): $directory" return 1 else return 0 @@ -126,7 +144,7 @@ function check_directory_owner_user() { function check_user_directory () { local directory=$1 - echo "Checking directory \"$directory\"." + [ $verbose -gt 0 ] && echo "Checking directory \"$directory\"." check_directory_exists_and_autocreate $directory check_directory_writable_user $directory return 0 @@ -134,6 +152,7 @@ function check_user_directory () { function check_root_directory () { local directory=$1 + [ $verbose -gt 0 ] && echo "Checking directory \"$directory\"." check_directory_exists_and_autocreate $directory check_directory_permission_root $directory return 0 @@ -158,7 +177,7 @@ function get_basename_directory () { function compress_to_tmp_file () { local source_file=$1 local tmp_file=$2 - echo "Compressing source ($source_file) to temporary file ($tmp_file)." + [ $verbose -gt 0 ] && echo "Compressing source ($source_file) to temporary file ($tmp_file)." case $tar_suffix in ".tar.tbz") tar cfj "$tmp_file" $source_file @@ -184,7 +203,7 @@ function encrypt_tmp_file () { local tmp_file=$1 local destination_file=$2 local encrypt_return=-1 - echo "Encrypting $tmp_file to $destination_file." + [ $verbose -gt 0 ] && echo "Encrypting $tmp_file to $destination_file." set +eu gpg -e \ -r "$gpg_public_key" \ @@ -194,11 +213,11 @@ function encrypt_tmp_file () { set -eu if [ $encrypt_return -gt 0 ];then echo "GnuPG encryption returns with: $encrypt_return" - echo "Removing $tmp_file." + [ $verbose -gt 0 ] && echo "Removing $tmp_file." rm -f "$tmp_file" return 1 fi - echo "Removing $tmp_file." + [ $verbose -gt 0 ] && echo "Removing $tmp_file." rm -f "$tmp_file" return 0 } @@ -213,7 +232,7 @@ function backup_single_directory () { local tmp_file="$tmp_directory$timestamp$source_directory_basename$tar_suffix" local destination_file="$destination_directory$timestamp$source_directory_basename$tar_suffix$gpg_suffix" notification_source_to_destination "$source_parent_directory$source_directory_basename" $destination_file - echo "Going to $source_directory_basename's parent directory: $source_parent_directory." + [ $verbose -gt 0 ] && echo "Going to $source_directory_basename's parent directory: $source_parent_directory." cd $source_parent_directory check_directory_exists $source_directory "check_"$user_mode"_directory" $tmp_directory @@ -229,7 +248,7 @@ function backup_multiple_directories () { local sub_count=0 if [ ${#@} -gt 2 ]; then layered=$3 - echo "Recursive backup (depth=1) of \"$source_directory\"." + [ $verbose -gt 0 ] && echo "Recursive backup (depth=1) of \"$source_directory\"." fi check_directory_exists $source_directory "check_"$user_mode"_directory" $destination_directory @@ -253,6 +272,7 @@ function backup_multiple_directories () { function dump_database () { local db=$1 local tmp_file=$2 + [ $verbose -gt 0 ] && echo "Dumping database $db to file $tmp_file." mysqldump --force \ --opt \ -u$database_user \ @@ -261,11 +281,12 @@ function dump_database () { } function backup_all_databases () { - check_database_server - check_database_settings local databases=( ) local destination=$(sanitize_pathname $database_destination) - local database + local database="" + check_database_server + check_database_settings + [ $verbose -gt 0 ] && echo "Backing up all available databases." set +eu databases=$(mysql -u$database_user \ -p$database_password \ @@ -276,7 +297,7 @@ function backup_all_databases () { 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[@]}" + [ $verbose -gt 0 ] && echo "Following databases will be backed up: $( ${databases[@]} )" for database in $databases; do backup_database $database $destination done @@ -293,7 +314,9 @@ function backup_database () { 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)." + [ $verbose -gt 0 ] && echo "Backing up database $db." + [ $verbose -gt 0 ] && echo "Going to temporary directory ($tmp_directory)." + notification_source_to_destination "$db" $destination_file cd $tmp_directory dump_database $db $sql_file compress_to_tmp_file $sql_file $tmp_file @@ -306,16 +329,10 @@ function set_user_mode () { else user_mode="user" fi + [ $verbose -gt 0 ] && echo "user_mode set to $user_mode." return 0 } -function check_gpg_set () { - if [ -z "$gpg_public_key" ];then - echo "Error. \"gpg_public_key\" not set!" - exit 1 - fi -} - function print_help () { echo "help" exit 0 @@ -346,7 +363,7 @@ if [ ${#@} -gt 0 ]; then source_mode="${OPTARG}" ;; v) - verbose='true' + verbose=1 ;; *) echo "Error. Unrecognized option: ${flag}." |