Day 9 of My 90 Days of DevOps: Shell Scripting Challenge - Directory Backup with Rotation

Day 9 of My 90 Days of DevOps: Shell Scripting Challenge - Directory Backup with Rotation

Hello LinkedIn Community,

As part of my 90-day DevOps journey, I’m excited to share my progress on Day 9! Today’s task was a challenging yet rewarding exercise in shell scripting. The goal was to create a bash script that performs directory backups with a rotation mechanism to keep only the last three backups. Here’s a detailed look at the task and how I approached it.


Challenge Description


The task was to create a bash script that:


  1. Takes a directory path as a command-line argument.
  2. Creates timestamped backup folders and copies all files from the specified directory into these backup folders.
  3. Implements a rotation mechanism to keep only the last three backups, removing the oldest ones if there are more than three.


Example Usage


Assume the script is named backup_with_rotation.sh. Here’s how it works:


First Execution (2023-07-30):


$ ./backup_with_rotation.sh /home/user/documents
        


Output:


Backup created: /home/user/documents/backup_2023-07-30_12-30-45
Backup created: /home/user/documents/backup_2023-07-30_15-20-10
Backup created: /home/user/documents/backup_2023-07-30_18-40-55
        


After this execution, the /home/user/documents directory will contain:


  • backup_2023-07-30_12-30-45
  • backup_2023-07-30_15-20-10
  • backup_2023-07-30_18-40-55
  • file1.txt
  • file2.txt


Second Execution (2023-08-01):


$ ./backup_with_rotation.sh /home/user/documents
        


Output:


Backup created: /home/user/documents/backup_2023-08-01_09-15-30
        


After this execution, the /home/user/documents directory will contain:


  • backup_2023-07-30_15-20-10
  • backup_2023-07-30_18-40-55
  • backup_2023-08-01_09-15-30
  • file1.txt
  • file2.txt


The Script


Here’s the bash script I created to accomplish this task:


#!/bin/bash

# Check if directory path is provided
if [ -z "$1" ]; then
  echo "Usage: $0 <directory_path>"
  exit 1
fi

# Directory to backup
DIR=$1

# Create a timestamp
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")

# Backup directory name
BACKUP_DIR="$DIR/backup_$TIMESTAMP"

# Create the backup directory
mkdir -p "$BACKUP_DIR"

# Copy all files to the backup directory
cp -r "$DIR"/* "$BACKUP_DIR"

echo "Backup created: $BACKUP_DIR"

# Find and delete old backups, keeping only the last 3
cd "$DIR"
BACKUPS=($(ls -d backup_* | sort -r))
COUNT=${#BACKUPS[@]}

if [ $COUNT -gt 3 ]; then
  for (( i=3; i<$COUNT; i++ )); do
    rm -rf "${BACKUPS[$i]}"
    echo "Old backup removed: ${BACKUPS[$i]}"
  done
fi
        


Script Breakdown with Example


Let’s break down the script step-by-step with an example:


  1. Check for Directory Path: The script first checks if a directory path is provided as an argument. If not, it exits with a usage message.
  2. Set Directory and Timestamp: The script sets the directory to backup and creates a timestamp for the backup folder name.
  3. Create Backup Directory: It then creates a new backup directory with the timestamp.
  4. Copy Files to Backup Directory: All files from the specified directory are copied into the new backup directory.
  5. Backup Rotation: The script checks for existing backup folders and removes the oldest ones if there are more than three.


Reflection

This task was a great way to practice shell scripting and understand the importance of backup rotation in maintaining a clean and efficient file system. By automating the backup process and ensuring only the most recent backups are kept, we can save storage space and reduce clutter.


I’m looking forward to the upcoming challenges and continuing to share my journey with you all. Stay tuned for more updates!


Happy Learning!

Feel free to connect with me and follow my journey as I dive deeper into the world of DevOps. Your support and feedback are always appreciated!

#DevOps #ShellScripting #Automation #LearningJourney #90DaysOfDevOps



Harsh Kumar

Software Engineer At Motherson Technology Service Limited

5 个月

Very helpful

回复
Shyam Sundar

IT Analyst In Microsoft Intune/O365 Mobility /MDM&MAM /JAMF 100 &170 /Remote Desktop support in Linux shell & windows

5 个月

Pakka

回复

要查看或添加评论,请登录

Shubham Niranjan的更多文章

社区洞察

其他会员也浏览了