I created a backup script for your WordPress installation. This is a script I am using for some years and I decided to make it public so that it can be useful to someone of you. If you want you can also contribute to improve it!
To use it you need SSH access to the server of your WordPress installation and WP-CLI installed. For example Siteground offers SSH access and WP-CLI to every user: https://www.siteground.com/kb/category/ssh-related-questions/.
Currently you can run this script only from the parent directory of your WordPress installation. So, if your WordPress is installed in the public_html directory, you’ll have to copy this script in the public_html parent directory.
This backup script for WordPress creates the backup files in its same directory. So for security reasons, please check that this is a non-public directory.
It creates two files:
- a .tar.gz or .tar.bz2 compressed archive containing the files (or a subset of them) of your installation.
- a .sql file that is the dump of the database.
The file names automatically contain the date, time, timezone, WordPress version and an optional comment. So you can easily remember, when, why and for which WordPress version the backup was done.
The script always dumps the entire database. For the files you can choose to include in the tarball the entire installation or just a subset. The options are:
- full (backup of all WordPress files)
- nouploads (all files except wp-content/uploads)
- system (all files except wp-content)
- wp-content (only wp-content)
- plugins (only wp-content/plugins)
- mu-plugins (only wp-content/mu-plugins)
- themes (only wp-content/themes)
The ‘nouploads’ option is useful to decrease the backup size for sites that have a good number of images. The reason is that in this case most of the hosting space is occupied by the images, that are stored under the wp-content/uploads
directory. If you want to check how much space is occupied by your uploads directory you can open a SSH session and use the du
command like in this example:
du -hcs public_html/wp-content/uploads
With this script you can make a backup very quickly: open an SSH session and execute it! Moreover you can add an optional comment to remember the reason why you did it.
For example I always make a backup:
- before any important change during the development of the site;
- before updating WordPress and its plugins.
Restoring the database from the backup is very simple. You have to use the WP-CLI wp db import command. For example if you run it from the parent directory of public_html, the command is:
wp db import --path=public_html mybackupfile.sql
To restore the files you have to use the tar command, you can find the complete syntax and all its options by executing:
man tar
Or you can unpack your archive in your computer and just upload the files you want to restore.
You can find the script with the setup instructions in GitHub under https://github.com/somedevtips/wordpress-backup .
I hope you’ll find my script useful!