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 backup of wordpress project files & db:
(If you want backup script for Magento then please go here)
#!/bin/bash #@author MagePsycho <magepsycho@gmail.com> #@website http://www.magepsycho.com #@blog http://www.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 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?
1. Gives options for backup type which are:
- DB backup only
- Files backup only
- Files backup with DB
2. Dumps the database by taking db info from wp-config.php
3. Makes a copy of project files and compresses it in .tar.gz format.
4. Deletes the dumped SQL file as it's already copied in compressed file.
5. Creates backup dir if not exists
6. 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 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
Clik here to view.

DB Backup Only
Clik here to view.

File Backup Only
Clik here to view.

Files Backup with DB
Thanks for reading & sharing.
The post Backup WordPress project files / db using bash script appeared first on MagePsycho's Magento Blog & more.