-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-backup-dates.sh
executable file
·93 lines (84 loc) · 2.64 KB
/
list-backup-dates.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
#!/bin/bash
set -eu
safe_source () { [[ ! -z ${1:-} ]] && source $1; _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; _sdir=$(dirname "$(readlink -f "$0")"); }; safe_source
# end of bash boilerplate
# magic variables
# $_dir : this script's (or softlink's) directory
# $_sdir : this script's real file's directory
show_help(){
cat <<HELP
$(basename $0) [options] /path/to/snapshots
Options:
--before TIMESTAMP : List before that timestamp (YYYYmmddTHHMM)
(exclusive)
--after TIMESTAMP : List after that timestamp
(inclusive)
--last N : Number of latest snapshots
HELP
}
die(){
>&2 echo
>&2 echo "$@"
exit 1
}
help_die(){
>&2 echo
>&2 echo "$@"
show_help
exit 1
}
# Parse command line arguments
# ---------------------------
# Initialize parameters
before_ts=
after_ts=
last=
# ---------------------------
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
before_ts=$1
;;
--after) shift
after_ts=$1
;;
--last) shift
last=$1
;;
# --------------------------------------------------------
-*) # 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."
counter=0
for i in `ls $snapshots | sort -n -r`; do
[[ $i =~ \.[0-9]{8}T[0-9]{4}.* ]] || continue
"$_sdir/is-btrfs-subvolume-ro" "$snapshots/$i" > /dev/null || continue
base=$(basename $i)
timestamp=$(echo $base | grep -oE -- '[0-9]{8}T[0-9]{4}_?[0-9]?')
[[ -n ${before_ts:-} ]] && { [[ $timestamp < $before_ts ]] || continue; }
[[ -n ${after_ts:-} ]] && { [[ $timestamp == $after_ts || $timestamp > $after_ts ]] || continue; }
[[ -n ${last:-} ]] && { [[ $((counter++)) -lt $last ]] || break; }
echo $base | grep -oE -- '[0-9]{8}T[0-9]{4}.*'
done