Return non-zero exit code if any command in pipe fails
[zfs-ubuntu/.git] / zfs-restore
1 #!/bin/bash
2
3 # Author: Alan Pippin
4 # Description: This script "restores" or sends a local filesystem to a remote zfs pool.
5 # Usage: zfs-restore <src pool> <src filesystem> <dst pool> <dst filesystem> <dst hostname>
6
7 # source our configuration
8 config="${0%/*}/zfs-scripts.conf"
9 [ -e "${config}.dist" ] && . ${config}.dist
10 [ -e "${config}" ] && . ${config}
11
12 # command line arg parsing
13 src_pool=$1
14 src_fs=$2
15 dst_pool=$3
16 dst_fs=$4
17 dst_hostname=$5
18
19 # return non-zero exit code if any command in the pipe fails
20 set -o pipefail
21
22 if [[ -z "$SCRIPT_UNDER_TEST" ]]; then
23     exec >> $logdir/zfs-restore.log 2>&1
24 fi
25
26 fatal_and_exit() {
27   echo -e 2>&1 "$1"
28   exit 1
29 }
30 trap fatal_and_exit INT
31
32 # Make sure we have valid arguments
33 if [[ -z "$src_pool" ]] || [[ -z "$src_fs" ]] || [[ -z "$dst_pool" ]] || [[ -z "$dst_fs" ]] || [[ -z "$dst_hostname" ]]; then
34   fatal_and_exit "Usage: $0 <src pool> <src filesystem> <dst pool> <dst filesystem> <dst hostname>"
35 fi
36
37 date=`date`
38 echo "$date ZFS restore started: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"
39
40 # check for localhost
41 if [[ $dst_hostname = "localhost" ]]; then
42   dst_hostname=""
43   ssh=""
44 fi
45
46 # Make sure the src pool and src filesystem exist, or print some errors
47 zpool list -H "$src_pool" >/dev/null 2>&1
48 if [ $? != 0 ]; then
49   fatal_and_exit "-E- The src pool, '$src_pool' doesn't seem to exist."
50 fi
51 zfs list "$src_pool/$src_fs" >/dev/null 2>&1
52 if [ $? != 0 ]; then
53   fatal_and_exit "-E- The src filesystem for the src pool, '$src_pool/$src_fs' doesn't seem to exist."
54 fi
55
56 # Obtain the zpool guid for the src pool
57 src_pool_guid=`zpool get guid $src_pool 2>&1 | grep $src_pool | awk '{ print $3 }'`
58 zpool get guid $src_pool > /dev/null 2>&1
59 if [ $? != 0 ]; then
60   fatal_and_exit "-E- Unable to extract the guid for the src pool on $hostname: $src_pool" $mailto
61 fi
62
63 # Setup our backup marker names
64 last_backup_marker=${src_fs}@previous-backup-${src_pool_guid}
65
66 # Check to make sure the src fs exists
67 $zfs list -t snapshot "$src_pool/$last_backup_marker" > /dev/null 2>&1
68 if [ $? != 0 ]; then
69   fatal_and_exit "-E- The src snapshot '$src_pool/$last_backup_marker' does not exist. Unable to continue."
70 fi
71
72 # Check to make sure the dst pool exists
73 $ssh $dst_hostname $zfs list ${dst_pool} > /dev/null 2>&1
74 if [ $? != 0 ]; then
75   fatal_and_exit "-E- The destination pool '$dst_pool' does not exist. Create the pool '$dst_pool' and try again."
76 fi
77
78 # Check to make sure the dst filesystem does not exist
79 $ssh $dst_hostname $zfs list ${dst_pool}/${dst_fs} > /dev/null 2>&1
80 if [ $? == 0 ]; then
81   fatal_and_exit "-E- The destination pool/filesystem '$dst_pool/$dst_fs' already exists. Destroy the filesystem '$dst_fs' and try again."
82 fi 
83
84 # Now send the src filesystem
85 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
86   echo "$zfs send -Rc $src_pool/$last_backup_marker | $ssh $dst_hostname $zfs recv -dv $dst_pool"
87 else 
88    if [[ $throttle_enable == 1 && -e $throttle ]]; then
89      $zfs send -Rc "$src_pool/$last_backup_marker" | $throttle $throttle_opt | $ssh $dst_hostname $zfs recv -dv $dst_pool
90    else 
91      $zfs send -Rc "$src_pool/$last_backup_marker" | $ssh $dst_hostname $zfs recv -dv $dst_pool
92    fi 
93 fi
94
95 # Now rename the dst filesystem (move it into place)
96 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
97   echo "$ssh $dst_hostname $zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs"
98 else
99   $ssh $dst_hostname $zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs
100 fi
101
102 # All done!
103 date=`date`
104 echo "$date ZFS restore completed: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"
105