Skip to content

Instantly share code, notes, and snippets.

@fracz
Created March 3, 2018 12:11
Show Gist options
  • Save fracz/3c58c9981562655a3e36c02315a12014 to your computer and use it in GitHub Desktop.
Save fracz/3c58c9981562655a3e36c02315a12014 to your computer and use it in GitHub Desktop.
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"
@makrz
Copy link

makrz commented Apr 6, 2022

There is an error on line 23. Should:
" ((' ${GDRIVE_DIR} ' w rodzicach) i trasshed=false i updatedTime < ' $OLD_BACKUPS ') " \
Greetings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment