How to Backup and Restore Wordpress Without Any Plugins

Recipe # | posted in Howto | Comments

1 – Problem Description

How to backup a remote Wordpress installation in your local pc and restore it in case of disaster! All this without any plugin, service, paypalware, gurulike custom application!!! :P

2 – Solution

Suppose you have a Wordpress installation at www.myblog.com, with the database at db.myblog.com and a table name wp_db to store all Wordpress settings. Both database location db.myblog.com and database table wp_db, can be found in the control panel provided by your hosting company.

Enable ssh using the panel and then login to the remote server where your site is located. To avoid the login screen in each new connection follow this post SSH login to remote PCs without typing your password.

Let’s say that your user name is sexy and the name for the remote server is beast

slogin sexy@beast

Check if git application is installed on the server

git --version

if not, you should backup your database as shown in the step below and use ftp or secure ftp (sftp) to copy the folder containing your worpress installation (i.e. myblog) somewhere in your local pc.

If git is available, go to the wordpress installation folder

cd myblog

Backup wordpress db

mysqldump -u sexy --password='mypass' wp_db --host=db.myblog.com > myblog_db.sql

And let git manage your files

git init
git add .
git add .htaccess 
git commit -am "First commit"

Now, install git in your local pc, go to the folder where you want to store the backup

cd my_backups

and let git do the magic :D

git clone ssh://sexy@beast/~/myblog myblog

From now on when you want to make a backup you just execute in your local pc

cd my_backups/myblog
ssh sexy@beast 'cd myblog; mysqldump -u sexy --password='mypass' wp_db --host=db.myblog.com > myblog_db.sql; git add .; git commit -am "Backup"'
git pull origin

and git will bring all new files in your local pc.

In case of disaster you can restore your super-duper blog by copying back the files from your local pc to the remote server

cd my_backups
scp -r  myblog sexy@beast:~/

Login to remote server and restore db

slogin sexy@beast
cd myblog
mysql -u sexy -p -h db.myblog.com wp_db < myblog_db.sql

That’s all guys and gals ;)

Comments