-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot
executable file
·205 lines (189 loc) · 6.92 KB
/
snapshot
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
########################################################################
# #
# This script creates and deletes snapshots of the home directory. #
# The snapshots are stored in directory ~/.snapshots. The file system #
# must be btrfs and the home directory needs to be a btrfs subvolume. #
# Script home2subvolume can be used to turn a home directory on btrfs #
# into a subvolume. Both regular home directories and those encrypted #
# with ecryptfs are supported. #
# #
# Usage: #
# snapshot [-s|--subdirectory subdir] [name] #
# Creates snapshot in optional subdirectory. When name is #
# not given, a name is generated based on the current date #
# and time. #
# #
# snapshot -d|--delete [-s|--subdirectory subdir] name ... #
# Deletes snapshot(s). Btrfs subvolumes can only be #
# deleted by root. sudo is invoked to delete snapshots. #
# #
########################################################################
function usage {
echo "Usage:" >&2
echo " $(basename $0) [-s|-subdirectory subdir] [name]" >&2
echo " Creates new snapshot of home directory; name optional." >&2
echo " $(basename $0) -d|--delete [-s|-subdirectory subdir] name ..." >&2
echo " Deletes snapshot(s) of home directory; name(s) required." >&2
exit 1
}
# Command-line argument checks.
args=$(getopt -o ds: --long delete,subdirectory: -- "$@")
if [[ $? -ne 0 ]]; then
usage
fi
delete=0
subdir=
eval set -- "$args"
for true; do
case "$1" in
-d | --delete) delete=1 ; shift ;;
-s | --subdirectory) subdir="$2" ; shift 2 ;;
--) shift ; break ;;
esac
done
if [[ $delete == 1 ]]; then
if [[ $# -eq 0 ]]; then
usage
fi
elif [[ $# -gt 1 ]]; then
usage
fi
if [[ -n "$subdir" ]] && [[ $(basename "$subdir") != "$subdir" ]]; then
echo "Invalid subdirectory: \"$subdir\" must not include directory." >&2
exit 1
fi
# Filesystem checks.
fs=$(stat -f -c %T $HOME)
if [[ $fs == ecryptfs ]]; then
ENC_HOME=$(dirname $HOME)/.ecryptfs/$USER/.Private
fs=$(stat -f -c %T $ENC_HOME)
if [[ $fs != btrfs ]]; then
echo "Wrong filesystem: home directory is ecryptfs on $fs, not btrfs."\
>&2
exit 1
elif [[ $(stat -c %i $ENC_HOME) -ne 256 ]]; then
echo "Not a subvolume: $ENC_HOME is not a btrfs subvolume." >&2
exit 1
fi
elif [[ $fs != btrfs ]]; then
echo "Wrong filesystem: home directory is on $fs, not btrfs." >&2
exit 1
elif [[ $(stat -c %i $HOME) -ne 256 ]]; then
echo "Not a subvolume: home directory is not a btrfs subvolume." >&2
exit 1
fi
SNAPSHOTS=$HOME/.snapshots
MODE=700 # mode for snapshots
enc_path=
function encrypted_path {
# recursive function, parameter: $1 = decrypted path
# result assigned to enc_path (not returned via echo)
if [[ "$1" == "$HOME" ]]; then
enc_path="$ENC_HOME"
elif encrypted_path $(dirname "$1"); then
enc_dir="$enc_path"
inode=$(stat -c %i "$1")
if [[ $inode -eq 256 ]]; then
echo "Error: snapshot parent $1 cannot be a subvolume." >&2
enc_path=
return 1
fi
enc_path="$(find $enc_dir -mindepth 1 -maxdepth 1 -inum $inode -print)"
if [[ -z "$enc_path" ]]; then
echo "Error: failed to lookup encrypted $1." >&2
return 1
fi
else
return 1
fi
}
if [[ $delete == 0 ]]; then
#
# create snapshot
#
if [[ $# -eq 1 ]] && [[ $(basename "$1") != "$1" ]]; then
echo "Invalid snapshot: name $1 cannot include directory." >&2
exit 1
fi
# Create directory ~/.snapshots if it does not already exist.
if [[ ! -d $SNAPSHOTS ]]; then
mkdir --mode $MODE $SNAPSHOTS
fi
SNAPSHOT_PATH=$SNAPSHOTS
if [[ -n "$subdir" ]]; then
SNAPSHOT_PATH=$SNAPSHOTS/"$subdir"
if [[ ! -d "$SNAPSHOT_PATH" ]]; then
mkdir --mode $MODE "$SNAPSHOT_PATH"
fi
fi
if [[ $# -eq 0 ]]; then
SNAPSHOT_PATH=$SNAPSHOT_PATH/$(date +"%Y-%m-%d_%H:%M:%S")
else
SNAPSHOT_PATH=$SNAPSHOT_PATH/"$1"
fi
#
# Make sure that snapshot does not exist already.
#
if [[ -x "$SNAPSHOT_PATH" ]]; then
echo "Error: snapshot $SNAPSHOT_PATH already exists." >&2
exit 1
fi
if [[ ! -z ${ENC_HOME+x} ]]; then
# create temporary directory to look up its encrypted name
mkdir --mode $MODE "$SNAPSHOT_PATH"
if encrypted_path "$SNAPSHOT_PATH"; then
ENC_SNAPSHOT_PATH="$enc_path"
rmdir "$SNAPSHOT_PATH"
# create read-only snapshot
btrfs subvolume snapshot -r $ENC_HOME $ENC_SNAPSHOT_PATH \
> /dev/null && echo "Created snapshot $SNAPSHOT_PATH."
else
rmdir "$SNAPSHOT_PATH"
exit 1
fi
else
btrfs subvolume snapshot -r $HOME "$SNAPSHOT_PATH" > /dev/null && \
echo "Created snapshot $SNAPSHOT_PATH."
fi
else
#
# delete snapshots
#
for snapshot in $*; do
if [[ $(basename "$snapshot") != "$snapshot" ]]; then
echo "Invalid snapshot: name $snapshot cannot include"\
"directory." >&2
exit 1
fi
SNAPSHOT_PATH=$SNAPSHOTS
if [[ -n "$subdir" ]]; then
SNAPSHOT_PATH=$SNAPSHOTS/$subdir
fi
SNAPSHOT_PATH=$SNAPSHOT_PATH/"$snapshot"
if [[ ! -d "$SNAPSHOT_PATH" ]]; then
echo "Not found: $SNAPSHOT_PATH does not exist." >&2
exit 1
elif [[ $(stat -c %i "$SNAPSHOT_PATH") -ne 256 ]]; then
echo "Not a snapshot: $SNAPSHOT_PATH is not a btrfs subvolume." >&2
exit 1
elif [[ ! -z ${ENC_HOME+x} ]]; then
mv "$SNAPSHOT_PATH" "$SNAPSHOT_PATH".renamed
mkdir --mode $MODE "$SNAPSHOT_PATH"
if encrypted_path "$SNAPSHOT_PATH"; then
ENC_SNAPSHOT_PATH="$enc_path"
rmdir "$SNAPSHOT_PATH"
mv "$SNAPSHOT_PATH".renamed "$SNAPSHOT_PATH"
sudo btrfs subvolume delete -c $ENC_SNAPSHOT_PATH \
> /dev/null && echo "Snapshot $SNAPSHOT_PATH deleted."
else
rmdir "$SNAPSHOT_PATH"
mv "$SNAPSHOT_PATH".renamed "$SNAPSHOT_PATH"
exit 1
fi
else
sudo btrfs subvolume delete -c "$SNAPSHOT_PATH" > /dev/null && \
echo "Snapshot $SNAPSHOT_PATH deleted."
fi
done
fi