-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-zfs
executable file
·157 lines (133 loc) · 5.27 KB
/
backup-zfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/sh
# Written in 2022 by Mason Loring Bliss <[email protected]>
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see:
#
# http://creativecommons.org/publicdomain/zero/1.0/
# ---------------------------------------------------------------------------
# User config:
# POOLS is a space-separated list of pools to back up.
POOLS="tank"
# SAFE is where the back-ups will be sent.
SAFE="backup/snapshots/mybox"
# List datasets to exclude. We make the recursive snapshot, but then remove
# it from these datasets.
IGNORE="tank/some/dataset tank/swap"
# Note that if there are snapshots created by other processes, they will be
# shipped, as things stand. See the code, below, for options here.
# If SAFE is on another system, TRANSPORT shows how to get there.
TRANSPORT=""
# TRANSPORT can be, for example, "ssh foo@bar" or you can leave it empty to
# use local pools on both sides of the transfer.
# Create backup/snapshots/mybox in advance - each specified pool will exist
# inside of it.
# ---------------------------------------------------------------------------
# Ensure that only root is running us.
if [ $(id -u) -ne 0 ]; then
echo "Requires root. Exiting."
exit 1
fi
# Take a new snapshot of the pools we're to back up.
SNAPSHOT="backup-$(hostname | awk -F. '{print $1}')-$(date +%Y%m%d-%H%M%S)"
for dataset in $POOLS
do
echo Creating recursive snapshot $dataset@$SNAPSHOT...
zfs snapshot -r "$dataset@$SNAPSHOT"
done
# ...and remove it from the datasets listed in IGNORE.
for dataset in $IGNORE
do
zfs destroy "$dataset@$SNAPSHOT"
# Note:
#
# If you want to remove all snapshots from the listed datasets, in
# case, for instance, there are other processes that create snapshots,
# do something like this instead:
#
# zfs list -Ht snap -o name $dataset | xargs -n 1 zfs destroy
#
# or if this looks cleaner to you:
#
# for snapshot in $(zfs list -Ht snap -o name $dataset)
# do
# zfs destroy $snapshot
# done
done
# Save off a list of snapshots that exist in the pools we'll back up.
SOURCESNAPS=$(mktemp /tmp/snaps-local-XXXXXX)
for pool in $POOLS
do
zfs list -Hrt snap -o name -S createtxg $pool >> $SOURCESNAPS
done
# Save off a list of snapshots that exist in the destination.
DESTSNAPS=$(mktemp /tmp/snaps-remote-XXXXXX)
for pool in $POOLS
do
$TRANSPORT zfs list -Hrt snap -o name $SAFE/$pool \
| sed -e "s,^$SAFE/,," >> $DESTSNAPS
done
# Find snapshots that exist in both source and destination.
SHAREDSNAPS=$(mktemp /tmp/snaps-shared-XXXXXX)
cat $SOURCESNAPS $DESTSNAPS | sort | uniq -d > $SHAREDSNAPS
# For each dataset to be considered...
for dataset in $(sed -e "s/@.*//" $SOURCESNAPS | sort -u)
do
# Identify the most recent snapshot. (Might not be the one we just
# took. That can be broken off into separate scripting if desired.)
LATEST=$(grep "^$dataset@" $SOURCESNAPS | head -n 1)
# If it's already backed up, say so...
grep -q "^$LATEST" $SHAREDSNAPS
if [ $? -eq 0 ]; then
echo "$LATEST already backed up."
continue
fi
# See if this dataset has a canmount property. We don't want to try to
# force a canmount for volumes, for example.
ARCHIVEDCANMOUNT=""
SOURCECANMOUNT=$(zfs get -o value -H canmount $dataset)
if [ $SOURCECANMOUNT != '-' ] ; then
# See if we're copying something that's already an archive.
if [ $(zfs get -o value -H archive:canmount $dataset) = '-' ] ; then
ARCHIVEDCANMOUNT="-o archive:canmount=$SOURCECANMOUNT"
fi
ARCHIVEDCANMOUNT="$ARCHIVEDCANMOUNT -o canmount=off"
fi
# Find the smallest set of snapshots we can send, searching newest
# backwards.
INCREMENTAL=0
for potential in $(grep "^$dataset@" $SOURCESNAPS)
do
# See if the older snapshot exists in the back-ups.
grep -q "^$potential" $SHAREDSNAPS
if [ $? -eq 0 ]; then
# The snapshot exists in back-ups, so we can use it as a base.
INCREMENTAL=1
echo "Sending incremental $potential -> $SAFE/$LATEST"
zfs send -I $potential $LATEST | $TRANSPORT zfs receive \
$ARCHIVEDCANMOUNT -Fu $SAFE/$dataset
break
fi
done
# We don't see a snapshot for this dataset. Send everything
# recursively. Note that if you have a funny situation where there are
# older snapshots but there's no overlap between the oldest on your
# local dataset and the newest on backup media, you'll need to manually
# correct this.
if [ $INCREMENTAL -eq 0 ]; then
FIRST=$(grep "^$dataset@" $SOURCESNAPS | tail -n 1)
FIRSTSNAP=$(echo $FIRST | sed -e "s/^[^@]*//")
echo "Seeding with $FIRST..."
zfs send -p $FIRST | $TRANSPORT zfs receive \
$ARCHIVEDCANMOUNT -Fu $SAFE/$dataset
echo "Sending incremental $FIRST -> $SAFE/$LATEST"
zfs send -p -i $FIRSTSNAP $LATEST | $TRANSPORT zfs receive \
$ARCHIVEDCANMOUNT -Fu $SAFE/$dataset
fi
done
rm $SOURCESNAPS $DESTSNAPS $SHAREDSNAPS