-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-volumes.sh
executable file
·67 lines (56 loc) · 1.71 KB
/
backup-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
#!/bin/bash
set -e
# Usage: backup-volumes.sh [-o|--output <backups-directory>]
OUTPUT_PATH=$PWD
while [[ $# -gt 0 ]]; do
case $1 in
-o|--output)
OUTPUT_PATH="$2"
shift # past argument
shift # past value
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done
#IFS=$'\n' containers_to_backup=( $(podman ps -q) )
IFS=$'\n' containers_to_backup=( $(podman ps -q --filter label=dev.cwmr.volumes-to-backup) )
# Make sure destination path exists
mkdir -p $OUTPUT_PATH
for container in "${containers_to_backup[@]}"
do
echo -e "---\nBacking up 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 exporting
echo "Stopping $container..."
{
echo "Trying via systemd $service..."
systemctl --user stop $service
} || {
echo "Systemd failed. Trying via podman..."
podman stop $container
}
echo "Stopped $container."
# Backup the volumes
{
for volume in "${volumes_to_backup[@]}"
do
echo "Exporting $volume..."
podman volume export $volume | gzip > $OUTPUT_PATH/$volume.tar.gz
echo "Exported $volume to $OUTPUT_PATH/$volume.tar.gz."
done
} || echo "Failed to backup 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."
done