Task: Shell Scripting Challenge - Directory Backup with Rotation. This script will create timestamped backups of a specified directory and manage the backups by keeping only the last three.
Backup Script: vim backup_with_
rotation.sh
#!/bin/bash
<< readme
This is a script for backup with a 3-day rotation.
Usage:
./backup.sh <path to your source> <path to backup folder>
readme
# Function to display usage information
function display_usage {
echo "Usage: ./backup.sh <path to your source> <path to backup folder>"
echo "./backup.sh <what to backup> <where to backup>"
}
# Check if no arguments are provided
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
# Assign command line arguments to variables
source_dir=$1
timestamp=$(date '+%Y-%m-%d-%H-%M-%S')
backup_dir=$2
# Function to create a backup
function create_backup {
zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null
if [ $? -eq 0 ]; then
echo "Backup generated successfully for ${timestamp}"
else
echo "Error creating backup."
exit 1
fi
}
# Function to perform backup rotation
function perform_rotation {
backups=($(ls -t "${backup_dir}/backup_"*.zip 2>/dev/null))
if [ "${#backups[@]}" -gt 3 ]; then
echo "Performing rotation for 3 backups"
backups_to_remove=("${backups[@]:3}") # Keep backups after the first 3
for backup in "${backups_to_remove[@]}"; do
rm -f "${backup}"
done
echo "Old backups removed."
fi
}
# Run the functions
create_backup
perform_rotation
Line-by-Line Explanation
Shebang:
#!/bin/bash
Specifies that the script should be executed using the Bash shell.
Multi-line Comment:
<< readme This is a script for backup with a 3-day rotation. ... readme
Describes the script and its usage. It utilizes a document for multi-line comments.
Function to Display Usage Information:
function display_usage { echo "Usage: ./backup.sh <path to your source> <path to backup folder>" echo "./backup.sh <what to backup> <where to backup>" }
This function outputs instructions on using the script if the required arguments are not provided.
Argument Check:
if [ $# -eq 0 ]; then display_usage exit 1 fi
Check if the number of arguments passed to the script is zero. If true, it calls the
display_usage
function and exits.Variable Assignments:
source_dir=$1 timestamp=$(date '+%Y-%m-%d-%H-%M-%S') backup_dir=$2
source_dir=$1
: Stores the first argument (source directory) insource_dir
.timestamp=$(date '+%Y-%m-%d-%H-%M-%S')
: Generates a timestamp for naming the backup file.backup_dir=$2
: Stores the second argument (backup directory) inbackup_dir
.
Function to Create Backup:
function create_backup { zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null if [ $? -eq 0 ]; then echo "Backup generated successfully for ${timestamp}" else echo "Error creating backup." exit 1 fi }
This function zips the source directory into a timestamped zip file in the backup directory and checks if the operation was successful.
Function to Perform Backup Rotation:
function perform_rotation { backups=($(ls -t "${backup_dir}/backup_"*.zip 2>/dev/null)) if [ "${#backups[@]}" -gt 3 ]; then echo "Performing rotation for 3 backups" backups_to_remove=("${backups[@]:3}") # Keep backups after the first 3 for backup in "${backups_to_remove[@]}"; do rm -f "${backup}" done echo "Old backups removed." fi }
This function lists all zip files in the backup directory, sorted by modification time.
It checks if there are more than three backups and removes the oldest ones if necessary.
Run the Functions:
create_backup perform_rotation
Calls the
create_backup
function to create a new backup and thenperform_rotation
to manage old backups.Functionality Breakdown
Directory Check: The script checks if a directory is provided and if it exists.
Timestamp Creation: It generates a timestamp to name the backup folder.
Backup Creation: It copies all files from the specified directory to the new backup folder.
Backup Rotation: It checks the number of existing backup folders and removes the oldest ones if there are more than three.
Summary
This script efficiently backs up a specified source directory, creating a timestamped zip file in a designated backup directory. It ensures that only the three most recent backups are kept, automatically deleting older backups as necessary. You can easily modify it for your specific needs, such as changing the backup format or the rotation policy.