-
Notifications
You must be signed in to change notification settings - Fork 5
/
baksnapperd.sh
executable file
·150 lines (144 loc) · 4.24 KB
/
baksnapperd.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
142
143
144
145
146
147
148
149
150
#! /bin/bash
# Baksnapperd - Daemon used by baksnapper when backup via ssh
# 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) 2.3.0
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 it's using, always one integer
echo 3
;;
list-snapshots) # List snapshots at backup location
shift
find "$1" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort -g
;;
get-snapper-root) # Return the location of the .snapshots directory
shift
snapper --no-dbus -c "$1" get-config | grep SUBVOLUME | awk '{ print $3 }'
;;
verify-snapshot)
shift
find "$1" &> /dev/null || error "Snapshot $1 doesn't exist."
;;
incomplete-snapshot)
shift
[[ ! -f "$1/$2/info.xml" ]] || \
[[ ! -d "$1/$2/snapshot" ]] || \
[[ $(btrfs property get "$1/$2/snapshot" ro) != "ro=true" ]]
;;
create-config)
shift
mkdir -p "$1"
;;
create-snapshot)
shift
mkdir -p "$1"/"$2"
;;
receive-info)
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)
shift
btrfs receive "$1/$2"
;;
send-info)
shift
cat "$1/$2/info.xml"
;;
send-snapshot)
shift
btrfs send "$1/$2/snapshot"
;;
send-incremental-snapshot)
shift
btrfs send -p "$1/snapshot" "$2/snapshot"
;;
remove-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)
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)
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 "$dir/snapshot" && ! -h "$dir" ]]; then
snapshots+=("$dir")
fi
done
if ! [ ${#snapshots[@]} -eq 0 ]; then
ln -sfn "${snapshots[-1]}" "$1/latest"
fi
;;
test-connection)
exit 0
;;
--version)
echo -e "$version"
exit 0
;;
*)
error "Unrecognized command, bailing out!"
;;
esac