Day 9 of My 90 Days of DevOps: Shell Scripting Challenge - Directory Backup with Rotation
Shubham Niranjan
NOC Engineer | ?? AWS | ?? Linux | ?? Docker | ?? Kubernetes | ?? Jenkins | ?? Git & GitHub | ?? Python | ??? CI/CD | ?? Cloud Infrastructure | ?? Automation | ?? Monitoring & Logging | ?? DevOps Enthusiast
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:
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:
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:
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:
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
Software Engineer At Motherson Technology Service Limited
5 个月Very helpful
IT Analyst In Microsoft Intune/O365 Mobility /MDM&MAM /JAMF 100 &170 /Remote Desktop support in Linux shell & windows
5 个月Pakka