Simple backup script written in bash, for WordPress on Linux, that will dump MySQL database used by WordPress site and create tar.gz file consisting of WordPress site files and database dump
#!/bin/bash BKPDIR=/local/backup/directory WEBROOT=/wordpress/install/directory/ DBUSER=$(grep DB_USER $WEBROOT/wp-config.php | awk -F\' '{print$4}') DBNAME=$(grep DB_NAME $WEBROOT/wp-config.php | awk -F\' '{print$4}') DBPASSWORD=$(grep DB_PASSWORD $WEBROOT/wp-config.php | awk -F\' '{print$4}') DBDUMP="$BKPDIR""$DBNAME"_$(date +"%Y-%m-%d-%H-%M").sql #In case you want to rsync backups to remote server RUSER=remoteuser RHOST=remoteserver RDIR=/remote/backup/directory/ RSSHPORT=22 mysqldump -u $DBUSER -p$DBPASSWORD $DBNAME > $DBDUMP tar -czvf "$BKPDIR"wpbackup_$(date +"%Y-%m-%d_%H-%M").tar.gz $WEBROOT $DBDUMP rsync -az $BKPDIR -e "ssh -p $RSSHPORT" $RUSER@$RHOST:$RDIR
It will create two backup files, one for database dump in format that looks like “dbname_2018-01-04-06-00.sql” and one tar.gz file that will look like “wpbackup_2018-01-04_06-00.tar.gz”.
Script can be saved as wpbackup.sh and put in crontab to run at a certain time like 6:00AM.
0 6 * * * /path/to/script/wpbackup.sh 2>/dev/null
It can also be run manually with bash wpbackup.sh
Hello,
The script was awesome, saved my ass
can you please add if the backup failed , send the admin a mail that the the backup was failed
I added email sending to script, you can see the new post with new script here
https://srvfail.com/wordpress-backup-email-on-failed-backups-delete-old-backups/