-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeshift-autosnap
61 lines (45 loc) · 2.56 KB
/
timeshift-autosnap
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
#!/bin/bash
#author: gobonja
[ "$(findmnt / -no fstype)" == "overlay" ] && { echo "==> skipping timeshift-autosnap because system is booted in Live CD mode..."; exit 0; }
[[ -v SKIP_AUTOSNAP ]] && { echo "==> skipping timeshift-autosnap due SKIP_AUTOSNAP environment variable being set."; exit 0; }
readonly CONF_FILE=/etc/timeshift-autosnap.conf
readonly SNAPSHOTS_TO_DELETE=$(mktemp -u --tmpdir "${0##*/}.XXXXXXXX")
readonly SNAPSHOT_NAME_DATE_PATTERN="[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}_[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}"
get_property() {
if [ ! -f $CONF_FILE ]; then
echo "$CONF_FILE not found! Using $1=$3" >&2;
param_value=$3
else
param_value=$(sed '/^\#/d' $CONF_FILE | grep "$1" | tail -n 1 |\
cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if { [ "$2" == "boolean" ] && [ "$param_value" != true ] && [ "$param_value" != false ]; } || \
{ [ "$2" == "integer" ] && [[ ! "$param_value" =~ ^[-+]?([1-9][[:digit:]]*|1)$ ]]; } || \
{ [ "$2" == "string" ] && [ "$param_value" == "" ]; } ; then
echo "Wrong paramater in $CONF_FILE. Using $1=$3" >&2
param_value=$3
fi
fi
echo "$param_value"
}
if $(get_property "skipAutosnap" "boolean" "false") ; then
echo "==> skipping timeshift-autosnap due skipAutosnap in $CONF_FILE set to TRUE." >&2; exit 0;
fi
readonly SNAPSHOT_DESCRIPTION=$(get_property "snapshotDescription" "string" "{timeshift-autosnap} {created before upgrade}")
timeshift --create --comments "$SNAPSHOT_DESCRIPTION${1:+ "$1"}" || { echo "Unable to run timeshift-autosnap! Please close Timeshift and try again. Script will now exit..." >&2; exit 1; }
if $(get_property "deleteSnapshots" "boolean" "true") ; then
timeshift --list > "$SNAPSHOTS_TO_DELETE"
sed -ni "/$SNAPSHOT_DESCRIPTION/p" "$SNAPSHOTS_TO_DELETE"
sed -ni "s/.*\($SNAPSHOT_NAME_DATE_PATTERN\).*/\1/p" "$SNAPSHOTS_TO_DELETE"
count=$(($(sed -n '$=' "$SNAPSHOTS_TO_DELETE")-$(get_property "maxSnapshots" "integer" "3")))
if [ "$count" -gt 0 ] ; then
sed -i ${count}q "$SNAPSHOTS_TO_DELETE"
for snapshot in $(cat "$SNAPSHOTS_TO_DELETE"); do
timeshift --delete --snapshot "$snapshot"
done
fi
fi;
if $(get_property "updateGrub" "boolean" "true") && [ -f /etc/grub.d/41_snapshots-btrfs ]; then
. /etc/default/grub-btrfs/config
if [ -s "${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}/grub-btrfs.cfg" ]; then /etc/grub.d/41_snapshots-btrfs; else ${GRUB_BTRFS_MKCONFIG:-grub-mkconfig} -o ${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}/grub.cfg; fi
fi;
exit 0