-
-
Save finloop/0dc136cecb5d92a11bba0b3dbfa14955 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Forked from: https://gist.github.com/Nitrousoxide/43ed81840c542965b76c08c60b69b1da | |
| # 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" | |
| RUNNING_CONTAINERS=$(podman ps -q) | |
| podman pause $RUNNING_CONTAINERS | |
| # 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 $RUNNING_CONTAINERS | |
| # 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