In this article I’m going to show you how you can create a complete backup of you WordPress site without the use of a plugin only using command line tools. In order to achieve this you should have SSH access to your WordPress site and have basic knowledge of the command line. The article will separated in two part – database backup and files backup. So let’s start with the first one.
Database backup
The first step is to create a new directory where you want to hold your backup, so for example we can create a new directory in our wp-content directory named “backup” with the following command (but if you want you can create the backup directory wherever you want):
//1. First we make sure that we are in the wp-content directory
cd public_html/wp-content
//2. We create the new directory
mkdir backup
//3. Go to the new backup directory
cd backupCode language: plaintext (plaintext)
Now after we have created our directory where we want to save our backup the first step is to create a backup for our database and actually this is very easy to do with a wp cli command which is (learn more about the wp db command):
wp db exportCode language: plaintext (plaintext)
This version of the command will autogenerate the backup file name which will be the database name with the time and date of backup which for me is ok, but if you want to name the file by yourself you can do it with this version of the command:
wp db export your_name.sqlCode language: plaintext (plaintext)
Now here is something important that you have to know if your database is big it’s a good idea to compress the generated backup using the gzip command:
wp db export - | gzip > backup.sql.gzCode language: plaintext (plaintext)
So as you can see creating a database backup for your WordPress site through the command line is very easy and fast. But you may be wondering what about restoring the database backup, well that is very easy as well. To restore the database backup first we need to empty (drop the tables) the current database with the command:
wp db reset --yesCode language: plaintext (plaintext)
Then the second step is to import the backup that we’ve generated with the following command:
wp db import name_of_our_backup.sqlCode language: plaintext (plaintext)
If you have compressed your database backup you have to uncompresse it first and than import it:
gunzip < backup.sql.gz | wp db import -Code language: plaintext (plaintext)
And voala now you know how backup and restore fast your database through the command line without the use of a plugin. Now let’s see how to backup your site files.
Files backup
Unfortunately there is no wp cli command for creating a backup of your site files the way you can backup your database, but this is not a problem since there is a very helpful standard cli command that we can use and this is the tar command.
Actually using this command gives us many options what we want to backup, because you may want to create a backup only a certain part of you site. For example let’s say that you want to archive only your plugins directory – you can do it like this given that we still in the backup directory that we created (see the bonus tip in this article about how you can see the path to a certain directory):
tar -cf plugins.tar path_to_your_plugins_directoryCode language: plaintext (plaintext)
You have to know that the tar command do not compress the files and directories it only goups them in single archive file which means that the generated tar file can become quite big so it’s better to use this version of the command that also compress the backup file:
tar -czf plugins.tar.gz path_to_your_plugins_directoryCode language: plaintext (plaintext)
Now when you want to restore a tar file backup first you have to navigate to the wp-content directory because in our example we want to restore our plugins directory backup and then with the following command with restore our compressed backup (here it doesn’t matter if you are restoring standard tar file or compressed one):
tar -xf path_to_your_backup_direcotry/plugins.tar.gzCode language: plaintext (plaintext)
As you can see creating and restoring files backups it’s not more complicated than creating and restoring database backups. In the example above we created only a backup of the plugins directory but this approach can be used if you want to backup your whole site.
Bonus tip
If you want to find what is the path to certain directory you can use the pwd (print working direcotry) command – so if you want to see the complete path to your wp-content directory navigate to it write the pwd command in the terminal and you’ll see the path it will be something like this (but it depends of your hosting settings):
/home/joro/domains/gbogdanov.com/public_html/wp-content/Code language: plaintext (plaintext)