Have you ever wondered how to back up a website on Linux? Jack Wallen shows you how easy it is.
Catastrophe occurs. Or, if disaster hasn’t happened yet, you may find yourself in a situation where you need to migrate a website from one server or host to another. When anything happens, what do you do? panic? No, you are following the backup and restore plan. You have one, right? number? Well, let’s fix that.
I will walk you through the process of backing up and restoring your Linux website. Understand that this process won’t work for every site (because not all things are created equal), but it should give you a foundation to work from.
With that, let’s get started.
What will you need
I’m going to assume that your website is based on a MySQL database, running on Apache, and /var/www/html is the root of your document. To perform this backup/restore you will need a user with root privileges.
See: 40+ Open Source and Linux Terms You Need to Know (TechRepublic Premium)
How to back up your database
First, I will explain the use of WordPress. Let’s say our database is wordpressdb. We have to create a backup of that before we do anything else. You may want to consider putting your site into maintenance mode (so that users are not active on the site and less data will be written to the database). You can put your WordPress site into maintenance mode using third-party plugins like WP Maintenance Mode or SeedProd.
Once your site is in maintenance mode, back up the database by logging into your hosting server and issuing the command:
sudo mysqldump wordpressdb > wordpressdb-backup.sql
You may also want to add the date to the backup file name, such as wordpress-backup-DEC302021.sql.
How to backup WordPress
Now that the database is backed up, it’s time to back up your WordPress directory. Let’s say the directory is /var/www/html/wordpress. To back up this directory, issue the command:
sudo tar -cpvzf wordpress-backup.tar.gz /var/www/html/wordpress
The above options are:
- against – Create archive
- s – Maintain permissions
- Fifth – Show verbose output
- z Archive compression
- F – create a file
At this point you have the two files:
Next, you’ll want to make a copy of the Apache configuration file. Assuming this file is wordpress.conf , make a copy of it with:
sudo cp /etc/apache2/sites-available/wordpress.conf ~/wordpress.conf
Finally, if you are using SSL certificates on your server, you will need to copy them as well.
How to recover WordPress files
Well, it’s time to recover. I’m going to assume we’re restoring the same server. If you are restoring to a new server, you will need to start by making sure all dependencies (full LAMP stack) are installed with a command like:
sudo apt install apache2 ghostscript libapache2-mod-php mysql-server php php-bcmath php-curl php-imagick php-intl php-json php-mbstring php-mysql php-xml php-zip -y
Let’s say you have everything WordPress requires installed. The first thing we will do next is to restore the database with the command:
sudo mysql wordpressdb < wordpressdb-backup.sql
Next, we will restore the backup directory to the root of the Apache document using:
tar -xzvf wordpress-backup.tar.gz sudo mv wordpress /var/www/html/
Transfer your apache config file with:
sudo mv wordpress.conf /etc/apache2/sites-available/
Enable the site with:
sudo a2ensite wordpress
You should now be able to access the WordPress site as you did before. If you put the site into maintenance mode before backing it up, you’ll need to get it out of maintenance mode so that users can access it.
And that’s it for backing up and restoring a website in Linux. Of course, this is very basic. If you have a more complex site, there will likely be more steps involved. However, this will at least give you a general understanding of how the process works.
Subscribe to TechRepublic’s How to make technology work on YouTube Get the latest tech advice for business professionals from Jack Wallen.