Autocodewizard Logo Practical Project Automated Backup Script - Autocodewizard Ebooks - Bash Scripting Essentials

Chapter 12: Practical Project – Automated Backup Script

Apply your knowledge to create a practical automated backup script that regularly backs up files or directories to a specified location.

In this chapter, we’ll bring together everything we’ve learned by building a practical automated backup script. This project will cover scripting techniques, scheduling, and error handling to create a reliable backup solution.

Step 1: Define the Backup Requirements

First, decide what files or directories you want to back up and where to store the backups. For this example, we’ll back up a directory /home/user/documents to a backup directory /home/user/backup.

Step 2: Creating the Backup Script

Let’s create a script named backup.sh that will compress and back up the specified directory:

#!/bin/bash

# Directories to back up and destination
SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/home/user/backup"
DATE=$(date +%Y-%m-%d)

# Create a backup file with the current date
BACKUP_FILE="$BACKUP_DIR/documents_backup_$DATE.tar.gz"

# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"

# Perform the backup
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"

echo "Backup completed successfully: $BACKUP_FILE"

This script compresses SOURCE_DIR and saves it in BACKUP_DIR with a timestamp, creating a unique backup for each run.

Step 3: Adding Error Handling

Let’s add error handling to make the script more reliable. This includes checking for errors and logging messages:

#!/bin/bash
set -e

SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/home/user/backup"
DATE=$(date +%Y-%m-%d)
LOG_FILE="$BACKUP_DIR/backup.log"
BACKUP_FILE="$BACKUP_DIR/documents_backup_$DATE.tar.gz"

# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"

# Log the start time
echo "Backup started at $(date)" >> "$LOG_FILE"

# Perform the backup and log success or failure
if tar -czf "$BACKUP_FILE" "$SOURCE_DIR"; then
    echo "Backup successful: $BACKUP_FILE" >> "$LOG_FILE"
else
    echo "Backup failed at $(date)" >> "$LOG_FILE"
    exit 1
fi

echo "Backup completed at $(date)" >> "$LOG_FILE"

This version of the script includes logging and stops the script if an error occurs, improving reliability and traceability.

Step 4: Scheduling the Backup Script with Cron

To automate the backup, schedule the script using cron. Open your crontab file with crontab -e and add an entry to run the backup daily at midnight:

# Run backup script daily at midnight
0 0 * * * /home/user/backup.sh

This cron job runs the backup script every day at midnight, ensuring daily backups without manual intervention.

Step 5: Restoring from a Backup

To restore from a backup, you can extract the compressed file to the desired directory:

# Example: Restoring backup
tar -xzf /home/user/backup/documents_backup_2023-01-01.tar.gz -C /home/user/documents

This command extracts a specific backup file and restores it to the original directory.

Summary and Next Steps

In this project, we created an automated backup script that compresses files, handles errors, and logs output. By scheduling the script with cron, we automated daily backups to ensure file safety and reliability. In the next chapter, we’ll discuss best practices for writing efficient and maintainable Bash scripts to further enhance your scripting skills.