-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap-restore.sh
executable file
·71 lines (57 loc) · 1.93 KB
/
snap-restore.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
#!/bin/bash
# Function to restore specified snaps
restore_snaps() {
local source="$1"
local app="$2"
# Check if source file exists
if [ ! -f "$source" ]; then
echo "Error: Snapshot file '$source' not found."
exit 1
fi
# Install and Stop
/usr/bin/snap install "$app" || { echo "Error: Failed to install snap '$app'." >&2; exit 1; }
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
# Import snapshot
/usr/bin/snap import-snapshot "$source" || { echo "error: failed to import snapshot from '$source'." >&2; exit 1; }
# Check if the snap is available
if ! /usr/bin/snap saved "$app" >/dev/null 2>&1; then
echo "error: snap '$app' snapshot is not available."
exit 1
fi
# Get the snapshot ID
snapshot_id=$(snap saved "$app" | grep -oE '^[0-9]+' | sort -nr | head -n 1)
# Restore the snapshot
/usr/bin/snap restore "$snapshot_id" || { echo "error: failed to restore snapshot '$snapshot_id'." >&2; exit 1; }
# Forget the snapshot
/usr/bin/snap forget "$snapshot_id" || { echo "error: failed to forget snapshot '$snapshot_id'." >&2; exit 1; }
echo "Snap '$app','$snapshot_id' restored successfully."
}
# Main function
main() {
local app=""
local source=""
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--app) app="$2"; shift ;;
--source) source="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Validate arguments
if [ -z "$app" ]; then
echo "Error: No app specified."
exit 1
fi
if [ -z "$source" ]; then
echo "Error: No source specified."
exit 1
fi
# Restore snaps
restore_snaps "$source" "$app"
}
# Execute main function with provided arguments
main "$@"