1. Home
  2. Linux
  3. How to back up and restore your website on linux

How to back up and restore your website on Linux

If you run a website on a Linux server, you should be keeping regular backups. We’ve got you covered if you’re new to Linux server administration. Here’s how to back up and restore websites on Linux.

How to back up your website on Linux

If you use Ubuntu Server, Debian, or any other Linux operating system and host a website, you’ll need to know how to create a backup. Creating a backup consists of four things.

  1. Backing up the website files
  2. Backing up the Apache website configuration file
  3. Backing up the MySQL database
  4. Backing up SSL certificates

To create your backup, you’ll need terminal access. Ssh into your server using a terminal on Linux, Powershell on Windows, or Terminal on Mac OS. Once logged in, follow the backup instructions outlined below.

Backing up website files

 Backing up website files is critical. Thankfully, backing them up is as easy as zipping up any old folder on Linux. To create a backup, run the tar command on the /var/www/html/ directory.

Note: if you house your website in a different directory other than /var/www/html/You must change the backup command before running it.

cd /var/www/html/
tar -cpvzf ~/my-website-backup.tar.gz *

Depending on how large your website files are, the backup could take time to complete. When the process is finished, you can move the “my-website-backup.tar.gz” file off your server and to a backup drive, cloud storage, etc.

Backing up website Apache configuration

Most people hosting websites or web apps on Linux are likely using the Apache tool. For this reason, you’ll need to create a backup of your site’s apache configuration file. Here’s how to do it.

First, get root access to the terminal. You can gain root access using the sudo -s command. Alternatively, you can log into root with su.

sudo -s

Once logged in, use the cp command to copy your website conf file from /etc/apache2/sites-available/.

cp /etc/apache2/sites-available/my-config-file.conf

If you can’t remember the name of your configuration file offhand, run the ls command to view the contents of the “sites-available” folder.

ls /etc/apache2/sites-available/

Move into the “sites-available” folder. Then, compress all Conf files.

cd /etc/apache2/sites-available/

tar -cpvzf ~/my-apache-configuration-backup.tar.gz *.conf

Backing up MySQL database

Does your website use MySQL? If so, you’ll need to create a backup of the database. To create a backup of your MySQL database, run the mysqldump command.

sudo -s
mysqldump name-of-MySQL-db > my-sql-backup.sql

Backing up SSL certificates

While not everyone uses SSL on websites, it’s good to back them up. To back up your SSL certificates, you must compress the /etc/ssl/ folder.

cd /etc/ssl/

tar -cpvzf ~/my-ssl-backup.tar.gz *

How to restore your website on Linux

If you need to restore your website data on Linux, you’ll need to open up a terminal window. Restoration, like backup, needs to be done in the Linux command line. Once the terminal window is open, follow the instructions below to restore your website from a backup.

Restore website files

To restore your website files to the /var/www/html/ folder on your Linux server, start by placing your backup file in the home directory for your user account. Once you’ve done that, run the following command to restore the files from backup.

sudo tar ~/xvf my-website-backup.tar.gz -C /var/www/html/

Restore Apache website configurations

Do the following to restore your Apache website configuration files to the correct directory. First, place the “my-apache-configuration-backup.tar.gz” file in the home directory for your user.

Once the “my-apache-configuration-backup.tar.gz” file is in your home folder, run the tar command to restore the configuration files to the correct folder.

Note: you will need to re-enable these configuration files with Apache2 to use them on your server.

sudo tar xvf ~/my-apache-configuration-backup.tar.gz -C /etc/apache2/sites-available/

Restore MySQL databases

To restore your MySQL databases dumped previously, start by dropping your existing database using the drop command.

sudo mysql
drop database my_db;

After dropping your database, it will be deleted. Create a new database using the create command.

create database my_db;
exit;

Finally, restore the database dump to your server using the following mysql command. Be sure that the “my-sql-backup.sql” file is in your home directory.

sudo mysql -u root -p my_db < ~/my-sql-backup.sql

Restore SSL certificates

To restore your SSL certificates to your Linux server, start by placing the “my-ssl-backup.tar.gz” file in your home folder. Once the file is there, you can restore the certificate backups using the tar command.

sudo tar xvf ~/my-ssl-backup.tar.gz -C /etc/ssl/