Ever wonder how to back up a website on Linux? Jack Wallen shows you how easy it can be.
Disaster happens. Or, if disaster has yet to strike, you might find yourself in a situation wherein you need to migrate a website from one server or host to another. When either thing happens, what do you do? Panic? No. You follow through with your backup and restore plan.
You have one, right?
Okay, let’s fix that.
I’m going to walk you through the process of backing up and restoring a website that is housed on Linux. Understand, this process won’t work for every site (because all things are not equal), but it should give you a foundation from which to work.
With that said, let’s get going.
What you’ll need
I’m going to assume your website depends on a MySQL database, runs on Apache, and /var/www/html is your document root. To make this backup/restore happen, you’ll 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’m going to demonstrate using WordPress. Let’s say our database is wordpressdb. We have to create a backup of that before we do anything else. You might want to consider putting your site into maintenance mode (so users aren’t actively on the site and less data will be written to the database). You can put your WordPress site in maintenance mode with third-party plugins such as WP Maintenance Mode or SeedProd.
Once your site is in maintenance mode, back up the database by logging into the hosting server and issuing the command:
sudo mysqldump wordpressdb > wordpressdb-backup.sql
You might also want to add the date to the backup filename, such as wordpress-backup-DEC302021.sql.
How to back up WordPress
Now that your database is backed up, it’s time to back up the WordPress directory. Let’s assume that directory is /var/www/html/wordpress. To back up that directory, issue the command:
sudo tar -cpvzf wordpress-backup.tar.gz /var/www/html/wordpress
The above options are:
- c – create an archive
- p – preserve permissions
- v – show verbose output
- z – compress the archive
- f – create a file
At this point you have the two files:
Next, you’ll want to make a copy of your Apache configuration file. Assuming that file is wordpress.conf, make a copy of it with:
sudo cp /etc/apache2/sites-available/wordpress.conf ~/wordpress.conf
Finally, if you’re using SSL certificates on your server, you’ll want to copy them as well.
How to restore WordPress
Okay, it’s now time for the restoration. I’m going to assume we’re restoring to the same server. If you’re restoring to a new server, you’ll need to start by making sure you have all of the dependencies installed (the full LAMP stack) 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 assume you have everything WordPress requires installed. The first thing we’ll then do is restore the database with the command:
sudo mysql wordpressdb < wordpressdb-backup.sql
Next, we’ll restore the backup directory to the Apache document root with:
tar -xzvf wordpress-backup.tar.gz sudo mv wordpress /var/www/html/
Move your apache configuration 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 in maintenance mode before you backed it up, you’ll need to take it out of maintenance mode so users can access it.
And that’s all there is to backing up and restoring a website in Linux. Of course, this is very basic. If you have a much more complicated site, there will probably 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 Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.