Skip to content

Instantly share code, notes, and snippets.

@mmontuori
Created May 13, 2022 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmontuori/27505b4b5d90484347f0351869949989 to your computer and use it in GitHub Desktop.
Save mmontuori/27505b4b5d90484347f0351869949989 to your computer and use it in GitHub Desktop.
This gist contains a bash script which backs up all volumes found in docker volume list except the auto generated ones. The backup is executed in the /opt/backup directory, but it can be customized as needed together with other variables.
#!/bin/bash
volumes=`docker volume list | grep -v DRIVER | awk '{ print $2 }'`
backup_dir="/opt/backup"
volumes_to_backup=""
backup_retention_dyas="10"
for volume in $volumes; do
name_length=`echo $volume | wc -m`
if [ "$name_length" == "65" ]; then
continue
fi
volumes_to_backup="$volumes_to_backup $volume"
done
for volume_to_backup in $volumes_to_backup; do
echo "backing up $volume_to_backup -> ${backup_dir}..."
docker run -v ${volume_to_backup}:/volume -v /opt/backup:/backup --rm loomchild/volume-backup backup ${volume_to_backup}-`date +%Y%m%d`
done
echo "cleaning old files..."
find ${backup_dir} -mtime +${backup_retention_dyas}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment