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

 

Sage 9 Beta 3 WooCommerce Setup

Since I wrote my last post on setting up WooCommerce in Sage 9 things have changed once again. Sage 9 beta 3 has adjusted its structure and looks even more like a Laravel app. And WooCommerce has also been adjusted and since WooCommerce 3.0 several patches have been added. So let’s discuss the Sage 9 Beta 3 WooCommerce Setup shall we?

Sage Views

There are no longer template files anymore. That is, there are, but they are now all added under /resources/views . Not under templates anymore. Here is the structure now:

site/web/app/themes/sage/resources/views $ tree -L 2
.
├── 404.blade.php
├── archive-product.php
├── index.blade.php
├── layouts
│   └── app.blade.php
├── page.blade.php
├── partials
│   ├── comments.blade.php
│   ├── content-page.blade.php
│   ├── content-search.blade.php
│   ├── content-single.blade.php
│   ├── content.blade.php
│   ├── entry-meta.blade.php
│   ├── footer.blade.php
│   ├── head.blade.php
│   ├── header.blade.php
│   ├── page-header.blade.php
│   └── sidebar.blade.php
├── search.blade.php
├── single-product.php
├── single.blade.php
├── template-custom.blade.php
├── woocommerce
│   ├── archive-product.php
│   └── single-product.php
└── woocommerce.blade.php

As you can see all .blade.php files are now under resources/views like they are in a Laravel app.

WooCommerce Templates

We still seem to need to add archive-product.php and single-product.php to make WooCommerce work properly and have the content we add for WooCommerce loaded there. These seem to work from the views directory or views/woocommerce. But we do now have a way to use Blade templating by making them load woocommerce.blade.php.

This we can do with a simple:

<?php echo App\Template('woocommerce'); ?>

WooCommerce Blade file

The woocommerce.blade.php file is where we do all our magic and CAN use Blade. For a basic display we add:

@extends('layouts.app') 
@section('content') 
 @php(woocommerce_content())
@endsection

This so we can load the basic app layout and the general WooCommerce content using:

 @php(woocommerce_content())

As you can see this is a great step forward integrating Blade into WooCommerce!