-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap-backup.sh
executable file
·58 lines (51 loc) · 1.94 KB
/
snap-backup.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
#!/bin/bash
# Function to backup specified snaps
backup_snaps() {
local dest="$1"
shift
local apps=("$@")
# Save specified snaps and copy snapshot files to the destination directory
for app in "${apps[@]}"; do
echo "Action: Stop, save and export snap $app"
if /usr/bin/snap services "$app" | grep -Ew 'active'; then
/usr/bin/snap stop "$app" || { echo "error: failed to stop snap $app." >&2; exit 1; }
fi
/usr/bin/snap save "$app" || { echo "error: failed to save snap $app." >&2; exit 1; }
if /usr/bin/snap services "$app" | grep -Ew 'inactive'; then
/usr/bin/snap start "$app" || { echo "error: failed to start snap $app." >&2; exit 1; }
fi
snapshot_id=$(/usr/bin/snap saved "$app" | awk 'NR==2 {print $1}')
date=$(date +"%Y-%m-%d-%H%M%S")
/usr/bin/snap export-snapshot "$snapshot_id" "$dest/$snapshot_id-$app-$date" || { echo "error: failed to export snapshot $snapshot_id for $app in $dest/$snapshot_id-$app-$date" >&2; exit 1; }
echo "Action: Forget $app:$snapshot_id"
/usr/bin/snap forget "$snapshot_id" || { echo "error: failed to forget snapshot $snapshot_id for $app." >&2; exit 1; }
done
}
# Main function
main() {
local apps=()
local dest=""
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--apps) IFS=',' read -ra apps <<< "$2"; shift ;;
--dest) dest="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Validate arguments
if [ ${#apps[@]} -eq 0 ]; then
echo "error: no apps specified."
exit 1
fi
if [ ! -d "$dest" ]; then
echo "error: invalid destination directory."
exit 1
fi
# Backup snaps
backup_snaps "$dest" "${apps[@]}"
echo "Snaps backup run completed successfully!"
}
# Execute main function with provided arguments
main "$@"