How To Quickly Backup/Restore WordPress Blog Using Linux Terminal

It is important to have regular blog backups and WordPress blogs essentially have two components that need to be backed up :

  • The WordPress database (that contains blog posts, comments, tags etc)
  • The images and other stuff like config files (robots.txt, web server config and so on).

Here is how to backup WordPress database named “wordpress” to a directory path /srv/dbdump directly through the Terminal:

sudo mysqldump wordpress -uroot -p > /srv/dbdump/wordpress.sql

Backing up WordPress database through command line

Enter the MySQL root password and the above will directly export the entire WordPress database to the path specified and store it in .sql format.

To restore it back to a database named “wordpress”, use the following command :

mysql -u root -p wordpress < /srv/dbdump/wordpress.sql;

WordPress database restoration through command line

This will import the previously backed up database. [Note the semicolon at the end of the command as this is a MySQL command].

There are useful plugins too that can backup and restore databases easily like WordPress Database Backup but it is always good to know how to do it alternatively and also quickly which is possible directly through commands.

For backing up images and other content, rsync is a quick way to copy the whole directory structure across to a backup server/other machine and then keep it updated.

Suppose a WordPress installation on local machine is at path : /srv/www/blogname/public_html and it is to be backed up/copied to a remote server named otherserver.com at remote path /home/backups/blogname/, then to do that, the command is :

rsync -avz -e  ssh  /srv/www/blogname/public_html/ admin@otherserver.com:/home/backups/blogname/

The above command will connect to remote server (otherserver.com) who accepts SSH connections at default port 22, checks the admin credentials and triggers rsync to copy the whole structure from current server to the remote one. Here is an earlier post that goes in more detail. To restore, just invert the above and provide SSH credentials of the server where it needs to be restored and the path.

The best part of using rsync over ssh is that it can be used to copy over any directory structure to a remote location and then only update the new/changed files and directory structure at the remote end.

Happy backing up.