DEV Community

Cover image for Day 9: Shell Scripting Challenge Directory Backup with Rotation
Udoh Deborah
Udoh Deborah

Posted on

Day 9: Shell Scripting Challenge Directory Backup with Rotation

Day 9 task — on day 9, the task is to create a directory Back-up with rotation.

Objective

The objective of day 9 is to write a bash script that can back up a directory and retain only the 3 most recent backups, automatically deleting older ones. This is useful for automating backup tasks while managing disk space efficiently.

🔹 Step 1: Open your Terminal

🔹Step 2: Create a New Bash Script File
In your terminal

nano backup_with_rotation.sh

Enter fullscreen mode Exit fullscreen mode

🔹 Step 3: Paste the Script
Copy and paste this into the terminal window:

#!/bin/bash

# Description:
# This script backs up a directory by creating a timestamped backup folder
# and implements a rotation mechanism to retain only the last 3 backups.

if [ -z "$1" ]; then
  echo "Usage: $0 /path/to/target_directory"
  exit 1
fi

TARGET_DIR="$1"

if [ ! -d "$TARGET_DIR" ]; then
  echo "Error: Directory does not exist: $TARGET_DIR"
  exit 1
fi

TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_NAME="backup_$TIMESTAMP"
BACKUP_PATH="$TARGET_DIR/$BACKUP_NAME"

mkdir "$BACKUP_PATH"
cp -r "$TARGET_DIR"/* "$BACKUP_PATH" 2>/dev/null

echo "Backup created: $BACKUP_PATH"

cd "$TARGET_DIR"
BACKUPS=$(ls -dt backup_* 2>/dev/null)
NUM_BACKUPS=$(echo "$BACKUPS" | wc -l)

if [ "$NUM_BACKUPS" -gt 3 ]; then
  TO_DELETE=$(echo "$BACKUPS" | tail -n +4)
  echo "$TO_DELETE" | xargs rm -rf
  echo "Old backups removed. Only the latest 3 are retained."
fi

Enter fullscreen mode Exit fullscreen mode

Save and exit the text editor.

🔹 Step 4: Make the Script Executable

chmod +x backup_with_rotation.sh

Enter fullscreen mode Exit fullscreen mode

🔹 Step 5: Create a Test Directory:

mkdir test_dir
touch test_dir/file{1..5}.txt

Enter fullscreen mode Exit fullscreen mode

🔹 Step 6: Run the Script

./backup_with_rotation.sh ./test_dir

Enter fullscreen mode Exit fullscreen mode

output:

Backup created: ./test_dir/backup_YYYY-MM-DD_HH-MM-SS
Enter fullscreen mode Exit fullscreen mode

Run multiple times to see the backup rotation in effect.

🔹 Step 7. Create a GitHub Repository
Go to https://github.com/new.

  • Name the Repository - 90DaysOfDevOps-Day9

Choose:

Public or Private

Don't initialize with a README.

Click "Create repository"

🔹 Step 8. Clone the Repository
back in your IDE

git clone https://github.com/your-username/90DaysOfDevOps-Day9.git
cd 90DaysOfDevOps-Day9

Enter fullscreen mode Exit fullscreen mode

🔹 Step9. Move Your Script into the Repo Folder
If your script is outside the cloned folder:

mv ../backup_with_rotation.sh .

Enter fullscreen mode Exit fullscreen mode

🔹 Step 10. Initialize Git
If you didn’t clone, but are working in a folder you want to turn into a repo:

git init
git remote add origin https://github.com/your-username/90DaysOfDevOps-Day9.git

Enter fullscreen mode Exit fullscreen mode

🔹 Step 11. Add and Commit

git add backup_with_rotation.sh
git commit -m "Add Day 9 backup with rotation script"

Enter fullscreen mode Exit fullscreen mode

🔹 Step 7. Push to GitHub

git push origin main

Enter fullscreen mode Exit fullscreen mode

Top comments (0)