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>
7 # source our configuration
8 config="${0%/*}/zfs-scripts.conf"
9 [ -e "${config}.dist" ] && . ${config}.dist
10 [ -e "${config}" ] && . ${config}
12 # command line arg parsing
19 if [[ -z "$SCRIPT_UNDER_TEST" ]]; then
20 exec >> $logdir/zfs-restore.log 2>&1
27 trap fatal_and_exit INT
29 # Make sure we have valid arguments
30 if [[ -z "$src_pool" ]] || [[ -z "$src_fs" ]] || [[ -z "$dst_pool" ]] || [[ -z "$dst_fs" ]] || [[ -z "$dst_hostname" ]]; then
31 fatal_and_exit "Usage: $0 <src pool> <src filesystem> <dst pool> <dst filesystem> <dst hostname>"
35 echo "$date ZFS restore started: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"
38 if [[ $dst_hostname = "localhost" ]]; then
43 # Make sure the src pool and src filesystem exist, or print some errors
44 zpool list -H "$src_pool" >/dev/null 2>&1
46 fatal_and_exit "-E- The src pool, '$src_pool' doesn't seem to exist."
48 zfs list "$src_pool/$src_fs" >/dev/null 2>&1
50 fatal_and_exit "-E- The src filesystem for the src pool, '$src_pool/$src_fs' doesn't seem to exist."
53 # Obtain the zpool guid for the src pool
54 src_pool_guid=`zpool get guid $src_pool 2>&1 | grep $src_pool | awk '{ print $3 }'`
55 zpool get guid $src_pool > /dev/null 2>&1
57 fatal_and_exit "-E- Unable to extract the guid for the src pool on $hostname: $src_pool" $mailto
60 # Setup our backup marker names
61 last_backup_marker=${src_fs}@previous-backup-${src_pool_guid}
63 # Check to make sure the src fs exists
64 $zfs list -t snapshot "$src_pool/$last_backup_marker" > /dev/null 2>&1
66 fatal_and_exit "-E- The src snapshot '$src_pool/$last_backup_marker' does not exist. Unable to continue."
69 # Check to make sure the dst pool exists
70 $ssh $dst_hostname $zfs list ${dst_pool} > /dev/null 2>&1
72 fatal_and_exit "-E- The destination pool '$dst_pool' does not exist. Create the pool '$dst_pool' and try again."
75 # Check to make sure the dst filesystem does not exist
76 $ssh $dst_hostname $zfs list ${dst_pool}/${dst_fs} > /dev/null 2>&1
78 fatal_and_exit "-E- The destination pool/filesystem '$dst_pool/$dst_fs' already exists. Destroy the filesystem '$dst_fs' and try again."
81 # Now send the src filesystem
82 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
83 echo "$zfs send -Rc $src_pool/$last_backup_marker | $ssh $dst_hostname $zfs recv -dv $dst_pool"
85 if [[ $throttle_enable == 1 && -e $throttle ]]; then
86 $zfs send -Rc "$src_pool/$last_backup_marker" | $throttle $throttle_opt | $ssh $dst_hostname $zfs recv -dv $dst_pool
88 $zfs send -Rc "$src_pool/$last_backup_marker" | $ssh $dst_hostname $zfs recv -dv $dst_pool
92 # Now rename the dst filesystem (move it into place)
93 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
94 echo "$ssh $dst_hostname $zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs"
96 $ssh $dst_hostname $zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs
101 echo "$date ZFS restore completed: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"