Backing up WordPress from the Command Line

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:

  • (c)ompress
  • use (v)erbose out
  • use (g)unzip
  • and all this done on the (f)ile

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

 

Tagged in : Tagged in : , ,
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.