There are a lot of WordPress backup tools and plugins available, many of which are packed with features and can be a great help to maintaining backups of your site. However, some simple command line tools and commands are all you need to create full backups and also to spin up development and testing versions of your site.
In this quick article, I will show you how to use the command line to create a full site backup in a zip file.
Prerequisites
- SSH access to your webserver.
- WP-CLI – This is the command line tool for WordPress. Most larger hosts have this available for you by default, but in some instances, you may have to install it yourself.
- Zip – Similar to the above, if you have SSH access to your site/server/host, then you probably already have “zip” available at the comment line. If not, you’ll need to install it.
Let’s Get Started
The first step is to create a database backup. This is what you need WP-CLI for.
In your SSH terminal, navigate to your site root directory and execute the following command:
wp db export
Note: this exports using a default file name with a random string as part of the name. The command does have some options to allow for a custom filename, but using the default is adequate for this purpose because we’re only going to do one file, and include it in the zip.
Now in your SSH terminal, remaining in your site root, execute the following command:
zip -r my_zip_filename.zip * .htaccess
This will zip up everything in your site root folder and all sub folders, including your .htaccess file (it will skip any other hidden files that start with a “.” though).
Now you have a zip file that contains all your site files as a db backup file that you can import either via WP-CLI or in a MySQL tool.
Clean Up After Yourself!
Don’t leave these files lying around in your site root. They are accessible to anyone who can figure out the file name. You need to delete these files when you’re done doing what you’re doing.
Run the following command in your SSH terminal to see the files in your web root:
ls
This will show you the files in the root. You should see a file with a long file name consisting of your site name, the date, and some random characters with “.sql” as the file extension. This is the db backup file. Delete this file by running the
following:
rm sitename-YYYY-MM-DD-randomstring.sql
Now do what you need to with the zip file – whether moving it within the file system or downloading it via FTP. But don’t leave it sitting there in the web root.
rm my_zip_filename.zip
Double check by running ls to list the file commands and make sure your .sql and .zip are deleted.
ls
Conclusion
You have learned how to use WP-CLI to export your site’s database. You have learned how to use commandline zip to compress your site and db export into a zip file you can use as a backup. And you’ve learned how (and why) to delete the files and clean up after yourself. Once you’ve learned these steps you can backup any WordPress site in seconds. You can also create a script to run this process for you in an automated way. Either way, you have a very simple and powerful method of backing up your entire WordPress site.
Leave a Reply