Merge branch 'master' of ssh://backup.pippins.net/etc/bin/zfs/
[zfs-nexenta/.git] / zfs-restore-all
diff --git a/zfs-restore-all b/zfs-restore-all
new file mode 100755 (executable)
index 0000000..7d174b4
--- /dev/null
@@ -0,0 +1,52 @@
+#!/bin/bash
+
+# Author: Alan J. Pippin
+# Description: This script calls zfs-restore for each filesystem needing
+#              to be restored (that was formerly replicated here) to another ZFS pool.
+
+# source our configuration 
+config="${0%/*}/zfs-scripts.conf"
+[ -e "${config}.dist" ] && . ${config}.dist
+[ -e "${config}" ] && . ${config}
+
+# Setup some default values
+logfile="$logdir/zfs-restore-all.log"
+
+# Setup our output
+if [[ -z "$SCRIPT_UNDER_TEST" ]]; then
+    exec >> $logfile 2>&1
+fi
+
+cleanup_and_exit() {
+  exit 1
+}
+trap cleanup_and_exit INT
+
+
+# See if the user has a specific pool to restore in mind
+restore_pool=$1
+
+# Restore every ZFS filesystem we were told to replicate 
+echo `date` ZFS restore started
+
+# For each filesystem we are supposed to restore, do it
+for filesystem in $filesystems_to_replicate; do
+  dst_pool=${filesystem%%/*}
+  dst_fs=${filesystem#*/}
+  # Check to make sure the dst filesystem does not exist
+  ssh $remote "$zfs list ${dst_pool}/${dst_fs}" > /dev/null 2>&1
+  if [ $? != 0 ]; then
+    echo "$filesystem" | grep -q "$restore_pool" 
+    if [ $? == 0 ]; then
+      # This filesystem matches our restore pool pattern 
+      echo `date` Restoring $filesystem to $remote
+      zfs-restore $local_pool $filesystem $dst_pool $dst_fs $remote
+    fi
+  else
+    echo "-I- Filesystem already exists on destination. Skipping: $filesystem"
+  fi 
+done
+
+# All done
+echo `date` ZFS restore complete
+