WordPress is undoubtedly the most popular blogging platform on the Internet. And when it comes to the maintenance or regular backups, life is not easier unless you have some backup script which works great via SSH & cron daemons.
Here is the custom backup script used for taking the backup of WordPress project files & DB:
(If you want a backup script for Magento then please go here)
#!/bin/bash
#@author MagePsycho <[email protected]>
#@website https://www.magepsycho.com
#@blog https://blog.magepsycho.com
#@version 0.1.0
#/************************ EDIT VARIABLES ************************/
projectName=magepsycho_blog
backupDir=/home/magepsycho/_backups
#/************************ //EDIT VARIABLES **********************/
fileName=$projectName-$(date +"%Y-%m-%d")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)
printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType
if [[ $backupType = @(d|b) ]]; then
echo "----------------------------------------------------"
echo "Dumping MySQL..."
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
echo "Done!"
fi
if [[ $backupType = @(f|b) ]]; then
echo "----------------------------------------------------"
echo "Archiving Files..."
tar -zcf $fileName.tar.gz * .htaccess
echo "Done!"
echo "----------------------------------------------------"
echo "Cleaning..."
rm -f $fileName.sql.gz
echo "Done!"
fi
if [[ $backupType = @(d|f|b) ]]; then
echo "----------------------------------------------------"
mkdir -p $backupDir;
echo "Moving file to backup dir..."
if [[ $backupType == d ]]; then
mv $fileName.sql.gz $backupDir
fi
if [[ $backupType = @(f|b) ]]; then
mv $fileName.tar.gz $backupDir
fi
echo "Done!"
else
echo "Invalid selection!"
fi
Or you can download it from here: [WordPress Backup Script]
Notes: If you get the following error:
syntax error in conditional expression: unexpected token `(‘
line 20: syntax error near `@(d’
line 20: `if [[ $backupType = @(d|b) ]]; then’
then this means you are using an older version of bash (< 4.0). And you need to patch the script by adding the following line after bash script declaration:
shopt -s extglob
What does this script do?
- Gives options for different types of backup:
- DB backup only
- Files backup only
- Files backup with DB
- Dumps the database by taking DB info from wp-config.php
- Makes a copy of project files and compresses it in .tar.gz format.
- Deletes the dumped SQL file as it’s already copied in the compressed file.
- Creates backup dir if not exists
- Copies the compressed project file to the backup dir.
Besides, you can also setup cron job to run this backup script at regular intervals.
How to run the backup script?
1. Edit _wpbackup.sh to configure variables ‘projectName’ & ‘backupDir’
2. Upload the edited _wpbackup.sh to the root of your WordPress installation
3. Run following series of commands in terminal:
cd /path/to/wordpress/root
chmod +x _wpbackup.sh
ex -sc %s/\r$//e|x' _wpbackup.sh
sh _wpbackup.sh
4. You will get the compressed backup file at backup dir
Snapshots for different backup types
Thanks for reading & sharing.