Made detecting if zpool is already mounted or unmounted better
[zfs-ubuntu/.git] / zfs-restore-all
1 #!/bin/bash
2
3 # Author: Alan J. Pippin
4 # Description: This script calls zfs-restore for each filesystem needing
5 #              to be restored (that was formerly replicated here) to another ZFS pool.
6
7 # source our configuration 
8 config="${0%/*}/zfs-scripts.conf"
9 [ -e "${config}.dist" ] && . ${config}.dist
10 [ -e "${config}" ] && . ${config}
11
12 # Setup some default values
13 logfile="$logdir/zfs-restore-all.log"
14
15 # Setup our output
16 if [[ -z "$SCRIPT_UNDER_TEST" ]]; then
17     exec >> $logfile 2>&1
18 fi
19
20 cleanup_and_exit() {
21   exit 1
22 }
23 trap cleanup_and_exit INT
24
25 # See if the user has a specific pool to restore in mind
26 restore_pool=$1
27
28 # Restore every ZFS filesystem we were told to replicate 
29 echo `date` ZFS restore started
30
31 # For each filesystem we are supposed to restore, do it
32 for filesystem in $filesystems_to_replicate; do
33   if [[ $filesystem =~ ':' ]]; then
34       src_pool=${filesystem%%:*}
35       filesystem=${filesystem#*:} # remove src_pool from string
36   else
37       src_pool=${backup_pool%% *} # use the first backup pool if none specified
38   fi
39   dst_pool=${filesystem%%/*}
40   dst_fs=${filesystem#*/}
41   # Check to make sure the dst filesystem does not exist
42   if [[ $remote = "localhost" ]]; then
43       $ssh $remote $zfs list ${dst_pool}/${dst_fs} > /dev/null 2>&1
44   else 
45       $zfs list ${dst_pool}/${dst_fs} > /dev/null 2>&1
46   fi
47   if [ $? != 0 ]; then
48     echo "$filesystem" | grep -q "$restore_pool" 
49     if [ $? == 0 ]; then
50       # This filesystem matches our restore pool pattern 
51       echo `date` Restoring $filesystem to $remote
52       zfs-restore $src_pool $filesystem $dst_pool $dst_fs $remote
53     fi
54   else
55     echo "-I- Filesystem already exists on destination. Skipping: $filesystem"
56   fi 
57 done
58
59 # All done
60 echo `date` ZFS restore complete
61