From 000f49c33c736f56bf5b9a0d6aedca74e0ae72dc Mon Sep 17 00:00:00 2001 From: David Runge Date: Mon, 26 Oct 2015 22:37:11 +0100 Subject: bin/crypted-backups: Distinguishing between checking for folder's existence and its additional creation upon non-existence. Adding message on empty subfolders when doing recursive backup. Adding check for configuration file. --- bin/crypted-backups | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'bin/crypted-backups') diff --git a/bin/crypted-backups b/bin/crypted-backups index e2dc887..5de0b7c 100755 --- a/bin/crypted-backups +++ b/bin/crypted-backups @@ -2,7 +2,12 @@ set -euo pipefail IFS=$'\n\t' -. /etc/crypted-backups +if [ -r /etc/crypted-backups ]; then + . /etc/crypted-backups +else + echo "Configuration file (/etc/crypted-backups) not readable! Aborting!" + exit 1 +fi user_mode="" source_mode="" verbose='' @@ -73,6 +78,16 @@ function check_database_settings () { } function check_directory_exists () { + local destination=$1 + if [ ! -d $destination ]; then + echo "Directory \"$destination\" does not exist!" + return 1 + else + return 0 + fi +} + +function check_directory_exists_and_autocreate () { local destination=$1 if [ ! -d $destination ]; then echo "Directory \"$destination\" does not exist yet. Creating..." @@ -111,14 +126,15 @@ function check_directory_owner_user() { function check_user_directory () { local directory=$1 - check_directory_exists $directory + echo "Checking directory \"$directory\"." + check_directory_exists_and_autocreate $directory check_directory_writable_user $directory return 0 } function check_root_directory () { local directory=$1 - check_directory_exists $directory + check_directory_exists_and_autocreate $directory check_directory_permission_root $directory return 0 } @@ -167,12 +183,21 @@ function compress_to_tmp_file () { function encrypt_tmp_file () { local tmp_file=$1 local destination_file=$2 + local encrypt_return=-1 echo "Encrypting $tmp_file to $destination_file." - #TODO: Failover for still deleting $tmp_file, if encryption fails + set +eu gpg -e \ -r "$gpg_public_key" \ -o "$destination_file" \ "$tmp_file" + encrypt_return=$? + set -eu + if [ $encrypt_return -gt 0 ];then + echo "GnuPG encryption returns with: $encrypt_return" + echo "Removing $tmp_file." + rm -f "$tmp_file" + return 1 + fi echo "Removing $tmp_file." rm -f "$tmp_file" return 0 @@ -190,7 +215,6 @@ function backup_single_directory () { notification_source_to_destination "$source_parent_directory$source_directory_basename" $destination_file echo "Going to $source_directory_basename's parent directory: $source_parent_directory." cd $source_parent_directory - #TODO: check if directory, not for existence? check_directory_exists $source_directory "check_"$user_mode"_directory" $tmp_directory "check_"$user_mode"_directory" $destination_directory @@ -202,14 +226,17 @@ function backup_multiple_directories () { local source_directory=$(sanitize_pathname $1) local destination_directory=$(sanitize_pathname $2) local layered=0 + local sub_count=0 if [ ${#@} -gt 2 ]; then layered=$3 - echo "Multi-layered" + echo "Recursive backup (depth=1) of \"$source_directory\"." fi + check_directory_exists $source_directory "check_"$user_mode"_directory" $destination_directory for sub_directory in $source_directory* do if [ -d $sub_directory ];then + sub_count=$((sub_count+1)) "check_"$user_mode"_directory" $sub_directory if [ $layered -eq 1 ];then backup_multiple_directories $sub_directory $destination_directory$(get_basename_directory $sub_directory) @@ -218,7 +245,9 @@ function backup_multiple_directories () { fi fi done - + if [ $sub_count -eq 0 ]; then + echo "There are actually no folders to backup in \"$source_directory\". Please check the configuration file!" + fi } function dump_database () { @@ -292,11 +321,9 @@ function print_help () { exit 0 } -#TODO: Add function to delete compressed data in working directory (also after fail) #TODO: Add function to cleanup backups #TODO: Add function to mirror backups #TODO: Add function to automatically add key to keyring, if not found -#TODO: Create logic to distinguish between different backups #TODO: Add verbose flag -- cgit v1.2.3-70-g09d2