TheGoldenAura

WordPress Backup with: Database backup + files backup + remote ftp upload

Saturday, April 13th, 2013

Hard Disk Drive

These last years, I have been using WordPress to create different kind of sites and hosting them on my dedicated server. Despite all kinds of security strategies with data replication on the hosting machine it is an obligation to maintain a good remote backup system.

I have been using different WordPress backup plugins and most of them run fine with small sites. However, it can be really painful to do backups with the available plugins (including both free and commercial ones) when you have big sites, I mean bigger than 8GB data per site. This is just my personal experience, but I have used “commercial paid pro versions” plugins like BackupBuddy, Simple Backup Ultra, BackWPup… and always had problems.

This week, I got a really scary server crash and… I realise I have “insufficient” backups…

So I decided to stop being lazy and to create a very simple shell script to do exactly what I need.

It is a very simple script that backups the WordPress database, backups all site files, creates a tar with all this data, uploads to a remote destination by ftp, keeping backups from the last 30 days. Feel free to reuse it:

 

#!/bin/sh
#
# Script for backup of WP sites (WPsite001, WPsite002, WPsite003…) with mysql database backup, all site files backup and ftp upload to remote destination

 

# Finds files older than 30 days and delete them find /bck/ -type f -mtime +30 -exec rm -f {} \;

 

 

# These lines create the backups folders, dumps mysql databases, copy wordpress files and create a tar

# WPsite001

mkdir /bck/mysql/ mkdir /bck/backup/

mysqldump -umysqluser -pmysqlpassword site001databasename > /bck/mysql/site001databasename.sql

mv /bck/mysql/ /bck/backup/mysql/

cp -r /wpsite001-rootfolder/ /bck/backup/

cd /bck/

tar -cvf backup-wpsite001.$(date +%Y%m%d-%H%M%S).tar /bck/backup/ rm -R /bck/backup/

 

# WPsite002

mkdir /bck/mysql/ mkdir /bck/backup/

mysqldump -umysqluser -pmysqlpassword site002databasename > /bck/mysql/site002databasename.sql

mv /bck/mysql/ /bck/backup/mysql/

cp -r /wpsite001-rootfolder/ /bck/backup/

cd /bck/

tar -cvf backup-wpsite001.$(date +%Y%m%d-%H%M%S).tar /bck/backup/ rm -R /bck/backup/

 

#
# Uploads (resuming) files to FTP server machine
#
ncftpput -u user -p password -R -z remote-adress /remotefolder/ /bck/
#