Backing up my blog
Recently, my Blog was migrated from a standard Apache document root into docker. My main URL is now https://blog.kushal.net
On my VPS, I have not opted for daily snapshot as backup and I was forced to put a local backup on my server for the files and database.
My Bash script to backup the MYSQL database and files on the docker containers:-
The output will be stored in a targz file
need to replace the variables by correct name
#!/bin/bash
#Script to backup MYSQL DB and DocRoot of Site web for docker
#Variables
BACKUPNAME=<NAMEOFBACKUP>
DBCONTAINERNAME=<DB_NAME_OF_CONTAINER>
DBUSERNAME=root
DBPASSWORD=<Password_of_root_DB>
DBNAME=<DBNAME>
WEBVOLNAME=<VolumeNameOf_WEBDATAfiles>
DATE=`date +%d-%m-%y`
BACKUPPATH=/home/backup/$BACKUPNAME-$DATE
LOG=$BACKUPPATH/backup.log
function backupdb {
echo "[`date +%H:%M:%S`] Backing Up DB $DBNAME" >> $LOG
docker exec $DBCONTAINERNAME /usr/bin/mysqldump -u $DBUSERNAME --password=$DBPASSWORD $DBNAME |gzip > $BACKUPPATH/$DBNAME-`date +%H:%M:%S`.sql.gz
if [ $? -eq 0 ]; then
echo "[`date +%H:%M:%S`] Backup Success [OK]" >> $LOG
else
echo "[`date +%H:%M:%S`] Backup failed [KO]" >> $LOG
fi
}
function backupwebvol {
echo "[`date +%H:%M:%S`] Backing Up WEB Volume $WEBVOLNAME" >> $LOG
docker run -t --rm -v $WEBVOLNAME:/var/www/html -v $BACKUPPATH:/backup alpine tar -czvf /backup/backupdocroot-`date +%H:%M:%S`.tar.gz -C /var/www ./
if [ $? -eq 0 ]; then
echo "[`date +%H:%M:%S`] Backup Success [OK]" >> $LOG
else
echo "[`date +%H:%M:%S`] Backup failed [KO]" >> $LOG
fi
}
function main {
mkdir $BACKUPPATH
}
case "$1" in
start|"")
main
backupdb
backupwebvol
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
db)
backupdb
;;
web)
backupwebvol
;;
*)
echo "Usage: backup_BLOG.sh [start|db|web]" >&2
exit 3
;;
esac
I have a daily cron which executes the script and a retention of 1 Month locally on the server.
The backup is also externalized with a synchronisation from my RaspberryPi at home with a daily rsync
The backup is save as follows:
blogkushal-30-11-18
├── backupdocroot-11:42:03.tar.gz
├── backup.log
└── blogkushal-11:42:01.sql.gz