Skip to main content

Command Palette

Search for a command to run...

Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation

Published
3 min read
S

"Passionate about simplifying the complexities of DevOps, I bring 5 years of hands-on experience in supporting development and operations teams to achieve faster, more reliable software delivery. I thrive on optimizing CI/CD pipelines, automating workflows, and troubleshooting challenges across diverse cloud environments. My expertise spans across infrastructure management, continuous integration, and performance monitoring—ensuring stability and efficiency at every stage of the software lifecycle. Always eager to learn, collaborate, and innovate, I’m committed to driving impactful change in the DevOps space."

The "Shell Scripting Challenge: Directory Backup with Rotation" typically refers to a task where you create a shell script that performs backups of a specified directory, while managing old backups in a rotating manner. This ensures that you have a certain number of recent backups while older ones are deleted or archived to save space.

Key Components of the Challenge

  1. Backup Creation:

    • The script should create a backup of a specified directory. This can be done using tools like tar, cp, or rsync.
  2. Backup Rotation:

    • The script should implement a rotation system, where only a fixed number of recent backups are kept. For example, if you want to keep the last 5 backups, the script should delete the oldest backups when a new one is created.
  3. Usage Parameters:

    • The script should accept parameters such as the source directory (the one to be backed up) and the destination directory (where backups are stored).
  4. Timestamping:

    • Each backup should have a unique name, typically including a timestamp, to differentiate between backups.
  5. Error Handling:

    • The script should handle potential errors, such as invalid paths or insufficient permissions, and provide useful output messages.

Example Outline of a Backup Script

Here’s a basic outline of how such a script might look:

bashCopy code#!/bin/bash

# Usage message
function display_usage {
    echo "Usage: $0 <source_directory> <backup_directory> <number_of_backups>"
}

# Check for the correct number of arguments
if [ $# -ne 3 ]; then
    display_usage
    exit 1
fi

SOURCE_DIR=$1
BACKUP_DIR=$2
NUM_BACKUPS=$3

# Create the backup
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_FILE="$BACKUP_DIR/backup_$TIMESTAMP.tar.gz"

# Create the backup using tar
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"

# Rotate backups
cd "$BACKUP_DIR" || exit
ls -1t backup_*.tar.gz | tail -n +$((NUM_BACKUPS + 1)) | xargs -r rm

echo "Backup created: $BACKUP_FILE"

Explanation of the Script

  1. Function Definition: The display_usage function shows how to use the script.

  2. Argument Checking: The script checks that exactly three arguments are provided.

  3. Variables:

    • SOURCE_DIR: The directory to back up.

    • BACKUP_DIR: The directory where backups will be stored.

    • NUM_BACKUPS: The maximum number of backups to keep.

  4. Backup Creation: The script creates a timestamped tarball of the source directory.

  5. Backup Rotation: It lists backups, sorts them by time, and removes the oldest ones if the count exceeds the specified limit.

  6. Completion Message: It echoes a message indicating where the backup was saved.

Running the Script

To run the script:

  1. Save it as backup.sh.

  2. Make it executable:

     bashCopy codechmod +x backup.sh
    
  3. Execute it with the required arguments:

     bashCopy code./backup.sh /path/to/source /path/to/backup 5
    

Step:1

Step :2

Step 3:

Step 4:

Step 5:

Step 6:

Step 7:

Step 8:

Step 9:

Step 10:

Step 11:

Step 12:

In this blog, we explored how to create a robust shell script for directory backups with rotation. We covered the essential components of the script, including how to back up a specified directory, manage old backups, and implement error handling. By using timestamps and command-line parameters, you can customize your backups to fit your needs while ensuring that storage space is efficiently utilized.

By following the outlined steps and utilizing the example script, you can easily set up your own backup system. Remember, regular backups are crucial for data security, and automation through shell scripting makes it easier than ever!

Happy scripting! 🚀💻📂✨

If you have any questions or need further assistance, feel free to reach out!

"The DevOps Insight and Learning with Shivani"

Part 15 of 22

"DevOps Insight with Shivani" is a beginner-friendly series exploring DevOps tools, practices, and concepts like CI/CD pipelines. Join me for easy tutorials, practical tips, and industry updates to help you learn and grow in the DevOps world.

Up next

Day 8: Understanding Package Manager and Systemctl

Task 1 : Install Docker and Jenkins: Install Docker and Jenkins on your system from your terminal using package managers.Update the package list and install required packages: 🚀 How to Install Jenkins and Docker on Ubuntu sudo apt update sudo apt ...

More from this blog

T

The DevOps Insight with Shivani

23 posts