zookeeper backup for containers

PROD-21753

Change-Id: Ic0572e3acb7f0dfff4478e64666129dea076f616
diff --git a/README.rst b/README.rst
index 0a79118..58154bb 100644
--- a/README.rst
+++ b/README.rst
@@ -51,6 +51,22 @@
   .. note:: full_backups_to_keep param states how many backup will be stored locally on zookeeper client.
             More options to relocate local backups can be done using salt-formula-backupninja.
 
+Backup client containers with ssh/rsync remote host
+
+.. code-block:: yaml
+
+    zookeeper:
+      backup:
+        client:
+          enabled: true
+          full_backups_to_keep: 3
+          hours_before_full: 24
+          containers:
+          - opencontrail_controller_1
+          target:
+            host: cfg01
+            backup_dir: server-home-dir
+
 Backup client with local backup only
 
 .. code-block:: yaml
@@ -201,6 +217,23 @@
   .. note:: restore_latest param with a value of 1 means to restore db from the last full backup. 2 would mean to restore second latest full backup.
 
 
+Client restore container from remote backup:
+
+.. code-block:: yaml
+
+    zookeeper:
+      backup:
+        client:
+          enabled: true
+          full_backups_to_keep: 3
+          hours_before_full: 24
+          containers:
+          - opencontrail_controller_1
+          target:
+            host: cfg01
+          restore_latest: 1
+          restore_from: remote
+
 Read more
 =========
 
diff --git a/zookeeper/files/backup/zookeeper-backup-client-restore.sh b/zookeeper/files/backup/zookeeper-backup-client-restore.sh
index d894573..019d039 100644
--- a/zookeeper/files/backup/zookeeper-backup-client-restore.sh
+++ b/zookeeper/files/backup/zookeeper-backup-client-restore.sh
@@ -76,6 +76,28 @@
 # LOAD BACKUP FILE
 # ----------------
     # Extract snapshot package
+
+    {%- if backup.client.containers is defined %}
+    BACKUPFILE_PARENT_DIR=`dirname $BACKUPFILE`
+    {%- for container_name in backup.client.containers %}
+
+    docker exec {{ container_name }} mkdir -p $BACKUPFILE_PARENT_DIR
+    docker cp $BACKUPFILE {{ container_name }}:$BACKUPFILE
+
+    docker exec {{ container_name }} tar -xvzf $BACKUPFILE -P
+    RC=$?
+
+    if [ $RC -gt 0 ]; then
+        printf "\nBackup file $BACKUPFILE failed to load.\n"
+        exit 1
+    else
+        printf "\nBackup file $BACKUPFILE was succesfully loaded.\n"
+        touch $DBALREADYRESTORED
+    fi
+
+    {%- endfor %}
+    {%- else %}
+
     tar -xvzf "$BACKUPFILE" -P
     RC=$?
 
@@ -87,4 +109,6 @@
         touch $DBALREADYRESTORED
     fi
 
+    {%- endif %}
+
 # Fin.
diff --git a/zookeeper/files/backup/zookeeper-backup-client-runner.sh b/zookeeper/files/backup/zookeeper-backup-client-runner.sh
index e66f4ae..fbb72e7 100644
--- a/zookeeper/files/backup/zookeeper-backup-client-runner.sh
+++ b/zookeeper/files/backup/zookeeper-backup-client-runner.sh
@@ -74,8 +74,12 @@
     else
         # Include the timestamp in the filename
         FILENAME="$BACKUPDIR/$TIMESTAMP/zookeeper-$TIMESTAMP.tar.gz"
+ 
+        {%- if backup.client.containers is defined %}
+        {%- for container_name in backup.client.containers %}
 
-        tar -zcvf $FILENAME -P $ZOOKEEPERDIR > /dev/null 2>&1
+        docker exec {{ container_name }} mkdir -p $BACKUPDIR/$TIMESTAMP
+        docker exec {{ container_name }} tar -zcvf $FILENAME -P $ZOOKEEPERDIR > /dev/null 2>&1
         RC=$?
 
         if [ $RC -gt 0 ]; then
@@ -86,6 +90,27 @@
             printf "Successfully created a backup tar file.\n"
             [ "$TMPDIR" != "/" ] && rm -rf "$TMPDIR"
         fi
+        docker cp {{ container_name }}:$FILENAME $BACKUPDIR/$TIMESTAMP
+        docker exec {{ container_name }} rm $FILENAME
+
+        {%- endfor %}
+        {%- else %}
+        tar -zcvf $FILENAME -P $ZOOKEEPERDIR > /dev/null 2>&1
+
+        RC=$?
+
+        if [ $RC -gt 0 ]; then
+            printf "Error generating tar archive.\n"
+            [ "$TMPDIR" != "/" ] && rm -rf "$TMPDIR"
+            exit 1
+        else
+            printf "Successfully created a backup tar file.\n"
+            [ "$TMPDIR" != "/" ] && rm -rf "$TMPDIR"
+        fi
+
+        {%- endif %}
+
+
     fi
 
 # rsync just the new or modified backup files