-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs-func
76 lines (73 loc) · 2.77 KB
/
zfs-func
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
# to source in bashrc
# pools to sync/compare between:
pool=( Data DataBackup DataNew )
# log file for stderror of zfs command
log=/tmp/zfs.log
# zfs wrapper function so stderr of zfs command doesn't 'flood' terminal
# for example if used wrong in which case stderr gets redirected to $log
zfs(){
sudo /usr/bin/zfs "$@" 2> >(
read -r && printf '%s\n' "$REPLY" >&2
read -r && [ ! -z "$REPLY" ] \
&& printf 'more error output in "%s"\n' "$log" >&2 \
&& printf '\n%s\n' "[$(date --rfc-3339=seconds)]" >> "$log" \
&& printf '%s\n' "$REPLY" >> "$log"; cat >> "$log"
)
}
# get pool name/root dir of a dataset/folder
getPool(){
[ $# -ne 1 ] || [ -z "$1" ] && return 1
[ "$(dirname "$1")" = "/" ] || [ "$(dirname "$1")" = "." ] \
&& printf '%s\n' "$1" && return 0
printf '%s\n' "$(getPool "$(dirname "$1")")"
}
# compare $1 (which is a dir) between pools; choosing pool of $1 as master
comparePool(){
[ $# -ne 1 ] || [ -z "$1" ] \
&& printf 'Usage: %s <filesystem>' "$0" && return 1
root="$(getPool "$1")"; root=${root///}
for i in "${pool[@]}"; do [ "$root" = "$i" ] && continue
printf 'comparing "%s" -> %s:\n' "$root" "$i"
sudo rsync -axHAXS --delete -i --dry-run "$1"/ /"$i${1//"/$root"}"/
done
}
# sync $1 (which is a dir) between pools; choosing pool of $1 as master
syncPool(){
[ $# -ne 1 ] || [ -z "$1" ] \
&& printf 'Usage: %s <filesystem>' "$0" && return 1
root="$(getPool "$1")"; root=${root///}
for i in "${pool[@]}"; do [ "$root" = "$i" ] && continue
printf 'comparing "%s" -> %s:\n' "$root" "$i"
sudo rsync -axHAXS -i --delete "$1/" /"$i${1//"/$root"}"/
done
}
#to source
zfs-send(){
dataset="$1"
snapshotFirst="$(zfs list -Ho name -t snapshot -d 1 -r "$dataset" | head -1)"
snapshotLast="$(zfs list -Ho name -t snapshot -d 1 -r "$dataset" | tail -1)"
attr=( compression mountpoint sharenfs exec setuid acltype xattr sharesmb )
parameter=()
for i in "${attr[@]}"; do
value="$(zfs get -Ho value "$i" "$dataset")"
[ "$value" = "-" ] && continue
parameter+=("-o")
parameter+=("$i=$value")
done
zfs send -cL "$snapshotFirst" \
| zfs recv -Fsuv "${parameter[@]}" "$(
i="${dataset//DataBackup/DataNew}"
echo "${i%@*}"
)"
if [ "$snapshotFirst" != "$snapshotLast" ]; then
zfs send -cLI "$snapshotFirst" "$snapshotLast" \
| zfs recv -Fsuv "${parameter[@]}" "$(
i="${dataset//DataBackup/DataNew}"
echo "${i%@*}"
)"
fi
}
# destroys only dataset including all its snapshots, throws error if there are other children
zfs-destroy(){
zfs list -Ht snapshot -o name -d 1 -r "$1" | while read -r snapshot; do zfs destroy "$snapshot"; done && zfs destroy "$1"
}