How to backup and restore a website in Linux



Have you ever wondered how to backup a website in Linux? Jack Wallen shows you how easy it can be.

Image: Shutterstock / fizkes

The disaster is coming. Or, if disaster hasn’t struck yet, you might find yourself in a situation where 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 your backup and restore plan. You have one, don’t you? No? Okay, let’s fix that.

I’ll walk you through the process of backing up and restoring a website hosted on Linux. Realize that this process won’t work for all sites (because not all things are equal), but it should give you a foundation from which to work.

Having said that, let’s go.

What you will need

I’m going to assume your website depends on a MySQL database, runs on Apache, and / var / www / html is your document root. For this backup / restore to happen, you will need a user with root privileges.

SEE: Over 40 Open Source & Linux Terms You Need To Know (TechRepublic Premium)

How to back up your database

First, I will demonstrate the use of WordPress. Let’s say our database is wordpressdb. We need to create a backup of this before we do anything else. You may want to consider putting your site in maintenance mode (so that users are not actively on the site and less data is written to the database). You can put your WordPress site in maintenance mode with third-party plugins like WP Maintenance Mode or SeedProd.

Once your site is in maintenance mode, back up the database by connecting to the hosting server and running the command:

sudo mysqldump wordpressdb > wordpressdb-backup.sql

You can also append the date to the name of the backup file, such as wordpress-backup-DEC302021.sql.

How to backup WordPress

Now that your database is backed up, it’s time to back up the WordPress directory. Suppose the directory is / var / www / html / wordpress. To back up this directory, run the command:

sudo tar -cpvzf wordpress-backup.tar.gz /var/www/html/wordpress

The above options are:

  • vs – create an archive
  • p – preserve permissions
  • v – display verbose output
  • z – compress the archive
  • F – create a file

At this point you have the two files:

  • wordpressdb-backup.sql
  • wordpress-backup.tar.gz

Next, you’ll want to make a copy of your 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 want to copy them as well.

How to restore WordPress

Alright, now is the time for the restoration. I’ll assume we’re restoring on the same server. If you are restoring to a new server, you will need to start by making sure that all dependencies are installed (the full LAMP stack) with a command such as:

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

Suppose everything that WordPress needs is installed. The first thing we’ll do next is 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 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/

Activate 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 saving it, you will need to take it out of maintenance mode before users can access it.

And that’s all there is to backing up and restoring a website in Linux. Of course, it is very basic. If you have a much more complicated 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 How to make technology work on YouTube for all the latest technical advice for professionals at Jack Wallen’s business.

Also look



Comments are closed.