Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation
Table of Contents
Challenge Description
Steps ๐
Create a Folder and Make Some Files
Install zip (if not already installed) ๐ฆ
crontab Job Scheduling ๐
Bash Script for Backup and Rotation ๐ ๏ธ
Example Crontab Job ๐
Explanation ๐
Challenge Description
The challenge is to create a Bash script that accepts a directory as a command-line argument and performs a backup. The script should:
Create timestamped backups in a specified directory.
Implement a rotation mechanism to retain only the last three backups.
Steps ๐
1. Create a Folder and Make Some Files
Start by simulating the data to be backed up. Use the following commands to create a directory and some sample files:
mkdir /root/datafile
touch /root/datafile/file1.txt
touch /root/datafile/file2.txt
2. Install zip (if not already installed) ๐ฆ
Ensure that the zip
command is available, as it will be used for compression. Check if zip
is installed by typing:
zip
If zip
is not installed, install it using:
sudo apt install zip
3. crontab Job Scheduling ๐
You can automate your backup process by scheduling the script using crontab
. For instance, to run the script every hour:
Open
crontab
for editing:crontab -e
Add the following line to schedule the job:
* 1 * * * bash /root/backup.sh /root/datafile /root/backup
This will execute the backup script every hour, storing backups in /root/backup
.
4. Bash Script for Backup and Rotation ๐ ๏ธ
Below is the script that performs timestamped backups and implements backup rotation:
#!/bin/bash
# Check if the required directories are provided
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 <source_directory> <backup_directory>"
exit 1
fi
SOURCE_DIR="$1"
BACKUP_DIR="$2"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_NAME="backup_$TIMESTAMP.zip"
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"
# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Create a backup of the source directory
zip -r "$BACKUP_PATH" "$SOURCE_DIR"
# Output the backup creation message
echo "Backup created: $BACKUP_PATH"
# Implement backup rotation - Keep only the last 3 backups
BACKUPS=("$BACKUP_DIR"/backup_*.zip)
BACKUP_COUNT="${#BACKUPS[@]}"
if [ "$BACKUP_COUNT" -gt 3 ]; then
ls -1t "${BACKUPS[@]}" | tail -n +4 | xargs -r rm
echo "Old backups removed, keeping only the last 3 backups."
fi
5. Example Crontab Job ๐
To automate this process, add the following line to your crontab
configuration:
* 1 * * * bash /root/backup.sh /root/datafile /root/backup
This will ensure that:
Backups are created every hour. ๐
Old backups are rotated out, keeping only the latest three. ๐
Explanation ๐
Backup Creation ๐พ
The script accepts two arguments: the source directory and the backup directory.
It generates a timestamp and creates a compressed backup file named
backup_YYYY-MM-DD_
HH-MM-SS.zip
.The backup is stored in the specified backup directory using the
zip
command.
Backup Rotation ๐
The script calculates the number of backup files in the backup directory.
If more than three backups exist, it deletes the oldest ones to maintain only the last three.
This ensures efficient storage management while preserving recent backups.
By following these steps, you can create a robust and automated backup system for your files! ๐๐