Skip to content

Instantly share code, notes, and snippets.

@Nitrousoxide
Last active November 20, 2024 22:23
Show Gist options
  • Save Nitrousoxide/43ed81840c542965b76c08c60b69b1da to your computer and use it in GitHub Desktop.
Save Nitrousoxide/43ed81840c542965b76c08c60b69b1da to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get a list of all volumes
volumes=$(podman volume ls -q)
#get timestamp for unique filename
timestamp=$(date +"%Y%m%d%H%M%S")
# Pause all running containers
echo "Pausing containers for backup"
podman pause $(podman ps -q)
# Iterate over the volumes
for volume in $volumes
do
#backup the listed vols
echo "Backing up ${volume}..."
podman volume export ${volume} --output ${volume}.tar
# Check if the podman volume export was successful
if [ $? -ne 0 ]; then
echo "An error occurred while exporting podman volume ${volume}"
exit 1
fi
done
# Unpause all paused containers
echo "Resuming containers"
podman unpause $(podman ps -aq)
# Archive and compress all .tar files that do not match the pattern podmanbackup_*.tar.gz
echo "Combining volume archives into compressed tar..."
tar cvzf podmanbackup_${timestamp}.tar.gz --exclude='podmanbackup_*.tar.gz' *.tar
# Check if the tar command was successful
if [ $? -eq 0 ]; then
# If the tar command was successful, remove the .tar files that do not match the pattern podmanbackup_*.tar.gz
echo "Archiving successful, removing individual .tar files..."
find . -maxdepth 1 -type f -name "*.tar" ! -name "podmanbackup_*.tar.gz" -exec rm -f {} \;
else
# If the tar command failed, print an error message
echo "An error occurred during archiving, no files have been deleted."
exit 1
fi
# Remove .tar.gz files older than a month that do not match the pattern podmanbackup_*.tar.gz
echo "Removing .tar.gz files older than a month..."
find . -maxdepth 1 -type f -name "*.tar.gz" ! -name "podmanbackup_*.tar.gz" -mtime +30 -exec rm -f {} \;
# Check if the old files deletion was successful
if [ $? -ne 0 ]; then
echo "An error occurred while deleting old .tar.gz files."
exit 1
fi
echo "Backup Complete"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment