-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbaksnapperd.sh
More file actions
executable file
·220 lines (214 loc) · 6.71 KB
/
Copy pathbaksnapperd.sh
File metadata and controls
executable file
·220 lines (214 loc) · 6.71 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#! /usr/bin/env bash
# Baksnapperd - Daemon used by baksnapper when backup snapper snapshots
# SPDX-FileCopyrightText: 2015-2024 Fredrik Salomonsson <plattfot@posteo.net>
# SPDX-FileCopyrightText: 2021-2022 Nathan Dehnel
# SPDX-FileCopyrightText: 2023 Juergen Gleiss
#
# SPDX-License-Identifier: GPL-3.0-or-later
read -rd '' version <<EOF
baksnapperd (baksnapper) @VERSION@
Copyright (C) 2015-2025 Fredrik Salomonsson
Copyright (C) 2021-2022 Nathan Dehnel
Copyright (C) 2023 Juergen Gleiss
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF
function error {
echo "[ERROR] $1" 1>&2
exit 1
}
function warning {
echo -e "[Warning] $1" 1>&2
}
case "$1" in
version)
# Return what version of the API this is using.
#
# It always a single integer.
# No input.
echo 4
;;
snapshot-type)
# Return what type of snapshot this backend supports
#
# No input.
echo snapper
;;
list-snapshots)
# List snapshots at location.
#
# Stripping any tags as they are volatile.
# 1 [in]: Path to the directory the snapshots are expected to be in.
shift
find "$1" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort -g
;;
get-snapper-root)
# Return the location of the .snapshots directory
#
# 1: Name of the config
shift
snapper --no-dbus -c "$1" get-config | grep SUBVOLUME | awk '{ print $3 }'
;;
verify-snapshot)
# Verify that a snapshot exist.
#
# 1 [in]: File path to a snapshot
# Return 0 if it exist, non-zero otherwise.
shift
test -d "$1" || error "Snapshot $1 doesn't exist."
;;
incomplete-snapshot)
# Check if a snapshot is incomplete.
#
# 1 [in]: Path to the directory the snapshot is in.
# 2 [in]: Name of the snapshot.
# Return 0 if the snapshot is incomplete, non-zero otherwise.
shift
[[ ! -f "$1/$2/info.xml" ]] || \
[[ ! -d "$1/$2/snapshot" ]] || \
[[ $(btrfs property get "$1/$2/snapshot" ro) != "ro=true" ]]
;;
create-location)
# Prepare destination to receive a snapshot.
#
# 1 [in]: Path to the directory the snapshots will be in.
shift
mkdir -p "$1"
;;
create-snapshot)
# Prepare destination to receive a snapshot.
#
# 1 [in]: path to the directory the snapshot will be in.
# 2 [in]: name of the snapshot.
shift
mkdir -p "$1"/"$2"
;;
receive-info)
# Receive the info.xml for a snapshot
#
# 1 [in]: Path to the directory the snapshot will be in.
# 2 [in]: Name of the snapshot
shift
info="$1/$2/info.xml"
[ -e "$info" ] && rm -f -- "$info"
touch "$info"
# Read from stdin
while read -r line || [[ -n $line ]]
do
echo "$line" >> "$info"
done
;;
receive-snapshot)
# Receive a snapshot.
#
# 1 [in]: Path to the directory the snapshot will be in.
# 2 [in]: Name of the snapshot.
shift
btrfs receive "$1/$2"
;;
send-info)
# Send the info.xml for a snapshot.
#
# 1 [in]: Path to the directory the snapshot is in.
# 2 [in]: Name of the snapshot.
shift
cat "$1/$2/info.xml"
;;
send-snapshot)
# Send a snapshot.
#
# 1 [in]: Path to the directory the snapshot is in.
# 2 [in]: Name of the snapshot.
shift
btrfs send "$1/$2/snapshot"
;;
send-incremental-snapshot)
# Only send what is different from the parent snapshot
#
# 1 [in]: File path to the parent snapshot.
# 2 [in]: File path to the snapshot.
shift
btrfs send -p "$1/snapshot" "$2/snapshot"
;;
remove-snapshots)
# Remove all snapshots listed in the input.
#
# 1 [in]: Path to the directory the snapshots are in.
# 2+ [in]: Name of the snapshots
shift
dest_root=$1
shift
for snapshot in "$@"
do
# Only delete directories containing info.xml and snapshot
mapfile -t content < <(find "$dest_root/$snapshot" \
-maxdepth 1 -mindepth 1 -printf "%f\n" 2> /dev/null | \
sort)
if [[ ${#content[@]} == 2 && \
"${content[0]}" == "info.xml" && \
"${content[1]}" == "snapshot" ]]; then
echo "Deleting snapshot $snapshot"
btrfs subvolume delete "$dest_root/$snapshot/snapshot"
rm -r -- "${dest_root:?}/$snapshot"
else
warning "Snapshot $snapshot doesn't match a snapper snapshot, "\
"ignoring it."
fi
done
;;
remove-broken-snapshot)
# Remove a broken snapshot.
#
# 1 [in]: Path to the directory the snapshot is in.
# 2 [in]: Name of the snapshot.
shift
dest_root=$1
shift
snapshot=$1
if [[ -d "$dest_root/$snapshot/snapshot" ]]
then
btrfs subvolume delete "$dest_root/$snapshot/snapshot"
fi
rm -r -- "${dest_root:?}/$snapshot"
;;
link-latest)
# Create symlink `latest` to the latest snapshot.
#
# 1 [in]: Path to the directory the snapshots are in.
shift
declare -a snapshots
if [[ -e "$1/latest" ]]; then
if [[ -h "$1/latest" ]]; then
rm "$1/latest"
else
error "$1/latest exists and is not a symbolic link. Link is not created."
fi
fi
for dir in $(find "$1" -maxdepth 1 -mindepth 1 -type d -printf "%P\n"|sort --version-sort)
do
if [[ -d "$1/$dir/snapshot" && ! -h "$1/$dir" ]]; then
snapshots+=("$dir")
fi
done
if [ ${#snapshots[@]} -ne 0 ]; then
ln -sfn "${snapshots[-1]}" "$1/latest"
else
error "link-latest: No suitable snapshots at $1 (${#snapshots[@]})"
fi
;;
test-connection)
# A test to make sure this backend listen to commands.
#
# No input
# Always return 0 when called successfully.
exit 0
;;
--version)
echo -e "$version"
exit 0
;;
*)
error "Unrecognized command '$1', bailing out!"
;;
esac