Automating Linux System Backups with Bash (original) (raw)

Last Updated : 23 Apr, 2026

A regular, automated backup is the single most important task for protecting your data. A simple Bash script can automate this entire process, saving you from manual work and protecting you from data loss.

This guide provides a complete, one-stop script to create a robust backup system. This script will:

Bash Script for Backup Automation

Below is a simple guide to set up automated Linux backups using a Bash script:

Step 1: Set Up a Project Folder & Sample Data

Open your terminal and run:

Create a folder to hold your backup script and output

mkdir -p ~/backup-script/backups

Navigate into your project directory

cd ~/backup-script

Create a sample folder and file for testing

mkdir -p ~/backup-script/sample-data
echo "This is a test file." > ~/backup-script/sample-data/testfile.txt

Your folder structure now looks like:

~/backup-script/
├── backups/ # Backups go here
└── sample-data/ # Sample directory to back up
└── testfile.txt

tree-command

Step 2: Create the Backup Script

Create and open the script:

nano backup.sh

Paste the following code:

#!/bin/bash

=== Configuration Section ===

Directories to back up (customize these)

SOURCE_DIRS="$HOME/backup-script/sample-data"

Destination for backups

BACKUP_DIR="$HOME/backup-script/backups"

Timestamp format

DATE=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_NAME="backup_$DATE.tar.gz"

Retention policy

RETENTION_DAYS=7

=== Start Backup ===

echo "Starting backup of: $SOURCE_DIRS"
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/$BACKUP_NAME" $SOURCE_DIRS

Check if tar was successful

if [ $? -eq 0 ]; then
echo "Backup saved to: BACKUPDIR/BACKUP_DIR/BACKUPDIR/BACKUP_NAME"
else
echo "Backup failed!"
exit 1
fi

=== Delete Old Backups ===

echo "Deleting backups older than $RETENTION_DAYS days..."
find "$BACKUP_DIR" -name "*.tar.gz" -type f -mtime +$RETENTION_DAYS -exec rm {} ;
echo "Cleanup done. Script finished!"

Step 3: Make the Script Executable

chmod +x backup.sh

Step 4: Run Your First Backup

Run the script:

./backup.sh

**Expected Output:

Starting backup of: /home/your-user/backup-script/sample-data
Backup saved to: /home/your-user/backup-script/backups/backup_2025-07-24_15-19-24.tar.gz
Deleting backups older than 7 days...
Cleanup done. Script finished!

**Output:

backup

How the Script Works

This script is built on four key commands:

**1. The Array : SOURCE_DIRS=(...) By using a Bash array, you can list multiple directories. The syntax "${SOURCE_DIRS[@]}" ensures that even directories with spaces in their names (like "$HOME/My Documents") are handled correctly.

**2. The Timestamp : DATE=$(...) DATE=$(date +%Y-%m-%d_%H-%M-%S) runs the date command and stores its output (e.g., 2025-11-15_14-30-05) in the DATE variable. This guarantees every backup has a unique name.

**3. The Archive : tar -czf ... This is the engine of the script. tar is the command that creates the archive.

**4. The Cleanup : find ... -exec rm ... This command automatically manages your retention policy.

Automation using cronjobs

A backup script is only useful if it runs automatically. The standard tool for this in Linux is cron. cron is a background service that runs scheduled tasks. You can edit its schedule by running:

crontab -e

Now, add a line to the bottom of this file to schedule your script. The syntax is:

Minute] [Hour] [Day of Month] [Month] [Day of Week] [Command to run]

**Example: Run the script every day at 2:00 AM Add this one line to your crontab -e file:

0 2 * * * /home/your-user/backup.sh > /home/your-user/backup.log 2>&1

**Breakdown of this line:

Real-World Use Cases

This script can be adapted to various use cases: