Given that WordPress is the most popular blogging software in the world, many users expect there to be an easy,Â backup system built into it. Sadly, there isn’t, so users who want to backup a WordPress site, will have to do it manually.
A critical aspect of backing up any WordPress installation on Linux is the SQL database. Luckily, backing up SQL on Linux is very easy. To create a backup of the WordPress database, we’ll be using “sqldump”. It’s a simple tool, that, if used correctly can be used to easily export the contents SQL databases on Linux.
To export your WordPress database, open up a terminal and gain a root shell with su (or sudo -s, if the root account is disabled).
Now that the shell has full root permissions, create a new backup folder, along with the SQL and installation files sub-directories.
mkdir -p /root/wp-backup mkdir -p /root/wp-backup/sql mkdir -p /root/wp-backup/wp-installation-files
Using the CD command, move the terminal to theÂ sql sub-folder inside of the wp-backup directory.
Next, export the WP database to an SQL file on the Linux server. Keep in mind that the sqldump command will not work if you do not know the correct database details (correct DB name, user, and password). If you followed the instructions on how to install WordPress on Ubuntu server, the WP SQL database name is “wordpressdb,” and the user is “ubuntuwordpress”.
‘); if (navigator.appVersion.indexOf(“Mac”)!=-1) document.write(”);
mysqldump -u username -p databasename > db.wp__backup.sql
Run the ls command to confirm that the new wp_backup.sql is in the /root/wp-backup/sql folder.
When you can confirm the SQL file is there, the WordPress SQL backup is complete!
Back Up WordPress Installation
With the WordPress SQL database taken care of, the next step in the backup process is to preserve the WordPress installation itself. Backing up the installation starts by using theÂ cpÂ command to move all files fromÂ /var/www/htmlÂ toÂ /root/wp-backup/wp-installation-files.
In the terminal, use cpÂ to create a complete copy of your current WordPress installation.
cp -rp /var/www/html/* /root/wp-backup/wp-installation-files/
The copy command may take a bit of time to complete, depending on how large yourÂ WordPress installation is. When the cp command finishes running, cd into the backup directory and use the ls command to confirm the files are there. If you notice any files missing, it may be a good idea to re-run the command.
cd /root/wp-backup/wp-installation-files/ ls -a
Compressing The Backup
All critical WordPress files are in a backup location.Â The next step in the backup process is to compress everything into a Tar archive. Compressing files may seem tedious, but it’s worth it as it’ll be much easier to archive and transport the backup later. To create a new Tar archive of the backup, run the following command:
tar -zcvpf wordpress-backup.tar.gz /root/wp-backup
Once the compression finishes up, at this point the backup is ready to transport. Feel free to upload the backup, but be warned that it is unencrypted. Leaving the backup unencrypted is a very bad idea, especially since it means that anyone could look through the SQL database file, website structure, etc.
Encrypting the WP backup archive is easy, and it starts by installing GnuPG on your server. In the package manager, search for “gpg” and install it. Then, use the following command to encrypt the backup archive.
gpg -c wordpress-backup.tar.gz
Gpg asks for a password during the encryption process. Enter a memorable password in the terminal, and press the enter key on the keyboard.
When the encryption process finishes, the output file is wordpress-backup.tar.gz.gpg. This is the secure form of the backup, and the only file you’ll need. DO NOT KEEP THE UNENCRYPTED ARCHIVE!
Restore The Backup
Need to restore the backup? Do the following.
First, gain a root shell with su or sudo -s.
Then, navigate to /root and paste the wordpress-backup.tar.gz.gpg file there.
Decrypt the encrypted backup:
Extract the backup archive with Tar.
tar -xvpf wordpress-backup.tar.gz
Next,Â CD into the SQL folder and import the database.
mysqldump -u username -p databasename < db.wp__backup.sql
Importing the SQL database will instantly re-import all blog data, user information, posts and etc. After that, all that’s left is to move the WordPress system files in the correct place.
Using the cp command, move the files into place.
cp -rp /root/wordpress-backup/* /var/www/html
Finally, remove the archive files and clean out the /root folder.
rm wordpress-backup.tar.gz rm wordpress-backup.tar.gz.gpg rm -rf wordpress-backup