Occasionally, disaster happens. You may even find yourself in a situation where you need to migrate a website from one server or host to another if tragedy has not yet struck. What do you do if either of these things happens? Panic?
No. You stick to your backup and restoration strategy. Isn’t it true that you have one?
No? Let’s see what we can do about that.
I’m going to walk you through the steps of backing up and restoring a Linux-based website.
Understand that this technique will not work for every site (because not everything is equal), but it should provide you with a starting point.
Let’s get started now, shall we?
How to back up your database
First, I’ll show you how to use WordPress. Assume our database is called wordpressdb.
Before we do anything else, we need to make a backup of it. Consider placing your site into maintenance mode (so that users aren’t actively using it and less data is written to the database). You can use third-party plugins like WP Maintenance Mode or SeedProd to place your WordPress site in maintenance mode.
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 you’ve backed up your database, it’s time to back up your WordPress directory. Assume the directory is located at /var/www/html/wordpress. Issue the following command to back up that directory:
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:
- wordpressdb-backup.sql
- wordpress-backup.tar.gz
Making a copy of your Apache configuration file is the next step. Make a copy of that file, assuming it’s wordpress.conf:
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, now it’s time to get back to work on the restoration.
I’m going to proceed with the assumption that we’re restoring to the same server.
If you’re restoring to a new server, make sure you have all of the dependencies (the whole LAMP stack) installed first 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 log in to the WordPress site as usual.
You’ll need to remove the site out of maintenance mode so people may access it if you put it in maintenance mode before backing it up.
That’s all there is to backing up and restoring a Linux website.
Of course, this is rudimentary. There will very certainly be more steps if your site is significantly more intricate. This will, however, provide you with a general idea of how the process works.
Click here to read more useful and interesting articles.