Added an already running check
[zfs-nexenta/.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 # Make sure the src pool and src filesystem exist, or print some errors
38 zpool list -H "$src_pool" >/dev/null 2>&1
39 if [ $? != 0 ]; then
40   fatal_and_exit "-E- The src pool, '$src_pool' doesn't seem to exist."
41 fi
42 zfs list "$src_pool/$src_fs" >/dev/null 2>&1
43 if [ $? != 0 ]; then
44   fatal_and_exit "-E- The src filesystem for the src pool, '$src_pool/$src_fs' doesn't seem to exist."
45 fi
46
47 # Obtain the zpool guid for the src pool
48 src_pool_guid=`zpool get guid $src_pool 2>&1 | grep $src_pool | awk '{ print $3 }'`
49 zpool get guid $src_pool > /dev/null 2>&1
50 if [ $? != 0 ]; then
51   fatal_and_exit "-E- Unable to extract the guid for the src pool: $src_pool" $mailto
52 fi
53
54 # Setup our backup marker names
55 last_backup_marker=${src_fs}@previous-backup-${src_pool_guid}
56
57 # Check to make sure the src fs exists
58 $zfs list -t snapshot "$src_pool/$last_backup_marker" > /dev/null 2>&1
59 if [ $? != 0 ]; then
60   fatal_and_exit "-E- The src snapshot '$src_pool/$last_backup_marker' does not exist. Unable to continue."
61 fi
62
63 # Check to make sure the dst pool exists
64 ssh $dst_hostname "$zfs list ${dst_pool}" > /dev/null 2>&1
65 if [ $? != 0 ]; then
66   fatal_and_exit "-E- The destination pool '$dst_pool' does not exist. Create the pool '$dst_pool' and try again."
67 fi
68
69 # Check to make sure the dst filesystem does not exist
70 ssh $dst_hostname "$zfs list ${dst_pool}/${dst_fs}" > /dev/null 2>&1
71 if [ $? == 0 ]; then
72   fatal_and_exit "-E- The destination pool/filesystem '$dst_pool/$dst_fs' already exists. Destroy the filesystem '$dst_fs' and try again."
73 fi 
74
75 # Now send the src filesystem
76 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
77   echo "$zfs send -R $src_pool/$last_backup_marker | ssh $dst_hostname $zfs recv -dv $dst_pool"
78 else 
79    if [[ $throttle_enable == 1 && -e $throttle ]]; then
80      $zfs send -R "$src_pool/$last_backup_marker" | $throttle $throttle_opt | ssh $dst_hostname "$zfs recv -dv $dst_pool"
81    else 
82      $zfs send -R "$src_pool/$last_backup_marker" | ssh $dst_hostname "$zfs recv -dv $dst_pool"
83    fi 
84 fi
85
86 # Now rename the dst filesystem (move it into place)
87 if [[ -n "$SCRIPT_UNDER_TEST" ]]; then
88   echo "$dst_hostname $zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs"
89 else
90   ssh $dst_hostname "$zfs rename $dst_pool/$src_fs $dst_pool/$dst_fs"
91 fi
92
93 # All done!
94 date=`date`
95 echo "$date ZFS restore completed: $src_pool/$src_fs -> $dst_hostname:$dst_pool/$dst_fs"
96