Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation
Challenge Description
The task is to create a bash script that takes a directory as a command-line argument and performs a backup. The script should create timestamped backup folders and copy all the files from the specified directory into the backup folder. Additionally, it should implement a rotation mechanism to keep only the last 3 backups.
Steps ๐
1. Create a Folder and Make Some Files
First, you'll need to create a directory with some files to simulate the data that will be backed up. ๐ Use the following commands to create the directory and files: ๐ป
mkdir /root/datafile
touch /root/datafile/file1.txt
touch /root/datafile/file2.txt
2. Install zip
(if not already installed) ๐ฆ
Before running the script, ensure that zip
is installed, as it will be used to compress the backups. To check if zip
is installed, type: ๐
zip
If zip
is not installed, install it using:
sudo apt install zip
3. crontab
Job Scheduling ๐
You can schedule your backup script to run periodically using crontab
. For example, if you want the script to run every hour, do the following: โฐ
Open crontab for editing:
crontab -e
Add the following line to schedule the job to run every hour:
* 1 * * * bash /root/backup.sh /root/datafile /root/backup
This will run your backup script every hour and store backups in /root/backup
.
4. Bash Script for Backup and Rotation ๐ ๏ธ
Here is the bash script that will create timestamped backups and implement a rotation mechanism to delete older backups, keeping only the last 3 backups. ๐๐
#!/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 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
BACKUP_COUNT=$(ls -1 "$BACKUP_DIR" | grep "backup_.*.zip" | wc -l)
if [ "$BACKUP_COUNT" -gt 3 ]; then
# Delete the oldest backups
ls -1t "$BACKUP_DIR"/backup_*.zip | tail -n +4 | xargs rm
echo "Old backups removed, keeping only the last 3 backups."
fi
Explanation: ๐
Backup Creation: ๐พ
The script takes two arguments: the source directory and the backup directory. ๐
It generates a timestamp and creates a
zip
file of the source directory in the backup directory with a name likebackup_YYYY-MM-DD_
HH-MM-SS.zip
. ๐It uses the
zip
command to compress the backup. ๐ฆ
Backup Rotation: ๐
The script checks how many backup files exist in the backup directory. ๐
If there are more than 3 backups, the script deletes the oldest backups and retains only the 3 most recent backups. ๐๏ธโจ
5. Example Crontab Job ๐
To run this script automatically every hour, add the following line to your crontab: โฐ
* 1 * * * bash /root/backup.sh /root/datafile /root/backup
This will ensure that backups are created every hour โฐ and that older backups are rotated out ๐, keeping only the latest three. ๐โจ
Subscribe to my newsletter
Read articles from Manav Raut directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by