Created
October 9, 2012 19:36
-
-
Save draptik/3860949 to your computer and use it in GitHub Desktop.
Backup script using rdiff-backup, duplicity and rsync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Setup: | |
# | |
# +--------+ +-----+ | |
# | MyData | | HDD | | |
# | | (1) rdiff-backup | | | |
# | | ------------------> | | | |
# | | | | +-----+ | |
# | | (2) duplicity | | (3) rsync | USB | | |
# | | ------------------> |---->| ----------> | | | |
# | | | | | | | |
# +--------+ +-----+ +-----+ | |
# | |
# (1) rdiff-backup | |
# | |
# + deltas are against current version (not oldest version) | |
# + files are not encrypted | |
# + files are in original format | |
# | |
# (2) duplicity | |
# | |
# + deltas are against oldest version (not newest version) | |
# + files are encrypted | |
# + files are split into small chunks | |
# | |
# (1) and (2) can be run as cron jobs | |
# | |
# (3) must be run manually when the external drive is present | |
# | |
# | |
# rdiff-backup: http://www.nongnu.org/rdiff-backup/ | |
# duplicity: http://duplicity.nongnu.org/ | |
# | |
# | |
# Usage | |
# ----- | |
# | |
# backup_virtualbox_partitions [rdiff|duplicity_full|duplicity_incr|rsync] | |
# | |
## =================================================================== | |
## Password file for symmetric gpg encryption | |
## =================================================================== | |
pw_file="${HOME}/.duplicity_pw" | |
export PASSPHRASE=`cat ${pw_file}` | |
## =================================================================== | |
## Commands | |
## =================================================================== | |
rdiffbackup=`which rdiff-backup` | |
duplicity=`which duplicity` | |
rsync=`which rsync` | |
## =================================================================== | |
## Locations | |
## =================================================================== | |
log_dir="/var/log/mybackup" | |
log=${log_dir}"/backup.log" | |
file_list="$(dirname $0)/rdiff_include_list" | |
src_dir="/virtualbox/" | |
hdd_backup_dir="${HOME}/vm_backups" | |
backup_dir="${hdd_backup_dir}/raw" | |
encrypted_dir="${hdd_backup_dir}/duplicity" | |
usb_dir="/run/media/$USER/My Passport/vm_backups/duplicity" | |
## =================================================================== | |
## Functions | |
## =================================================================== | |
function usage () { | |
echo " | |
Usage | |
----- | |
$0 [rdiff|duplicity_full|duplicity_incr|rsync] | |
" | |
} | |
function ts () { echo $(date +"%Y-%m-%d_%T"); } | |
function myecho () { echo -e $(ts) $1 | tee -a ${log}; } | |
function check_file_list_presence () { | |
if [ -f "${file_list}" ]; then | |
myecho "INFO: Found file ${file_list}"; | |
else | |
myecho "ERROR: Can't find file ${file_list}"; | |
exit 1 | |
fi | |
} | |
function rdiff_mydata () { | |
$(check_file_list_presence); | |
${rdiffbackup} \ | |
--include-globbing-filelist ${file_list} \ | |
${src_dir} "${backup_dir}" 2>&1 | tee -a ${log}; | |
} | |
function duplicity_full_backup () { | |
$(check_file_list_presence); | |
# Requires PASSPHRASE... | |
${duplicity} full \ | |
--gpg-options "--batch --symmetric" \ | |
--include-globbing-filelist ${file_list} \ | |
${src_dir} file://"${encrypted_dir}" 2>&1 | tee -a ${log}; | |
} | |
function duplicity_incremental_backup () { | |
$(check_file_list_presence); | |
${duplicity} incremental --include-globbing-filelist ${file_list} \ | |
${src_dir} file://"${encrypted_dir}" 2>&1 | tee -a ${log}; | |
} | |
function sync2usb () { | |
${rsync} -azvr --delete ${encrypted_dir} "${usb_dir}" 2>&1 | tee -a ${log}; | |
} | |
# function recover_from_rdiff_backup () { | |
# ## TODO | |
# } | |
# function recover_from_duplicity_backup_hdd () { | |
# ## TODO | |
# } | |
# function recover_from_duplicity_backup_usb () { | |
# ## TODO | |
# } | |
## =================================================================== | |
## Main program | |
## =================================================================== | |
cmd="" | |
if [ $# != 1 ]; then | |
echo "$(usage)"; | |
exit 1; | |
else | |
if [ $1 == "rdiff" -o $1 == "duplicity_incr" -o $1 == "duplicity_full" -o $1 == "rsync" ]; then | |
cmd=$1 | |
else | |
echo "$(usage)" | |
exit 1; | |
fi | |
fi | |
myecho "INFO: Program called with argument: $cmd" | |
myecho "\nSTART ============================================" | |
myecho "INFO: Current working dir is $(dirname $0)" | |
if [ $cmd == "rdiff" ]; then | |
myecho "INFO: Starting Backup (rdiff) to\t${backup_dir}" | |
$(rdiff_mydata) & | |
wait | |
myecho "INFO: Finished rdiff-backup." | |
elif [ $cmd == "duplicity_full" ]; then | |
myecho "INFO: Starting Backup (duplicity full) to\t${encrypted_dir}" | |
$(duplicity_full_backup) & | |
wait | |
myecho "INFO: Finished duplicity_full_backup." | |
elif [ $cmd == "duplicity_incr" ]; then | |
myecho "INFO: Starting Backup (duplicity incr) to\t${encrypted_dir}" | |
$(duplicity_incremental_backup) & | |
wait | |
myecho "INFO: Finished duplicity_incr_backup." | |
elif [ $cmd == "rsync" ]; then | |
myecho "INFO: Checking if usb is mounted ..." | |
if [ -d "${usb_dir}" ]; then | |
myecho "INFO: Found usb device ${usb_dir}." | |
myecho "INFO: Starting rsync to usb ..." | |
$(sync2usb) & | |
wait | |
myecho "INFO: Finished rsync." | |
else | |
myecho "WARN: Usb device not found! (${usb_dir}).\nAborting ..." | |
fi | |
fi | |
myecho " END ============================================" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment