[PATCH] Add namespaces support to rlFile{Backup,Restore}

Jiri Jaburek jjaburek at redhat.com
Tue Oct 23 15:35:30 UTC 2012


This change introduces a new --namespace option for rlFileBackup
and rlFileRestore, allowing the user to perform several backup/restore
operations independently.

Getopt might seem like an overkill, but there's no primitive and safe
way of using multiple long options with arguments without it.
Also while rlFileRestore could get away without it (no --clean),
I believe it's important to keep a certain level of consistency.

Signed-off-by: Jiri Jaburek <jjaburek at redhat.com>
---
 src/infrastructure.sh          | 70 ++++++++++++++++++++++++++++++++++--------
 src/test/infrastructureTest.sh | 32 +++++++++++++++++++
 2 files changed, 89 insertions(+), 13 deletions(-)

diff --git a/src/infrastructure.sh b/src/infrastructure.sh
index a5edd76..73096fa 100644
--- a/src/infrastructure.sh
+++ b/src/infrastructure.sh
@@ -295,7 +295,7 @@ Create a backup of files or directories (recursive). Can be used
 multiple times to add more files to backup. Backing up an already
 backed up file overwrites the original backup.
 
-    rlFileBackup [--clean] file [file...]
+    rlFileBackup [--clean] [--namespace name] file [file...]
 
 =over
 
@@ -305,6 +305,11 @@ If this option is provided (have to be first option of the command),
 then file/dir backuped using this command (provided in next
 options) will be (resursively) removed before we will restore it.
 
+=item --namespace name
+
+Specifies the namespace to use.
+Namespaces can be used to separate backups and their restoration.
+
 =item file
 
 Files and/or directories to be backed up.
@@ -329,9 +334,29 @@ Returns 0 if the backup was successful.
 rlFileBackup() {
     local backup status file path dir failed selinux acl
 
+    local OPTS clean="" namespace=""
+
+    # getopt will cut off first long opt when no short are defined
+    OPTS=$(getopt -o "cn:" -l "clean,namespace:" -- "$@")
+    [ $? -ne 0 ] && return 1
+
+    eval set -- "$OPTS"
+    while true; do
+        case "$1" in
+            '--clean') shift; clean=1 ;;
+            '--namespace') shift; namespace="$1"; shift ;;
+            --) shift; break ;;
+        esac
+    done;
+
+    # check parameter sanity
+    if [ -z "$1" ]; then
+        rlLogError "rlFileBackup: Nothing to backup... supply a file or dir"
+        return 2
+    fi
+
     # check if we have '--clean' option and save items if we have
-    if [ "$1" = '--clean' ]; then
-        shift
+    if [ "$clean" ]; then
         rlLogDebug "rlFileBackup: Adding '$@' to the clean list"
         for file in "$@"; do
             ###rlLogDebug "rlFileBackup: ... '$@'"
@@ -343,18 +368,14 @@ rlFileBackup() {
         done
     fi
 
-    # check parameter sanity
-    if [ -z "$1" ]; then
-        rlLogError "rlFileBackup: Nothing to backup... supply a file or dir"
-        return 2
-    fi
-
     # set the backup dir
     if [ -z "$BEAKERLIB_DIR" ]; then
         rlLogError "rlFileBackup: BEAKERLIB_DIR not set, run rlJournalStart first"
         return 3
     fi
-    backup="$BEAKERLIB_DIR/backup"
+
+    # backup dir to use, append namespace if defined
+    backup="$BEAKERLIB_DIR/backup${namespace:+-$namespace}"
 
     # create backup dir (unless it already exists)
     if [ -d "$backup" ]; then
@@ -442,15 +463,38 @@ has been made.  If you don't want to leave anything behind just
 remove the whole original tree before running C<rlFileRestore>,
 or see C<--clean> option of C<rlFileBackup>.
 
-    rlFileRestore
+    rlFileRestore [--namespace name]
+
+=over
+
+=item --namespace name
+
+Specifies the namespace to use.
+Namespaces can be used to separate backups and their restoration.
+
+=back
 
 Returns 0 if backup dir is found and files are restored successfully.
 
 =cut
 
 rlFileRestore() {
-    # check for backup dir first
-    backup="$BEAKERLIB_DIR/backup"
+    local OPTS namespace=""
+
+    # getopt will cut off first long opt when no short are defined
+    OPTS=$(getopt -o "n:" -l "namespace:" -- "$@")
+    [ $? -ne 0 ] && return 1
+
+    eval set -- "$OPTS"
+    while true; do
+        case "$1" in
+            '--namespace') shift; namespace="$1"; shift ;;
+            --) shift; break ;;
+        esac
+    done;
+
+    # check for backup dir first, append namespace if defined
+    backup="$BEAKERLIB_DIR/backup${namespace:+-$namespace}"
     if [ -d "$backup" ]; then
         rlLogDebug "rlFileRestore: Backup dir ready: $backup"
     else
diff --git a/src/test/infrastructureTest.sh b/src/test/infrastructureTest.sh
index 7d9fadf..8542728 100644
--- a/src/test/infrastructureTest.sh
+++ b/src/test/infrastructureTest.sh
@@ -198,6 +198,38 @@ test_rlFileBackupCleanAndRestoreWhitespace() {
     chmod -R 777 "$BEAKERLIB_DIR/backup" && rm -rf "$BEAKERLIB_DIR/backup"
 }
 
+test_rlFileBackupAndRestoreNamespaces() {
+    test_file=$(mktemp '/tmp/beakerlib-test-XXXXXX')
+    test_file2=$(mktemp '/tmp/beakerlib-test-XXXXXX')
+
+    # namespaced backup should restore independently
+    echo "abcde" > "$test_file"
+    rlFileBackup "$test_file"
+    echo "fghij" > "$test_file2"
+    rlFileBackup --namespace myspace1 "$test_file2"
+    echo "12345" > "$test_file"
+    echo "67890" > "$test_file2"
+    rlFileRestore
+    assertRun "grep 'abcde' '$test_file' && grep '67890' '$test_file2'" 0 \
+        "Normal restore shouldn't restore namespaced backup"
+    echo "12345" > "$test_file"
+    echo "67890" > "$test_file2"
+    rlFileRestore --namespace myspace1
+    assertRun "grep '12345' '$test_file' && grep 'fghij' '$test_file2'" 0 \
+        "Namespaced restore shouldn't restore normal backup"
+
+    # namespaced backup doesn't overwrite normal one
+    echo "abcde" > "$test_file"
+    rlFileBackup "$test_file"
+    echo "12345" > "$test_file"
+    rlFileBackup --namespace myspace2 "$test_file"
+    rlFileRestore
+    assertRun "grep 'abcde' '$test_file'" 0 \
+        "Namespaced backup shouldn't overwrite normal one"
+
+    rm -f "$test_file" "$test_file2"
+    chmod -R 777 "$BEAKERLIB_DIR"/backup* && rm -rf "$BEAKERLIB_DIR"/backup*
+}
 
 test_rlFileBackup_MissingFiles() {
     local dir
-- 
1.7.11.4



More information about the beakerlib-devel mailing list