-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfs-ls
executable file
·97 lines (86 loc) · 2.43 KB
/
btrfs-ls
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
#!/bin/bash
safe_source () { [[ ! -z ${1:-} ]] && source $1; _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; _sdir=$(dirname "$(readlink -f "$0")"); }; safe_source
# show help
show_help(){
cat <<HELP
$(basename $0) [options] path/to/dir[/]
Notice: The "/" character at the end works like in rsync.
Options:
--ro, --only-ro : List only readonly snapshots
--rw, --only-rw : List only read-write snapshots
--relative : Print relative path
HELP
exit
}
die(){
echo
echo "ERROR: $@"
echo
exit 1
}
# Parse command line arguments
# ---------------------------
# Initialize parameters
only_ro=false
only_rw=false
relative=false
# ---------------------------
args_backup=("$@")
args=()
_count=1
while :; do
key="${1:-}"
case $key in
-h|-\?|--help|'')
show_help # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
--only-ro|--ro)
only_ro=true
;;
--only-rw|--rw)
only_rw=true
;;
--relative)
relative=true
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
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
shift
[[ -z ${1:-} ]] && break
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
errcho () { >&2 echo -e "$@"; }
[[ $(whoami) = "root" ]] || { sudo $0 "$@"; exit 0; }
filter=''
[[ $only_ro = true ]] && filter='if($3 == "readonly")'
[[ $only_rw = true ]] && filter='if($3 == "-")'
while read i; do
[[ -z $i ]] && continue
if [[ $arg1 =~ ^.*/$ ]] && [[ $i/ =~ ^$arg1$ ]]; then
errcho "INFO: Skipping $i"
continue
else
if $relative; then
[[ "$arg1" != "$i" ]] && realpath --relative-to="$arg1" "$i"
else
echo "$i"
fi
fi
done <<< $($_sdir/btrbk ls "$arg1" \
| sed -n '1!p' \
| awk '{'"$filter"' print}' \
| awk '{$1=""; $2=""; $3=""; print}' \
| sort -r)