diff --git a/.env.sample b/.env.sample index d5123c53de0a377807dad42b111fbb5aa4079494..323ed7b255fd448c2b73baf64c7746f2a33c1450 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 5eae3551c941da5f6c5f7dd0301e0b7693141dec..74337089d66e9a49dbdd7f6ec6aca2c0d7fd84bd 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 1204b5480f26e123412948b43d5926d2f0326542..45ec58369b39376258c3524ccd9eaf89d8163e02 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 53806c03759b7d624b3013163198e67f0f512d13..f79fd1abe3ce2033015028932116043717641db8 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 d0ff899d4d98319527a207b1b778dd2c80995641..2ad0412dd65a8bd4e415b6c3ddc0aa1981ecbddb 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 3d7554dce5e7d7c7d9851b013bdf96e01183f612..e504731a2e2a7a25d1a5a9d65c5b3058bcb1311a 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 8b7e9aafd326d18561bc9ce04899e4cd4abec9cb..b38f893c1d5ef087107692e3feefe198fb01a807 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