How to Automatically Backup Your Website on an Ubuntu Cloud Server?

23-01-2024 02:39:15

This is a practical guide detailing the steps to automatically backup website data and configuration files. Using a cloud server with Ubuntu OS as an example, we employ tools like tar, rsync, and cron. The backup script runs daily, retaining data for the past year. Our backup strategy adheres to the 3-2-1 backup principle, ensuring three copies of the data: one working copy, another on a different local medium, and a third off-site backup. For instance, consider a photo stored on your home computer (the first local copy), on a portable hard drive (the second local copy), and on a cloud service (the third copy).

1. Preparation

Before creating backups, ensure sufficient disk space on your cloud server. Use the following commands to check total disk space, website data size, and configuration file size. Adjust the directories for website data and configuration files as needed.

$ df -h
$ du -sh /var/www
$ du -sh /etc/apache2

Once enough disk space for backup is confirmed, create a directory to store backup files.

$ sudo mkdir /var/web_backup

2.Performing Backup

Create a backup of website data and configuration files using the tar tool, assuming the backup is named web-server.tar.gz.

$ sudo tar -cvzpf /var/web_backup/web-server.tar.gz -C /var/ www -C /etc/ apache2

Here's a breakdown of the command options:

  • c: Create an archive
  • v: Verbose output
  • z: Use gzip compression
  • p: Preserve file permissions
  • f: Path and filename of the archive
  • C: Change directory
    Note the space after /var/ and /etc/. Include additional directories for backup after the -C option.

To exclude certain files from the archive, use the --exclude=path/to/file/ option. For example, to exclude the /var/www/example.com/junk/ directory, use the command below.

$ sudo tar -cvpzf /var/web_backup/web-server.tar.gz --exclude=www/example.com/junk -C /var/ www -C /etc/ apache2

Remember: Use relative, not absolute paths for exclusions. For instance, use www/example.com/junk instead of /var/www/example.com/junk. Multiple files for exclusion require multiple --exclude options or a file listing all exclusions.

3.Testing Recovery

Before automating the entire backup and recovery process, ensure successful restoration. Create a directory for recovery and execute the decompression command.

$ sudo mkdir /var/web_backup/restored/
$ sudo tar -xvzpf /var/web_backup/web-server.tar.gz -C /var/web_backup/restored/ 

The options for this command are:

  • x: Extract files
  • z: Uncompress files

Then verify the contents of the recovered files and delete the recovery directory if there are no issues.

$ ls -lh /var/web_backup/restored/
$ sudo rm -r /var/web_backup/restored/

4.Automating the Process

The cron tool can execute commands at specified times. In this case, we schedule backups at 2 AM, retaining a week's worth of backups and naming them by date.

$ sudo tar -cvpzf /var/web_backup/web-server.`date +\%a`.tar.gz -C /var/ www -C /etc/ apache2

In the command, the +%a parameter represents the day of the week, like Mon for Monday, Tue for Tuesday, etc. The tar command will overwrite the backup file from the same day of the previous week.

Cron tasks are stored in a file named crontab. A typical automated task starts with five numbers or asterisks, followed by a command. The numbers represent the time for the task execution, and asterisks signify 'every'. These numbers correspond to minute (0-59), hour (0-23), day of the month (1-31), month (1-12), and day of the week (0-7).

To edit the crontab file, execute the following command.

$ sudo crontab -e

Add this automated task at the end of the file for daily execution at 2 AM.

0 2 * * * sudo tar -cvpzf /var/web_backup/web-server.`date +\%a`.tar.gz -C /var/ www -C /etc/ apache2

For monthly backups, use the +%b parameter for month abbreviation. The following task represents execution on the 1st of every month at midnight, retaining backups for a year.

0 0 1 * * sudo tar -cvpzf /var/web_backup/web-server.`date +\%b`.tar.gz -C /var/ www -C /etc/ apache2

5.Off-Site Backup

To fulfill the 3-2-1 backup principle's off-site backup requirement, use the rsync tool to download backups from the Web server. Ensure both Web and backup servers have rsync installed. It's not installed by default on Ubuntu, so install rsync and create a backup directory on the backup server with the following command.

$ sudo apt install rsync
$ mkdir server_backup

Execute the following rsync command to download website data from the Web server to the backup server.

$ rsync -azP --delete username@remotehost:/var/webbackup/ /path/to/offsite/server_backup

Options for this command include:

  • a: Archive all files while preserving symlinks and file permissions.
  • z: Compress files during transfer.
  • P: Combination of --partial and --progress, retaining partially transferred files during interruption and displaying transfer progress.
  • --delete: Remove irrelevant files from the backup directory. Use cautiously; test with --dry-run option to prevent data loss.

To exclude specific files from rsync, use the --exclude=relative/path/to/file/ option. For example, to exclude Wednesday's backup, use the command below.

$ rsync -azP --delete --exclude=web-server.Wed.tar.gz username@remote_host:/var/web_backup/ /path/to/offsite/server_backup

Remember: Use relative, not absolute paths for exclusions. The example uses web-server.Wed.tar.gz, not /var/web_backup/web-server.Wed.tar.gz. Use multiple --exclude options for multiple file exclusions.

6.Passwordless Rsync Login

To automate the process fully, set up passwordless rsync login from the backup server to the Web server.

Create an rsync key pair on the backup server:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/your_home/.ssh/id_rsa):

Then copy the public key to the Web server, enabling passwordless file downloads from the Web server to the backup server.

$ ssh-copy-id -i ~/.ssh/id_rsa_rsync username@remote_host

Test passwordless rsync login with the following command on the backup server.

$ rsync -azP --delete -e 'ssh -i ~/.ssh/id_rsa_rsync' username@remote_host:/var/web_backup/ /path/to/offsite/server_backup

7.Automating the Entire Process

Edit the crontab on the backup server:

$ sudo crontab -e

Add the following automated task at the end of the file for daily execution at 3 AM.

0 3 * * * rsync -azP --delete -e 'ssh -i ~/.ssh/id_rsa_rsync' username@remote_host:/var/web_backup/ /path/to/offsite/server_backup

In summary, we've outlined the process of automatic website backup on an Ubuntu cloud server according to the 3-2-1 backup principle. The website data is backed up daily, including local and off-site backups, with a retention period of one year.