Tweaked comment in last commit
[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 if [[ -z "$SCRIPT_UNDER_TEST" ]]; then
20     exec >> $logdir/zfs-restore.log 2>&1
21 fi
22
23 fatal_and_exit() {
24   echo -e 2>&1 "$1"
25   exit 1
26 }
27 trap fatal_and_exit INT
28
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>"
32 fi
33
34 date=`date`
35 echo "$date ZFS restore started: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"
36
37 # check for localhost
38 if [[ $dst_hostname = "localhost" ]]; then
39   dst_hostname=""
40   ssh=""
41 fi
42
43 # Make sure the src pool and src filesystem exist, or print some errors
44 zpool list -H "$src_pool" >/dev/null 2>&1
45 if [ $? != 0 ]; then
46   fatal_and_exit "-E- The src pool, '$src_pool' doesn't seem to exist."
47 fi
48 zfs list "$src_pool/$src_fs" >/dev/null 2>&1
49 if [ $? != 0 ]; then
50   fatal_and_exit "-E- The src filesystem for the src pool, '$src_pool/$src_fs' doesn't seem to exist."
51 fi
52
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
56 if [ $? != 0 ]; then
57   fatal_and_exit "-E- Unable to extract the guid for the src pool on $hostname: $src_pool" $mailto
58 fi
59
60 # Setup our backup marker names
61 last_backup_marker=${src_fs}@previous-backup-${src_pool_guid}
62
63 # Check to make sure the src fs exists
64 $zfs list -t snapshot "$src_pool/$last_backup_marker" > /dev/null 2>&1
65 if [ $? != 0 ]; then
66   fatal_and_exit "-E- The src snapshot '$src_pool/$last_backup_marker' does not exist. Unable to continue."
67 fi
68
69 # Check to make sure the dst pool exists
70 $ssh $dst_hostname $zfs list ${dst_pool} > /dev/null 2>&1
71 if [ $? != 0 ]; then
72   fatal_and_exit "-E- The destination pool '$dst_pool' does not exist. Create the pool '$dst_pool' and try again."
73 fi
74
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
77 if [ $? == 0 ]; then
78   fatal_and_exit "-E- The destination pool/filesystem '$dst_pool/$dst_fs' already exists. Destroy the filesystem '$dst_fs' and try again."
79 fi 
80
81 # Now send the src filesystem
82 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
83   echo "$zfs send -R $src_pool/$last_backup_marker | $ssh $dst_hostname $zfs recv -dv $dst_pool"
84 else 
85    if [[ $throttle_enable == 1 && -e $throttle ]]; then
86      $zfs send -R "$src_pool/$last_backup_marker" | $throttle $throttle_opt | $ssh $dst_hostname $zfs recv -dv $dst_pool
87    else 
88      $zfs send -R "$src_pool/$last_backup_marker" | $ssh $dst_hostname $zfs recv -dv $dst_pool
89    fi 
90 fi
91
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"
95 else
96   $ssh $dst_hostname $zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs
97 fi
98
99 # All done!
100 date=`date`
101 echo "$date ZFS restore completed: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"
102