Added dry-run command line option (-n or --dry-run) and many short versions of long options (-b, -f, -v, -l, -s)

This commit is contained in:
Zertrin 2012-08-06 18:17:06 +02:00
parent a6921f391d
commit 293b765a5c
3 changed files with 104 additions and 51 deletions

View file

@ -1,7 +1,9 @@
0.8 (05 August 2012) 0.8 (05 August 2012)
=================== ===================
* Improved argument parsing. Now the script is insensitive to the order of appearance of the options and handles correctly optional options parameters * Improved argument parsing. Now the script is insensitive to the order of appearance of the options and handles correctly optional options parameters
* Re-added possibility to specify the config file on the command line, which is now th recommanded way to do (facilitating script updates) * Re-added possibility to specify the config file on the command line, which is now the recommended way to do (facilitating script updates)
* Added dry-run command line option (-n or --dry-run)
* Added many short versions of long options (-b, -f, -v, -l, -s)
* Fix "unary operator expected warning" in some rare cases * Fix "unary operator expected warning" in some rare cases
0.7.1 (9 July 2012) 0.7.1 (9 July 2012)

67
README
View file

@ -2,13 +2,15 @@ ABOUT THIS SCRIPT
================= =================
This bash script was designed to automate and simplify the remote backup process This bash script was designed to automate and simplify the remote backup process
of duplicity on Amazon S3 primarily. Other backup destinations are possible. of duplicity on Amazon S3 primarily. Other backup destinations are possible
(FTP, SFTP, SCP, rsync, file...), i.e. any of duplicity's supported outputs.
After your script is configured, you can easily backup, restore, verify and After your script is configured, you can easily backup, restore, verify and
clean (either via cron or manually) your data without having to remember lots of clean (either via cron or manually) your data without having to remember lots of
different command options and passphrases. different command options and passphrases.
Most importantly, you can easily backup the script and your gpg key in a Most importantly, you can easily backup the script and your gpg key in a
convenient passphrase-encrypted file. This comes in in handy if/when your convenient passphrase-encrypted file. This comes in in handy if/when your
machine ever does go belly up. machine ever does go belly up.
Optionally, you can set up an email address where the log file will be sent, Optionally, you can set up an email address where the log file will be sent,
@ -38,10 +40,10 @@ file, but make a copy of it and edit this one. Be sure to replace all the
*foobar* values with your real ones. Almost every value needs to be configured *foobar* values with your real ones. Almost every value needs to be configured
in someway. in someway.
You can use multiple copies of the script with different settings for different You can use one copy of the script with different settings for different backup
backup scenarios. It is designed to run as a cron job and will log information scenarios. It is designed to run as a cron job and will log information to a
to a text file (including remote file sizes, if you have s3cmd installed). Be text file (including remote file sizes, if you have s3cmd installed). Be sure to
sure to make the script executable (chmod +x) before you hit the gas. make the script executable (chmod +x) before you hit the gas.
REQUIREMENTS REQUIREMENTS
============ ============
@ -57,16 +59,47 @@ CONFIGURATION
The configuration takes place in a separate config file and is documented there. The configuration takes place in a separate config file and is documented there.
You want to copy duplicity-backup.conf.example to another place that suits your
needs (for example /etc/duplicity-backup.conf)
The script looks for its configuration by reading the config file specified by The script looks for its configuration by reading the config file specified by
the command line option -c and fallback on the CONFIG parameter at the beggining the command line option -c or --config (see USAGE)
of the script otherwise.
Be sure to specify it on the command line or to edit the CONFIG parameter in the If no config file was given on the command line, the script will try to find the
script to match the actual location of your config file. file specified in the CONFIG parameter at the beggining of the script (defaults
to "duplicity-backup.conf" in the script's directory).
NOTE: to ease future updates of the script, you may want not to edit the script So be sure either:
at all and to specify systematically the path to your config file on the - to specify the configuration file path on the command line [recommended]
command line with the -c option. - or to edit the CONFIG parameter in thescript to match the actual location of
your config file. [deprecated]
NOTE: to ease future updates of the script, you may prefer NOT to edit the
script at all and to specify systematically the path to your config file
on the command line with the -c or --config option.
USAGE
=====
duplicity-backup.sh [options]
Options:
-c, --config CONFIG_FILE specify the config file to use
-b, --backup runs an incremental backup
-f, --full forces a full backup
-v, --verify verifies the backup
--restore [PATH] restores the entire backup to [path]
--restore-file [FILE_TO_RESTORE] [DESTINATION]
restore a specific file
-l, --list-current-files lists the files currently backed up in the archive
-s, --collection-status show all the backup sets in the archive
--backup-script automatically backup the script and secret key to
the current working directory
-n, --dry-run perform a trial run with no changes made
COMMON USAGE EXAMPLES COMMON USAGE EXAMPLES
===================== =====================
@ -114,9 +147,11 @@ are having any problems with the script the first step is to determine if the
script is generating an incorrect command or if duplicity itself is causing your script is generating an incorrect command or if duplicity itself is causing your
error. error.
To see exactly what is happening when you run duplicity-backup, head to the bottom To see exactly what is happening when you run duplicity-backup, either pass the
of the configuration file and uncomment the `ECHO=$(which echo)` variable. This option -n or --dry-run on the command line, or head to the bottom of the
will stop the script from running and will, instead, output the generated configuration file and uncomment the `ECHO=$(which echo)` variable.
This will stop the script from running and will, instead, output the generated
command into your log file. You can then check to see if what is being generated command into your log file. You can then check to see if what is being generated
is causing an error or if it is duplicity causing you woe. is causing an error or if it is duplicity causing you woe.

View file

@ -38,18 +38,44 @@ CONFIG="duplicity-backup.conf"
# Script Happens Below This Line - Shouldn't Require Editing # # Script Happens Below This Line - Shouldn't Require Editing #
############################################################## ##############################################################
usage(){
echo "USAGE:
`basename $0` [options]
Options:
-c, --config CONFIG_FILE specify the config file to use
-b, --backup runs an incremental backup
-f, --full forces a full backup
-v, --verify verifies the backup
--restore [PATH] restores the entire backup to [path]
--restore-file [FILE_TO_RESTORE] [DESTINATION]
restore a specific file
-l, --list-current-files lists the files currently backed up in the archive
-s, --collection-status show all the backup sets in the archive
--backup-script automatically backup the script and secret key to
the current working directory
-n, --dry-run perform a trial run with no changes made
CURRENT SCRIPT VARIABLES:
========================
DEST (backup destination) = ${DEST}
INCLIST (directories included) = ${INCLIST[@]:0}
EXCLIST (directories excluded) = ${EXCLIST[@]:0}
ROOT (root directory of backup) = ${ROOT}
LOGFILE (log file path) = ${LOGFILE}
"
}
# Some expensive argument parsing that allows the script to # Some expensive argument parsing that allows the script to
# be insensitive to the order of appearance of the options # be insensitive to the order of appearance of the options
# and to handle correctly option parameters that are optional # and to handle correctly option parameters that are optional
while getopts ":c:-:" opt; do while getopts ":c:bfvlsn-:" opt; do
case $opt in case $opt in
# parse long options (a bit tricky because builtin getopts does not # parse long options (a bit tricky because builtin getopts does not
# manage long options and i don't want to impose GNU getopt dependancy) # manage long options and i don't want to impose GNU getopt dependancy)
-) -)
case "$OPTARG" in case "$OPTARG" in
backup | full | verify | list-current-files | collection-status | help)
COMMAND=$OPTARG
;;
# --restore [restore dest] # --restore [restore dest]
restore) restore)
COMMAND=$OPTARG COMMAND=$OPTARG
@ -75,16 +101,29 @@ while getopts ":c:-:" opt; do
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing
fi fi
;; ;;
config) # set the config file from the command line
# We try to find the config file
if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
CONFIG=${!OPTIND}
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing
fi
;;
dry-run)
ECHO=$(which echo)
;;
*) *)
echo "Invalid option: --$OPTARG" >&2
COMMAND=$OPTARG COMMAND=$OPTARG
;; ;;
esac esac
;; ;;
# here are parsed the short options # here are parsed the short options
c) # set the config file from the command line c) CONFIG=$OPTARG;; # set the config file from the command line
CONFIG=$OPTARG b) COMMAND="backup";;
;; f) COMMAND="full";;
v) COMMAND="verify";;
l) COMMAND="list-current-files";;
s) COMMAND="collection-status";;
n) ECHO=$(which echo);; # dry run
:) :)
echo "Option -$OPTARG requires an argument." >&2 echo "Option -$OPTARG requires an argument." >&2
COMMAND="" COMMAND=""
@ -102,6 +141,7 @@ then
. $CONFIG . $CONFIG
else else
echo "ERROR: can't find config file! (${CONFIG})" >&2 echo "ERROR: can't find config file! (${CONFIG})" >&2
usage
exit 1 exit 1
fi fi
@ -133,7 +173,7 @@ NO_S3CMD_CFG="WARNING: s3cmd is not configured, run 's3cmd --configure' \
in order to retrieve remote file size information. Remote file \ in order to retrieve remote file size information. Remote file \
size information unavailable." size information unavailable."
README_TXT="In case you've long forgotten, this is a backup script that you used to backup some files (most likely remotely at Amazon S3). In order to restore these files, you first need to import your GPG private key (if you haven't already). The key is in this directory and the following command should do the trick:\n\ngpg --allow-secret-key-import --import duplicity-backup-secret.key.txt\n\nAfter your key as been succesfully imported, you should be able to restore your files.\n\nGood luck!" README_TXT="In case you've long forgotten, this is a backup script that you used to backup some files (most likely remotely at Amazon S3). In order to restore these files, you first need to import your GPG private key (if you haven't already). The key is in this directory and the following command should do the trick:\n\ngpg --allow-secret-key-import --import duplicity-backup-secret.key.txt\n\nAfter your key as been succesfully imported, you should be able to restore your files.\n\nGood luck!"
CONFIG_VAR_MSG="Oops!! ${0} was unable to run!\nWe are missing one or more important variables at the top of the script.\nCheck your configuration because it appears that something has not been set yet." CONFIG_VAR_MSG="Oops!! ${0} was unable to run!\nWe are missing one or more important variables in the configuration file.\nCheck your configuration because it appears that something has not been set yet."
if [ ! -x "$DUPLICITY" ]; then if [ ! -x "$DUPLICITY" ]; then
echo "ERROR: duplicity not installed, that's gotta happen first!" >&2 echo "ERROR: duplicity not installed, that's gotta happen first!" >&2
@ -504,31 +544,7 @@ case "$COMMAND" in
*) *)
echo -e "[Only show `basename $0` usage options]\n" >> ${LOGFILE} echo -e "[Only show `basename $0` usage options]\n" >> ${LOGFILE}
echo " USAGE: usage
`basename $0` [options]
Options:
-c config_file : specify the config file to use
--backup: runs an incremental backup
--full: forces a full backup
--verify: verifies the backup
--restore [path]: restores the entire backup
--restore-file [file] [destination/filename]: restore a specific file
--list-current-files: lists the files currently backed up in the archive
--collection-status: show all the backup sets in the archive
--backup-script: automatically backup the script and secret key to the current working directory
CURRENT SCRIPT VARIABLES:
========================
DEST (backup destination) = ${DEST}
INCLIST (directories included) = ${INCLIST[@]:0}
EXCLIST (directories excluded) = ${EXCLIST[@]:0}
ROOT (root directory of backup) = ${ROOT}
LOGFILE (log file path) = ${LOGFILE}
"
;; ;;
esac esac