-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup
executable file
·52 lines (38 loc) · 1.06 KB
/
cleanup
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
#!/bin/bash
# © 2015–2016 Sebastián González
# http://www.apache.org/licenses/LICENSE-2.0
set -o errexit
this="$(realpath "$0")"
lib="$(dirname "$this")"
script="$(basename "$this")"
source "$lib/common.sh"
source "$lib/$script.sh"
usage() {
cat >&2 <<EOF
Usage: $script <pool>
Removes old backups from snapshot pool <pool>.
Example: $script /var/backup/home
EOF
exit 1
}
(( $# == 1 )) || usage
pool="$1"
use-pool "$pool"
delete-subvolumes() {
while read subvolume; do
local path="$pool/$subvolume"
btrfs subvolume delete "$path" > /dev/null
echo "Removed subvolume $path"
done
}
#---[ Cleanup tasks ]---
# Delete empty subvolumes (usually created for testing).
find "$pool" -maxdepth 1 -type d -empty -print0 | \
xargs -0 -n 1 -r btrfs subvolume delete | \
sed -e "s/Delete subvolume[^:]*:[^']*'\([^']*\)'/Removed empty subvolume \1/g"
# Run cleanup filters.
if [ -d "$lib"/cleanup.d ]; then
for filter in "$lib"/cleanup.d/*.sh; do
subvolume-names "$pool" | (source "$filter") | delete-subvolumes
done
fi