Backup WordPress Using The Linux Terminal

This process requires a remote webhost running on a Linux distribution such as Ubuntu. Before you begin you’ll need the following information about your server.

mysql username, password and database table name

Log into your website via SSH

ssh root@your-domain.com
root@your-domian.com's password:

Open a new terminal window and run the ssh command above.

Server Side Backup storage

Once logged in navigate to a new “Current Directory” to a backup folder of your choosing and create a folder to store your backup. I like to use today’s date.

All commands below are performed with superuser / root access

cd /var/www/backups
mkdir 20221001
cd 20221001

  • cd will change you to the new “current directory”
  • mkdir will create a new directory

mysqldump --add-drop-table -h localhost -u username -p wp_wordpress > wordpress.bak.sql
Enter password:

  • the mysqldump command above will export your database into wordpress.bak.sql

ls
wordpress.bak.sql

  • ls will display your exported database file

tar czvf wordpress.tar.gz wordpress.bak.sql ../../html/wp-content/ ../../html/wp-config.php ../../html/.htaccess

  • tar is a fun throwback from the days of “tape archives.”

Effectively happening is the creation of an archive file named named wordpress.tar.gz which includes the database, wp-config.php, .htaccess and everything in the wp-content folder. You may include others if you like.

cp wordpress.tar.gz /var/www/html/

Move the archive to a public folder and download.

Download to your computer

wget your-domain.com/wordpress.tar.gz

On your local machine open an new terminal window and use wget to download the archive.

Remove the original backup from the server

Be careful using the rm command. Any files removed can’t be recovered. The -i flag will require a yes or no confirmation.

rm -i /var/www/html/wordpress.tar.gz 
rm: remove regular file '/var/www/html/wordpress.tar.gz'? 

That’s it, admittedly the process takes a little practice. However, for small to medium sized sites a person could run thorough the process in a matter of minutes.

Author: Joseph Dickson

Joseph Dickson is a front-end web developer with experience building higher education websites using Drupal and WordPress.