-
-
Save apizz/41c2fe8affe64d004734aa0de22cfd57 to your computer and use it in GitHub Desktop.
#!/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 |
Creating database backup for snipe-it ...
Starting backup...
Dumping database snipeit...
Determining files to backup...
Zipping 78 files...
Created zip containing 78 files. Size is 2.01 MB
Copying zip to disk named local...
Successfully copied zip to disk named local.
Backup completed!
OLD_BACKUPS=
BACKUPS=/var/www/html/storage/app/backups/snipe-it-2019-09-02-09-09-33.zip
/var/www/html/storage/app/backups/snipe-it-2019-09-02-09-09-16.zip
/var/www/html/storage/app/backups/snipe-it-2019-09-02-00-20-02.zip
/var/www/html/storage/app/backups/snipe-it-2019-09-01-00-20-01.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-31-00-20-02.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-30-00-20-02.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-29-00-20-02.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-51-18.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-46-25.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-44-50.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-44-48.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-44-45.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-44-43.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-14-44-40.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-43-01.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-41-27.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-40-29.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-40-07.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-39-22.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-39-00.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-38-17.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-34-22.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-32-33.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-32-28.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-31-03.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-31-01.zip
/var/www/html/storage/app/backups/snipe-it-2019-08-28-09-30-59.zip
BACKUP_NUM=3
No backups to clean. Done.
@grimurd so it appears something with the OLD_BACKUPS commands / formatting is to blame here (echo $BACKUPS | tail -n +${BACKUP_NUM}
). I'm honestly not sure what's different between our CentOS setups, but I can confirm that I only have 14 backups on all my Snipe-IT instances, and this was both before and after Snipe updated their naming scheme to include "snipe-it" in the ZIP backup filename.
I'd play around with the commands used in OLD_BACKUPS
on your end to get just the backups that you'd want to remove based on your desired # of historical backups. If you get something working, post it here so I can test on my end. It's very possible there's a better way to do this than what I've come up with here.
I know this is old, but what about the SQL container? Does that need to be done?
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.
@grimurd, what that tells me is that OLD_BACKUPS ends up blank. If you can temporarily add some additional lines that echoes the value for the BACKUPS, BACKUP_NUM, and OLD_BACKUPS. That should tell you what's causing that.
I explicitly supply the path to the
find
andsort
binaries in the script, so if those happen to be in different places on your host machine, that may be the issue here.