Skip to content

Instantly share code, notes, and snippets.

@pujianto
Last active May 20, 2020 02:36
Show Gist options
  • Save pujianto/18126f910d3d58e5c326c7122d2e2d2e to your computer and use it in GitHub Desktop.
Save pujianto/18126f910d3d58e5c326c7122d2e2d2e to your computer and use it in GitHub Desktop.
Offload mysql databases & website backup files to S3 Server
#!/bin/sh
LOCAL_BACKUP_FOLDER="/home/user/backup"
S3_BACKUP_FOLDER="s3://bucket-name/backup_folder/"
WEBSITE_PATH="/var/www"
WEBSITE_BACKUP_NAME="mywebsites.tar.gz"
printf "\nCREATING BACKUP ARCHIVE FROM ${WEBSITE_PATH}\n\n"
tar -czf "${LOCAL_BACKUP_FOLDER}/${WEBSITE_BACKUP_NAME}" -C "${WEBSITE_PATH}" .
printf "\nCREATING MYSQL BACKUP ARCHIVE (ALL DATABASES)\n\n"
DATABASES=$(echo "show databases" | mysql | grep -Ev "^(Database|mysql|performance_schema|information_schema|sys)$")
FILENAME="full_backup.$(date +%F.%H%M%S).sql.gz"
/usr/bin/mysqldump --databases ${DATABASES} | gzip > ${LOCAL_BACKUP_FOLDER}/${FILENAME}
# sync with s3 & delete old backup (files that not exist in our local backup folder)
/usr/bin/s3cmd sync --delete-removed ${LOCAL_BACKUP_FOLDER} ${S3_BACKUP_FOLDER}
# remove local backup folder's content if necessary
# rm -rv ${LOCAL_BACKUP_FOLDER}/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment