Skip to content

Instantly share code, notes, and snippets.

@fracz fracz/backup.sh
Created Mar 3, 2018

Embed
What would you like to do?
Automatic dockerized SUPLA MySQL database backup to Google Drive
#!/usr/bin/env bash
# Sample crontab: 0 */2 * * * /home/unicorn/backup.sh > /home/unicorn/backup.log 2>&1
NOW=$(date +"%Y%m%d%H%M%S")
BACKUP_FILE=supla-$NOW.sql
GDRIVE_DIR=0B0OnuB36CtoeknVRX2xRbS1sT1E
SUPLA_PATH=/var/www/supla-docker
BACKUP_EXPIRATION_DAYS=7
cd "$(dirname "$0")"
source $SUPLA_PATH/docker/.env && \
docker exec supla-db mysqldump -u root --password="$DB_PASSWORD" supla > $BACKUP_FILE && \
gzip $BACKUP_FILE && \
gdrive upload $BACKUP_FILE.gz --parent $GDRIVE_DIR --delete
sleep 5
OLD_BACKUPS=$(date -u -d "${BACKUP_EXPIRATION_DAYS} days ago" +"%Y-%m-%dT%H:%M:%SZ")
OBSOLETE_BACKUPS=$(gdrive list -m 1000 --query \
"(('${GDRIVE_DIR}' in parents) and trashed=false and modifiedTime < '$OLD_BACKUPS'" \
--order "modifiedTime asc" --no-header | cut -d ' ' -f1)
while read -r OBSOLETE_BACKUP_ID; do
if [ ! -z "$OBSOLETE_BACKUP_ID" ]; then
echo Deleting $OBSOLETE_BACKUP_ID
gdrive delete $OBSOLETE_BACKUP_ID
fi
done <<< "$OBSOLETE_BACKUPS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.