-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore-volumes.sh
executable file
·68 lines (59 loc) · 1.62 KB
/
restore-volumes.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
#!/bin/bash
set -e
# Usage: restore-volumes.sh -c|--container <container-name> [-d|--dir <backups-directory>]
container=""
backups_path=$PWD
while [[ $# -gt 0 ]]; do
case $1 in
-c|--container)
container="$2"
shift # past argument
shift # past value
;;
-d|--dir)
backups_path="$2"
shift # past argument
shift # past value
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done
if test -z "$container"; then
echo "A container must be specified."
exit 1
fi
echo -e "---\nRestoring volumes for $container"
IFS=$' ' volumes_to_backup=( $(podman container inspect --format '{{ join (split (index .Config.Labels "dev.cwmr.volumes-to-backup") ",") " " }}' $container) )
service=$(podman container inspect --format '{{ .Config.Labels.PODMAN_SYSTEMD_UNIT }}' $container)
# Stop the service before importing
echo "Stopping $container..."
{
echo "Trying via systemd $service..."
systemctl --user stop $service
} || {
echo "Systemd failed. Trying via podman..."
podman stop $container
}
echo "Stopped $container."
# Restore the volumes
{
for volume in "${volumes_to_backup[@]}"
do
echo "Importing $volume..."
cat $backups_path/$volume.tar.gz | podman volume import $volume -
echo "Imported $volume from $backups_path/$volume.tar.gz."
done
} || echo "Failed to restore all volumes for $container."
# Restart the service
echo "Restarting $container..."
{
echo "Trying via systemd $service..."
systemctl --user start $service
} || {
echo "Systemd failed. Trying via podman..."
podman start $container
}
echo "Successfully restarted $container."