Ugacomp

How to configure an automated backup system on Ubuntu Linux

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

If you want to configure an automated backup system on Ubuntu Linux, you will need to use rsync too for the file backup and cron job for automated backup scheduling. In this guide, let’s explore how it’s done;

Install rsync

rsync is a widely used and powerful command-line utility for synchronizing files and directories between two locations. It is particularly efficient for copying files over a network or between different drives on the same system. rsync is designed to minimize data transfer by only copying the parts of files that have changed.

How are the commands you need to install rsync;

sudo apt update
sudo apt install rsync

Create a backup script

The backup script will contain the rsync commands for your backup. Let’s create a simple script named backup.sh:

nano ~/backup.sh

Inside the editor, add the following lines. Customize the source and destination directories accordingly:

#!/bin/bash

# Set source and destination directories
SOURCE_DIR="/path/to/source"
DESTINATION_DIR="/path/to/backup"

# Run rsync command
rsync -av --delete $SOURCE_DIR $DESTINATION_DIR

To help you understand well, let’s break down the above script we’ve just created;”

Shebang (#!/bin/bash)

The shebang at the beginning of the script (#!/bin/bash) specifies the path to the Bash interpreter. It indicates that the script should be interpreted and executed using the Bash shell.

Source and Destination Directories

The script defines two variables, SOURCE_DIR and DESTINATION_DIR, which represents the source directory (the directory you want to back up) and the destination directory (where the backup should be stored).

Rsync Command

The rsync command is used to synchronize files and directories between the source and destination. Here’s a breakdown of the options used:

-a: Archive mode. This preserves various file attributes, including permissions, ownership, and timestamps.

-v: Verbose mode. It displays details about the files being transferred.

--delete: This option ensures that files in the destination directory that do not exist in the source directory are deleted. This helps keep the backup synchronized with the source.

Make the script executable

Making a script executable is necessary to allow it to be run as a standalone program or command. In Linux and Unix-like operating systems, including Ubuntu, scripts are executed by the shell interpreter specified in the script’s shebang line. Making the script executable involves giving it the necessary permissions to be treated as an executable file.

You can use this command to add the executable permission to the script

chmod +x ~/backup.sh

Test the script

To be sure that the integrated commands in the script are working perfectly, we need to manually use the command below;

./backup.sh

To verify, check the destination directory to ensure that the files are copied correctly as defined in the script

Schedule the backup using cron

The backup automation will be done by using the cron job. So, we will edit the crontab file using the following command;

crontab -e

Add a line to schedule your backup. For example, to run the backup every day at 2:00 AM:

0 2 * * * /path/to/backup.sh

The expression “0 2 * * *” is a cron schedule, specifying the timing for the execution of a command or script. In this case, it denotes that the command “/path/to/backup.sh” should be executed at 2:00 AM every day. The five asterisks represent the minutes, hours, day of the month, month, and day of the week, respectively, with “0” indicating the exact time (midnight) for the daily backup to occur. Adjusting these values allows for customization of the schedule, enabling you to automate tasks such as backups or maintenance at specified intervals.

You can adjust the cron schedule according to your preference.

Monitor the backup

You can redirect the output of your script to a log file to monitor the backup process:

0 2 * * * /path/to/backup.sh >> /path/to/backup.log 2>&1

This line redirects both standard output and error output to a log file.

That’s it! You’ve configured an automated backup system using rsync and cron on Ubuntu Linux. Adjust the paths, schedule, and other parameters according to your specific requirements.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.