Last active
July 6, 2023 13:01
-
-
Save sdebacker/2650421d36345a45172b752adbb503ec to your computer and use it in GitHub Desktop.
Backup script for Laravel Forge provisioned servers
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/sh | |
# Configuration: | |
# mysql_config_editor set --login-path=local --host=localhost --user=forge --password | |
# aws configure | |
S3_MYSQL_BUCKET=server-name-mysql | |
S3_FILES_BUCKET=server-name-files | |
DATESTAMP=$(date +"%F") | |
DAY=$(date +"%d") | |
DAYOFWEEK=$(date +"%A") | |
PERIOD=day | |
if [ $DAY = "01" ]; then | |
PERIOD=month | |
elif [ $DAYOFWEEK = "Sunday" ]; then | |
PERIOD=week | |
fi | |
echo "Streaming backups to storage (Selected period: $PERIOD)..." | |
DATABASES=`mysql --login-path=local --execute="SHOW DATABASES;" | grep -Ev "(Database|information_schema)"` | |
echo "Delete “previous_$PERIOD” folder..." | |
aws s3 rm s3://$S3_MYSQL_BUCKET/previous_$PERIOD --recursive | |
echo "Move “$PERIOD” folder to “previous_$PERIOD”..." | |
aws s3 mv s3://$S3_MYSQL_BUCKET/$PERIOD/ s3://$S3_MYSQL_BUCKET/previous_$PERIOD/ --recursive | |
for DATABASE in $DATABASES; do | |
if [ $DATABASE != "performance_schema" ]&&[ $DATABASE != "mysql" ]&&[ $DATABASE != "sys" ];then | |
ARCHIVE_NAME="$DATABASE-$DATESTAMP.sql.gz" | |
echo "Dump database “$DATABASE”..." | |
mysqldump \ | |
--login-path=local \ | |
--single-transaction \ | |
--skip-lock-tables \ | |
--databases $DATABASE | \ | |
gzip -c | \ | |
aws s3 cp - s3://$S3_MYSQL_BUCKET/$PERIOD/$ARCHIVE_NAME | |
fi | |
done | |
echo "Syncing files..." | |
aws s3 sync --exclude="*" --include="*/storage/app/*" --storage-class STANDARD_IA ~/ s3://$S3_FILES_BUCKET/ | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment