-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsnapFunc.sh
141 lines (111 loc) · 3.93 KB
/
snapFunc.sh
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
# functions to source
# snap srcpath snappath mark keep snapname
#
# e.g.: snap /abc/subvol1 /snapshots test 5 testsnapshot1
# creates a snapshot of /abc/subvol1 in /snapshots as testsnapshot1@test
## keeping the last 5 snapshots marked with test
#
msg() {
echo -e "[i] ${1}"
}
snap() {
[ ${DEBUG} ] || msg "${FUNCNAME[*]} parameter_: ${*}"
local src="${1:-$SRCPATH}"
local spath="${2:-$SNAPPATH}"
local mark="${3:-$MARK}"
local keep=${4:-KEEP}
local sname="${5:-$SNAPNAME}"
local snaps2del
local s
snapdel "${src}" "${spath}" "${mark}" ${keep} "${sname}"
btrfs subvolume snapshot -r "${src}" "${spath}/${sname}@${mark}"
}
snapdel() {
[ ${DEBUG} ] || msg "${FUNCNAME[*]} parameter_: ${*}"
local src="${1:-$SRCPATH}"
local spath="${2:-$SNAPPATH}"
local mark="${3:-$MARK}"
local keep=${4:-KEEP}
local sname="${5:-$SNAPNAME}"
local s
msg "removing outdated snapshots @${mark} from ${spath} keeping the last ${keep}"
snaps2del=$(ls -1d "${spath}"/*${mark} | head --lines=-${keep})
for s in ${snaps2del}; do
msg "delete snapshot ${s}"
btrfs subvol delete "${s}"
done
}
# snap snappath snappath2 mark keep snapname
#
# e.g.: snap /snapshots /snapshots2 test 5 mysnap
# will send /snapshots/mysnap@test to /snapshots2/mysnap@test
# keeping the last 5 snapshots marked with test
#
snapsend() {
[ ${DEBUG} ] || msg "${FUNCNAME[*]} parameter_: ${*}"
local spath="${1:-$SNAPPATH}"
local spath2="${2:-$SNAPPATH2}"
local mark="${3:-$MARK}"
local keep=${4:-$KEEP}
local sname="${5:-$SNAPNAME}"
local snaps2del
local parent
local s
snapdel "${src}" "${spath}" "${mark}" ${keep} "${sname}"
msg "Syncing_:${spath}"
btrfs filesystem sync "${spath}"
msg "Syncing_:${spath2}"
btrfs filesystem sync "${spath2}"
parent=$(ls -1tr "${spath2}" | tail -1)
msg "Found ${parent} as parent"
[ "${parent}" == "" ] || {
[ -d "${spath}/${parent}" ] && {
msg "btrfs send -p ${spath}/${parent} ${spath}/${sname}@${mark}|btrfs receive ${spath2}"
btrfs send -p "${spath}/${parent}" "${spath}/${sname}@${mark}" | btrfs receive "${spath2}"
return $?
}
}
msg "No common parent found. Complete snapshot is sent."
msg "btrfs send ${spath}/${sname}@${mark}|btrfs receive ${spath2}"
btrfs send "${spath}/${sname}@${mark}" | btrfs receive "${spath2}"
return $?
}
snapremote() {
[ ${DEBUG} ] || msg "${FUNCNAME[*]} parameter_: ${*}"
local ruser="${1:-$REMOTEUSER}"
local spath="${2:-$SNAPPATH}"
local rpath="${3:-$REMOTESNAPPATH}"
local mark="${4:-$MARK}"
local keep="${5:-$KEEP}"
local sname="${6:-$SNAPNAME}"
local snaps2del
local parent
local s
msg "sending snapshot ${spath}/${sname}@${mark} to ${ruser} ${rpath}"
msg "removing outdated snapshots @${mark} from ${rpath} keeping the last ${keep}"
#cleanup
snaps2del=$(ssh ${ruser} ls -1d "${rpath}"/*@${mark} | head --lines=-${keep})
for s in ${snaps2del}; do
msg "delete snapshot ${s}"
ssh ${ruser} btrfs subvol delete "${s}"
done
msg "Syncing ${rpath}"
ssh ${ruser} btrfs filesystem sync "${rpath}"
msg "looking for parent"
parent=$(ssh ${ruser} ls -1 "${rpath}" | tail -1)
msg "Parent is_:${parent}"
[ "${parent}" == "" ] || {
[ -d "${spath}/${parent}" ] && {
msg "btrfs send -p ${spath}/${parent} ${spath}/${sname}@${mark}|ssh ${ruser} btrfs receive ${rpath}"
btrfs send -p "${spath}/${parent}" "${spath}/${sname}@${mark}" | ssh ${ruser} btrfs receive "${rpath}"
return $?
}
}
msg "No common parent found. Complete snapshot is sent."
#
# send without parent
#
msg "btrfs send ${spath}/${sname}@${mark}|ssh ${ruser} btrfs receive ${rpath}"
btrfs send "${spath}/${sname}@${mark}" | ssh ${ruser} btrfs receive "${rpath}"
return $?
}