Hi all,
I just wanted to post a script I wrote to backup the individual site files and SQL databases from v4, in case it will help anybody.
Background/Notes/Considerations:
-
I wanted all the site files in one big zip file, along with each individual site’s database exported to a .sql file. I find this to be the best way to keep a restorable copy of the full site, for emergencies.
-
On my system, I put the backups in /var/Backups, then use azcopy to upload them to an Azure Blob storage.
-
I keep the clock on my server set to GMT, but I want the filename to show the local time that the backup was made. you can adjust this timezone by changing the timezone that is defined.
-
I put this script into a file in /usr/local/sbin/ called Backup.sh, did a
chmod +x
on it, and then run it from cron as root. -
If you want to send it to Azure, update the last line with your own info. If you want to do something else with the backups, adjust/remove accordingly. Also - The removal of these backups from Azure is not handled here, so you’ll need to address that accordingly (We have another script that purges backups older than x days).
-
I have no error handling in here, This script presumes everything runs correctly
.
I hope it helps someone.
David.
Script:
#!/bin/sh
USER="root"
PASSWORD=`cat /opt/easyengine/services/docker-compose.yml | grep MYSQL_ROOT_PASSWORD | awk -F'=' '{print $2}'`
OUTPUT="/var/Backups"
DATETIME=`TZ=":America/Edmonton" date +%Y%m%d-%H%M`
DOCKERDatabaseID=`docker ps | grep -e 'services_global-db' | cut -c1-12;`
databases=`docker exec $DOCKERDatabaseID bash -c "mysql -h localhost --user=$USER --password=$PASSWORD -e 'show databases;'" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ;
then
#uncomment this next line if you want to know which DB the script is on.
#echo "Dumping database: $db"
sudo docker exec $DOCKERDatabaseID bash -c "/usr/bin/mysqldump -u $USER -p$PASSWORD --databases $db" > $OUTPUT/$db.sql
fi
done
tar -jcf $OUTPUT/DBs-$DATETIME.tar.bz2 $OUTPUT/*.sql
rm -f $OUTPUT/*.sql
tar -jcf $OUTPUT/siteFiles-$DATETIME.tar.bz2 /opt/easyengine/sites/* --dereference
azcopy --source /var/Backups --destination [YOUR BLOB URL HERE] --dest-key [YOUR BLOG KEY HERE] --quiet
.