Last active
September 25, 2024 23:43
-
-
Save apizz/41c2fe8affe64d004734aa0de22cfd57 to your computer and use it in GitHub Desktop.
Backup a specified Snipe-IT Docker container and cleanup any old backups
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 | |
# | |
# Script for running automated backups for Snipe-IT Docker containers and removing old backups | |
# | |
# Mean to be used as part of a crontab | |
# | |
# Limits its search for backups to clean up to those in the 'BACKUP_DIR' folder, so | |
# you can create folders in this location to keep any manual backups for historical purposes | |
# | |
# Docker container name to backup | |
CONTAINER="${1}" | |
# Snipe-IT Docker container backup location | |
BACKUP_DIR="/var/www/html/storage/app/backups/" | |
# Number of backups to keep | |
MAX_BACKUPS="14" | |
# Verify a container name is supplied | |
if [ "$CONTAINER" = "" ]; then | |
/bin/echo "No value supplied for 'CONTAINER'. Please run the script followed by the container name. ex. sh script.sh <container_name>" | |
exit 1 | |
fi | |
# First, complete a backup | |
/bin/echo "Creating database backup for ${CONTAINER} ..." | |
docker exec "$CONTAINER" /usr/bin/php /var/www/html/artisan snipeit:backup | |
# Process existing backups for cleanup | |
BACKUPS=$(docker exec "$CONTAINER" /usr/bin/find "$BACKUP_DIR" -maxdepth 1 -type f | /usr/bin/sort -r) | |
BACKUP_NUM=$((${MAX_BACKUPS} + 1)) | |
OLD_BACKUPS=$(echo $BACKUPS | tail -n +${BACKUP_NUM}) | |
# If old backups found, remove them | |
if [ "$OLD_BACKUPS" != "" ]; then | |
echo "Cleaning up old backups ..." | |
for f in $OLD_BACKUPS; do | |
echo "Removing old backup: ${f} ..." | |
docker exec "$CONTAINER" rm $f | |
done | |
else | |
echo "No backups to clean. Done." | |
fi | |
exit |
Hi there,
thanks for providing this backup script :-)
Working on Debian Bullseye I faced exactly the same problem as @grimurd described (OLD_BACKUPS variable is always empty). While not being a Bash guy I fiddled around and found a working solution. Instead of using (echo $BACKUPS | tail -n +${BACKUP_NUM})
I changed it to (echo "$BACKUPS" | tail -n +${BACKUP_NUM})
. With this change "$BACKUPS"
, the backup script works now flawlessly in my setup.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this is old, but what about the SQL container? Does that need to be done?