We often have to back up our website and we do not always want to use plugins or control panel options to do this. Often using the command line is a lot easier and quicker. With the help of your terminal you can ssh into your website and fire a few simple commands to backup your WordPress website files and database. I did write about all this briefly before here, but this post is more detailed and includes database backup examples too.

Backing up files

To backup your files you can use tar. Here is the command I use to backup all and exclude the BackupBuddy backups which I don’t need and which tend to take up way too much space.

tar cvzf  domain.nl-16-06-2017.tar.gz domain.nl --exclude=domain.nl/wp-content/uploads/backupbuddy_backups

Tar starts out details on what should be done, then indicates your destination and finally the source directory. So I first tell tar to:

Then I tell the destination domain.nl-16-06-2017.tar.gz which I tend to call domain.com-day=month-year.tar.gz. And then finally I tell it to grab all from directory domain.nl and exclude all my backupbuddy backups using the exclude command. If I would have added those the backup would have been several 100 MB larger than without! And as I often just want to backup and SFTP to my home box or another server I prefer to keep the backup as small as possible.

Database Backup

We have not backed up the database yet, which is also very important. To do that I use:

mysqldump --opt --user=user --password=password --host=mysql-site.domain.com databasename > domain.com-dd-mm-yy.sql

Afterwards you can tar the whole file to make it smaller too. See above tar command for that.

WP-CLI Database Backup

Now, if you have wp-cli available or can get it installed you can use it for backing up as well. This command will backup the file in the root of your WordPress website:

wp db export

Now you do have to make sure you are in the WordPress project root. Especially if you like me have multiple WordPress installations. Too, using this command creates the file in the same directory with no specifics in the file name. You may prefer to use something like:

wp db export domain.com_db-dd-mm-yy.sql

 

Leave a Reply

Your email address will not be published. Required fields are marked *