-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-snapshots.sh
executable file
·112 lines (100 loc) · 2.99 KB
/
remove-snapshots.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
#!/bin/bash
set -u
safe_source () { [[ ! -z ${1:-} ]] && source $1; _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; _sdir=$(dirname "$(readlink -f "$0")"); }; safe_source
show_help(){
cat <<HELP
$(basename $0) /path/to/snapshots options
Options:
--before [TIMESTAMP] : Remove all snapshots before that timestamp
(Omit the TIMESTAMP to get the list of
available timestamps)
--preserve-last N : Remove all snapshots except for this amount
of latest snapshots
--apply : No dry-run, actually apply.
HELP
}
die(){
>&2 echo
>&2 echo "$@"
>&2 echo
exit 1
}
help_die(){
>&2 echo
>&2 echo "$@"
show_help
exit 1
}
# Parse command line arguments
# ---------------------------
# Initialize parameters
limit_string=
dry_run=true
help_needed=false
# ---------------------------
args_backup=("$@")
args=()
_count=1
while [ $# -gt 0 ]; do
key="${1:-}"
case $key in
-h|-\?|--help|'')
show_help # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
--before) shift
[[ -n ${1:-} ]] && limit_string="--after $1" || help_needed=true
;;
--preserve-last) shift
limit_string="--last $1"
;;
--apply)
dry_run=false
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
help_die "Unknown option: $1"
;;
*) # Generate the new positional arguments: $arg1, $arg2, ... and ${args[@]}
if [[ ! -z ${1:-} ]]; then
declare arg$((_count++))="$1"
args+=("$1")
fi
;;
esac
[[ -z ${1:-} ]] && break || shift
done; set -- "${args_backup[@]}"
# Use $arg1 in place of $1, $arg2 in place of $2 and so on,
# "$@" is in the original state,
# use ${args[@]} for new positional arguments
[[ -n ${arg1:-} ]] && snapshots=$arg1 || die "Snapshot path is required."
if $help_needed; then
echo "Please provide a timestamp:"
echo "---------------------------"
$_sdir/list-backup-dates.sh $snapshots
exit 1
fi
[[ -n ${limit_string:-} ]] || die '"--before" or "--preserve-last NUM" is required.'
readarray -t to_be_saved < <($_sdir/list-backup-dates.sh $snapshots $limit_string)
for s in ${to_be_saved[@]}; do
echo "Will be saved: $s"
done
for snap in `$_sdir/btrfs-ls $snapshots`; do
save=false
for del in "${to_be_saved[@]}"; do
if echo "$snap" | grep -q "\.$del"; then
save=true
break
fi
done
if $save; then
echo "to be saved: $snap"
else
echo "REMOVE: $snap"
[[ $dry_run == false ]] && sudo btrfs sub del "$snap"
fi
done
if $dry_run; then
echo "This was a dry run. Provide --apply switch if you want to apply."
fi