a6921f391d038215c3270b2a166a215c41db1347

Author
Zertrin <zertrin@gmail.com>
Committer
Zertrin <zertrin@gmail.com>
Date

Message

Intelligent argument parsing added and possibility to specify the config file on the command line re-added.

Diff

This diff is truncated to protect this page.

  1diff --git a/CHANGELOG b/CHANGELOG
  2index 545e9651114e8835a6183c9c057b810301a717e0..847a3286e1ff3d976319962c1a26753112c6ce9f 100644
  3--- a/CHANGELOG
  4+++ b/CHANGELOG
  5@@ -1,3 +1,9 @@
  6+0.8 (05 August 2012)
  7+===================
  8+  * Improved argument parsing. Now the script is insensitive to the order of appearance of the options and handles correctly optional options parameters
  9+  * Re-added possibility to specify the config file on the command line, which is now th recommanded way to do (facilitating script updates)
 10+  * Fix "unary operator expected warning" in some rare cases
 11+
 12 0.7.1 (9 July 2012)
 13 ===================
 14   * Adding 'duplicity-backup.conf' to .gitignore
 15diff --git a/README b/README
 16index 5199f148dc96e874dc8f62e6815c20a0e8b1c977..25eb7ea1a8c0041b9679d8f5b14e027162c9c9ae 100644
 17--- a/README
 18+++ b/README
 19@@ -57,9 +57,16 @@ CONFIGURATION
 20 
 21 The configuration takes place in a separate config file and is documented there.
 22 
 23-The script looks for its configuration by reading the config file specified in
 24-the CONFIG parameter at the beggining of the script. Be sure to edit it to match
 25-the actual location of your config file.
 26+The script looks for its configuration by reading the config file specified by
 27+the command line option -c and fallback on the CONFIG parameter at the beggining
 28+of the script otherwise. 
 29+
 30+Be sure to specify it on the command line or to edit the CONFIG parameter in the
 31+script to match the actual location of your config file.
 32+
 33+NOTE: to ease future updates of the script, you may want not to edit the script
 34+      at all and to specify systematically the path to your config file on the 
 35+      command line with the -c option.
 36 
 37 COMMON USAGE EXAMPLES
 38 =====================
 39@@ -68,36 +75,36 @@ COMMON USAGE EXAMPLES
 40     $ duplicity-backup.sh
 41 
 42 * Run an incremental backup:
 43-    $ duplicity-backup.sh --backup
 44+    $ duplicity-backup.sh [-c config_file] --backup
 45 
 46 * Force a one-off full backup:
 47-    $ duplicity-backup.sh --full
 48+    $ duplicity-backup.sh [-c config_file] --full
 49 
 50 * Restore your entire backup:
 51-    $ duplicity-backup.sh --restore
 52+    $ duplicity-backup.sh [-c config_file] --restore
 53     You will be prompted for a restore directory
 54 
 55-    $ duplicity-backup.sh --restore /home/user/restore-folder
 56+    $ duplicity-backup.sh [-c config_file] --restore /home/user/restore-folder
 57     You can also provide a restore folder on the command line.
 58 
 59 * Restore a specific file in the backup:
 60-    $ duplicity-backup.sh --restore-file
 61+    $ duplicity-backup.sh [-c config_file] --restore-file
 62     You will be prompted for a file to restore to the current directory
 63 
 64-    $ duplicity-backup.sh --restore-file img/mom.jpg
 65+    $ duplicity-backup.sh [-c config_file] --restore-file img/mom.jpg
 66     Restores the file img/mom.jpg to the current directory
 67 
 68-    $ duplicity-backup.sh --restore-file img/mom.jpg /home/user/i-love-mom.jpg
 69+    $ duplicity-backup.sh [-c config_file] --restore-file img/mom.jpg /home/user/i-love-mom.jpg
 70     Restores the file img/mom.jpg to /home/user/i-love-mom.jpg
 71 
 72 * List files in the remote archive
 73-    $ duplicity-backup.sh --list-current-files
 74+    $ duplicity-backup.sh [-c config_file] --list-current-files
 75 
 76 * Verify the backup
 77-    $ duplicity-backup.sh --verify
 78+    $ duplicity-backup.sh [-c config_file] --verify
 79 
 80 * Backup the script and gpg key (for safekeeping)
 81-    $ duplicity-backup.sh --backup-script
 82+    $ duplicity-backup.sh [-c config_file] --backup-script
 83 
 84 TROUBLESHOOTING
 85 ===============
 86diff --git a/duplicity-backup.sh b/duplicity-backup.sh
 87index c8c957eaf5ab3bbdbb92f7664a57a2efeebe7d0b..ec45ab1fd4b3cfeb0a015f0a5de9ed09074cf228 100755
 88--- a/duplicity-backup.sh
 89+++ b/duplicity-backup.sh
 90@@ -27,14 +27,75 @@
 91 #
 92 # ---------------------------------------------------------------------------- #
 93 
 94-# Set config file (don't forget to copy duplicity-backup.conf.example to
 95-# match that path) 
 96+# Default config file (don't forget to copy duplicity-backup.conf.example to
 97+# match that path)
 98+# NOTE: It can be useful not to edit this script at all to ease future updates
 99+#       so the config file can be specified directly on the command line too 
100+#       with the -c option.
101 CONFIG="duplicity-backup.conf"
102 
103 ##############################################################
104 # Script Happens Below This Line - Shouldn't Require Editing #
105 ##############################################################
106 
107+# Some expensive argument parsing that allows the script to
108+# be insensitive to the order of appearance of the options
109+# and to handle correctly option parameters that are optional
110+while getopts ":c:-:" opt; do
111+  case $opt in
112+    # parse long options (a bit tricky because builtin getopts does not
113+    # manage long options and i don't want to impose GNU getopt dependancy)
114+    -)
115+      case "$OPTARG" in
116+        backup | full | verify | list-current-files | collection-status | help)
117+          COMMAND=$OPTARG
118+        ;;
119+        # --restore [restore dest]
120+        restore)
121+          COMMAND=$OPTARG
122+          # We try to find the optional value [restore dest]
123+          if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
124+            RESTORE_DEST=${!OPTIND}
125+            OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing
126+          fi
127+        ;;
128+        # --restore-file [file to restore] [restore dest]
129+        restore-file)
130+          COMMAND=$OPTARG
131+          # We try to find the first optional value [file to restore]
132+          if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
133+            FILE_TO_RESTORE=${!OPTIND}
134+            OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing
135+          else
136+            continue # no value for the restore-file option, skip the rest
137+          fi
138+          # We try to find the second optional value [restore dest]
139+          if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
140+            RESTORE_DEST=${!OPTIND}
141+            OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing
142+          fi
143+        ;;
144+        *)
145+          echo "Invalid option: --$OPTARG" >&2
146+          COMMAND=$OPTARG
147+        ;;
148+        esac
149+    ;;
150+    # here are parsed the short options
151+    c) # set the config file from the command line
152+      CONFIG=$OPTARG
153+    ;;
154+    :)
155+      echo "Option -$OPTARG requires an argument." >&2
156+      COMMAND=""
157+    ;;
158+    \?)
159+      echo "Invalid option: -$OPTARG" >&2
160+      COMMAND=""
161+    ;;
162+  esac
163+done
164+
165 # Read config file if specified
166 if [ ! -z "$CONFIG" -a -f "$CONFIG" ];
167 then
168@@ -322,13 +383,13 @@ echo -e "--------    START DUPLICITY-BACKUP SCRIPT    --------\n" >> ${LOGFILE}
169 
170 get_lock
171 
172-case "$1" in
173-  "--backup-script")
174+case "$COMMAND" in
175+  "backup-script")
176     backup_this_script
177     exit
178   ;;
179 
180-  "--full")
181+  "full")
182     OPTION="full"
183     include_exclude
184     duplicity_backup
185@@ -336,7 +397,7 @@ case "$1" in
186     get_file_sizes
187   ;;
188 
189-  "--verify")