From 5f4fef7b1940ff6417242c82d67ca1588e9ffe25 Mon Sep 17 00:00:00 2001 From: David Beniamine <david.beniamine@tetras-libre.fr> Date: Fri, 4 Jun 2021 09:54:17 +0200 Subject: [PATCH] Code refactor + archive_path + logfile + verbosity --- .env.sample | 4 +++ backup.sh | 72 +++++++++++++++++--------------------------- clear_backup.sh | 26 ++-------------- collection_status.sh | 8 +---- inc.sh | 49 +++++++++++++++++++++++++++--- list_files.sh | 8 +---- restore_file.sh | 2 +- 7 files changed, 82 insertions(+), 87 deletions(-) diff --git a/.env.sample b/.env.sample index d5123c5..323ed7b 100644 --- a/.env.sample +++ b/.env.sample @@ -5,6 +5,10 @@ encpass='CHANGEME' bckplist="/root /etc /srv /var/www /usr /lib /opt /var/opt /var/lib/docker/volumes/ /home" remotedir="bckp_`hostname`" +verbosity="info" +# Set an arcihve directory, subdir of /root/.cache/duplicity +archive_dir="" +log_file=/var/log/bckp.log BK_FULL_FREQ="3W" # create a new full backup every... diff --git a/backup.sh b/backup.sh index 5eae355..7433708 100755 --- a/backup.sh +++ b/backup.sh @@ -6,6 +6,7 @@ if [ ! -z "$1" ]; then shift fi source $DIR/inc.sh +exec &> >(tee -a "$log_file") exit_on_fail(){ [ $1 -ne 0 ] && leave $@ @@ -24,50 +25,33 @@ leave $? } backup(){ - - retries=$1 - shift - export PASSPHRASE="$encpass" - export FTP_PASSWORD="$ftppass" - #set -x - - echo "Removing old backups" - /usr/bin/duplicity remove-older-than $BK_FULL_LIFE \ - --force ftp://$user@$host/$remotedir/ - /usr/bin/duplicity remove-all-inc-of-but-n-full $BK_KEEP_FULL \ - --force ftp://$user@$host/$remotedir/ - /usr/bin/duplicity remove-all-but-n-full $BK_KEEP_FULL \ - --force ftp://$user@$host/$remotedir/ - - echo "Running pre_actions" - pre_actions - exit_on_fail $? "Pre_action failed" - echo "Creating encrypted incremental backup: $bckplist " - /usr/bin/duplicity \ - --full-if-older-than $BK_FULL_FREQ \ - --include ${bckplist// / --include } \ - --exclude '**' \ - / ftp://$user@$host/$remotedir/ - ret=$? - if [ -z "$retries" ] - then - retrie_on_fail $ret "Duplicity failed" - else - exit_on_fail $ret "Duplicity failed" - fi - - echo "Backup completed" - - echo "Cleaning" - /usr/bin/duplicity cleanup --force --extra-clean ftp://$user@$host/$remotedir/ - echo "Removing backup cache" - rm -rf /root/.cache/duplicity/ - echo "Backups state" - /usr/bin/duplicity collection-status ftp://$user@$host/$remotedir/ - #set +x - unset PASSPHRASE - unset FTP_PASSWORD - echo "All Done" + retries=$1 + shift + + cleanup + echo "Removing old backups" + delete_old_backups + + echo "Running pre_actions" + pre_actions + exit_on_fail $? "Pre_action failed" + echo "Creating encrypted incremental backup: $bckplist " + run_duplicity --full-if-older-than $BK_FULL_FREQ \ + --include ${bckplist// / --include } \ + --exclude /root/.cache/duplicity/ \ + / + ret=$? + if [ -z "$retries" ] + then + retrie_on_fail $ret "Duplicity failed" + else + exit_on_fail $ret "Duplicity failed" + fi + + echo "Backup completed" + + collection_state + echo "All Done" } backup leave $? diff --git a/clear_backup.sh b/clear_backup.sh index 1204b54..45ec583 100755 --- a/clear_backup.sh +++ b/clear_backup.sh @@ -7,28 +7,8 @@ if [ ! -z "$1" ]; then fi source $DIR/inc.sh -export PASSPHRASE="$encpass" -export FTP_PASSWORD="$ftppass" -#set -x +delete_old_backups -echo "Removing old backups" -/usr/bin/duplicity remove-older-than $BK_FULL_LIFE \ - --force ftp://$user@$host/$remotedir/ -/usr/bin/duplicity remove-all-inc-of-but-n-full $BK_KEEP_FULL \ - --force ftp://$user@$host/$remotedir/ -/usr/bin/duplicity remove-all-but-n-full $BK_KEEP_FULL \ - --force ftp://$user@$host/$remotedir/ - -/usr/bin/duplicity collection-status ftp://$user@$host/$remotedir/ - -echo "Cleaning" -/usr/bin/duplicity cleanup --force --extra-clean ftp://$user@$host/$remotedir/ -echo "Removing backup cache" -rm -rf /root/.cache/duplicity/ -echo "Backups state" -/usr/bin/duplicity collection-status ftp://$user@$host/$remotedir/ -#set +x -unset PASSPHRASE -unset FTP_PASSWORD +cleanup +collection_state echo "All Done" -leave 0 diff --git a/collection_status.sh b/collection_status.sh index 53806c0..f79fd1a 100755 --- a/collection_status.sh +++ b/collection_status.sh @@ -2,10 +2,4 @@ DIR=$(realpath $(dirname $0)) source $DIR/inc.sh -export PASSPHRASE="$encpass" -export FTP_PASSWORD="$ftppass" - -/usr/bin/duplicity collection-status ftp://$user@$host/$remotedir/ - -unset PASSPHRASE -unset FTP_PASSWORD +collection_state diff --git a/inc.sh b/inc.sh index d0ff899..2ad0412 100755 --- a/inc.sh +++ b/inc.sh @@ -1,11 +1,27 @@ #!/bin/bash +if [ `whoami` != "root" ]; then + echo "this script must be run as root" + exit 1 +fi + DIR=$(realpath $(dirname $0)) if [ -z "$conf_file" ]; then conf_file="$DIR/.env" fi source $conf_file +if [ -z "$log_file" ]; then + log_file=/var/log/bckp.log +fi + + +if [ -z "$verbosity" ]; then + verbosity="notice" +fi + +remote="ftp://$user@$host/$remotedir/" + # Exit the script and sendmail # $1 : exit code # $2 : message @@ -32,8 +48,31 @@ EOF exit $1 } -if [ `whoami` != "root" ] -then - echo "this script must be run as root" - exit 1 -fi +run_duplicity(){ + export PASSPHRASE="$encpass" + export FTP_PASSWORD="$ftppass" + set -x + /usr/bin/duplicity --verbosity $verbosity \ + $@ \ + $remote + set +x + unset PASSPHRASE + unset FTP_PASSWORD +} + +cleanup(){ + echo "Cleaning" + run_duplicity cleanup --force +} + +delete_old_backups(){ + echo "Removing old backups" + run_duplicity remove-older-than $BK_FULL_LIFE --force + run_duplicity remove-all-but-n-full $BK_KEEP_FULL --force + collection_state +} + +collection_state(){ + echo "Backups state" + run_duplicity collection-status +} diff --git a/list_files.sh b/list_files.sh index 3d7554d..e504731 100755 --- a/list_files.sh +++ b/list_files.sh @@ -2,10 +2,4 @@ DIR=$(realpath $(dirname $0)) source $DIR/inc.sh -export PASSPHRASE="$encpass" -export FTP_PASSWORD="$ftppass" - -/usr/bin/duplicity list-current-files ftp://$user@$host/$remotedir/ - -unset PASSPHRASE -unset FTP_PASSWORD +run_duplicity list-current-files diff --git a/restore_file.sh b/restore_file.sh index 8b7e9aa..b38f893 100755 --- a/restore_file.sh +++ b/restore_file.sh @@ -36,7 +36,7 @@ source $DIR/inc.sh export PASSPHRASE="$encpass" export FTP_PASSWORD="$ftppass" -/usr/bin/duplicity --file-to-restore $delay "$restore_path" ftp://$user@$host/$remotedir/ "$destination_path" +/usr/bin/duplicity --verbosity $verbosity --file-to-restore $delay "$restore_path" $remote "$destination_path" unset PASSPHRASE unset FTP_PASSWORD -- GitLab