Skip to main content
Tutorials

Configuring Automated Linux Server Backups: A Step-by-Step Tutorial

Learn how to configure automated, reliable local backups for your Linux servers using Rsync and systemd timers to protect your business data.

A technician examining server storage racks in a clean server room | NetBit Software House and Technology | netbit.pk

Introduction: Why Local Linux Server Backups Matter for Pakistani Offices

Data loss can paralyze any business. Whether due to hardware failure, corrupted files, unexpected system crashes, or power disruptions common in regions across Pakistan, losing operational data creates costly downtime. While cloud storage options exist, reliance on external cloud services for massive daily files can strain internet bandwidth and incur high recurring fees for local companies and educational institutions.

Setting up an automated, local Linux server backup system offers an efficient solution. By leveraging tools native to Linux—specifically Rsync and systemd timers—you can create secure, scheduled daily backups across your local network without purchasing expensive proprietary software. In this step-by-step tutorial, the team at netbit tech will walk you through creating an automated backup process designed for durability and performance.

Prerequisites for Setting Up Automated Backups

Before beginning this tutorial, ensure you have the following requirements ready on your primary Linux server:

  • A user account with sudo administrative privileges.
  • Two distinct storage locations: a source folder containing your critical files and a target backup location (such as a separate local drive or a dedicated secondary server mount).
  • Basic familiarity with running commands in the Linux terminal.
  • OpenSSH installed if you intend to mirror files to a secondary machine across your local network.

Step 1: Installing and Verifying Rsync

Rsync is a fast, flexible utility that synchronizes files between source and destination directories. It saves time and bandwidth by transferring only the modified portions of files.

First, open your terminal and check if Rsync is installed on your Linux distribution. For Ubuntu or Debian-based distributions, execute the following command:

sudo apt update && sudo apt install rsync -y

If your office server runs AlmaLinux, Rocky Linux, or RHEL, run:

sudo dnf install rsync -y

Verify the installation by checking the version:

rsync --version

Step 2: Preparing Your Backup Script

Rather than running manual commands every day, we will write a shell script that handles directory checks, executes the synchronization, and logs the output for audit purposes.

Creating the System Script

Create a dedicated directory to store maintenance scripts on your system:

sudo mkdir -p /opt/scripts

Open a new script file in your preferred text editor:

sudo nano /opt/scripts/server_backup.sh

Paste the following script content into the file. Be sure to update the SOURCE_DIR and DEST_DIR paths according to your actual storage layout:

#!/bin/bash

# Define backup directories
SOURCE_DIR="/var/www/html"
DEST_DIR="/mnt/backup_drive/daily_backup"
LOG_FILE="/var/log/server_backup.log"

# Get current timestamp
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")

echo "[$TIMESTAMP] Starting automated server backup..." >> "$LOG_FILE"

# Ensure destination directory exists
mkdir -p "$DEST_DIR"

# Execute Rsync command
rsync -avz --delete "$SOURCE_DIR" "$DEST_DIR" >> "$LOG_FILE" 2>&1

if [ $? -eq 0 ]; then
echo "[$TIMESTAMP] Backup completed successfully." >> "$LOG_FILE"
else
echo "[$TIMESTAMP] Backup failed! Check log for details." >> "$LOG_FILE"
fi

Understanding the Rsync Options

In this script, the flags passed to Rsync perform vital duties:

  • -a (archive): Preserves file permissions, modification times, ownership, and symbolic links.
  • -v (verbose): Displays detailed transfer output.
  • -z (compress): Compresses data during transfer to optimize network throughput.
  • --delete: Removes files from the backup directory if they no longer exist in the source directory, keeping your backup mirror accurate.

Save and close the file. Next, make the script executable by adjusting file permissions:

sudo chmod +x /opt/scripts/server_backup.sh

Test the script manually to confirm it operates correctly:

sudo /opt/scripts/server_backup.sh

Check the log file output to ensure your data synchronized cleanly:

cat /var/log/server_backup.log

Step 3: Automating the Task Using Systemd Services and Timers

While traditional Linux administrators often use cron jobs, modern distributions rely on systemd timers. Systemd offers superior logging, better service dependency handling, and reliable catch-up execution if a server was powered off during scheduled execution times.

Create the Systemd Service File

Create a service file that defines what task systemd will execute:

sudo nano /etc/systemd/system/server-backup.service

Add the following configuration:

[Unit]
Description=Automated Local Server Backup Task
After=network.target

[Service]
Type=oneshot
ExecStart=/opt/scripts/server_backup.sh

Create the Systemd Timer File

Next, create the timer unit file that controls when the service runs:

sudo nano /etc/systemd/system/server-backup.timer

Add the following schedule definition to execute the script daily at 2:00 AM:

[Unit]
Description=Runs Server Backup Daily at 2:00 AM

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

The setting Persistent=true ensures that if your server was powered down at 2:00 AM, systemd will run the missed backup task immediately upon rebooting.

Enable and Activate the Timer

Reload the systemd daemon to process your new units:

sudo systemctl daemon-reload

Start and enable the timer so it automatically runs on boot:

sudo systemctl enable --now server-backup.timer

Verify that your timer is active and scheduled properly:

sudo systemctl list-timers --all | grep server-backup

Step 4: Testing and Verifying Your Backup System

Automated backup routines require periodic verification to ensure integrity. Follow these testing steps:

Discussion

Reader comments.

Share your question, suggestion or experience related to this article.

0 Approved Comments

No comments yet

There are no approved comments on this article yet. You can be the first person to leave one.

Keep Reading

Related insights.

View all articles
WhatsApp