Day -04/100 - Automate Your Backups with a Bash Script
Backups are an essential part of maintaining data security and preventing loss due to accidental deletion or system failures. In this blog post, we'll walk through a simple yet effective Bash script that automates the backup process using rsync.
Script Overview
This script:
Ensures the user provides the correct number of arguments
Checks if rsync is installed
Creates a timestamped backup directory
Uses rsync to copy files efficiently
Retains only the last three backups, deleting older ones
Let's break it down step by step.
The Script
#! /bin/bash
# Check to make sure user has entered exactly two arguments
if [ $# -ne 2 ];
then
echo "Usage: backup.sh "
echo "Please try again"
exit 1
fi
# Check if rsync is installed
if ! command -v rsync > /dev/null 2>&1;
then
echo "The script requires rsync to be installed"
echo "Please use your distribution package manager to install it and try again"
exit 2
fi
# Capture the current date and store it in a format YYYY-MM-DD_HH-MM-SS
current_date=$(date +%Y-%m-%d_%H-%M-%S)
# Define the source and target directories
source_dir="$1"
target_dir="$2"
# Create a backup folder with a timestamp
backup_dir="$target_dir/backup_$current_date"
mkdir -p "$backup_dir"
# Run rsync to perform the backup
rsync -av --delete "$source_dir/" "$backup_dir/" > "$target_dir/backup_$current_date.log"
# Retain only the last 3 backups and delete older ones
find "$target_dir" -maxdepth 1 -type d -name "backup_*" | sort -r | tail -n +4 | xargs -r rm -rf
echo "Backup completed successfully. Backup folder - $backup_dir"
Step-by-Step Explanation
Checking the Number of Arguments
if [ $# -ne 2 ];
then
echo "Usage: backup.sh "
echo "Please try again"
exit 1
fi
The script requires two arguments:
Source directory: The folder you want to back up.
Target directory: The destination where backups will be stored.
If the user doesn't provide exactly two arguments, the script exits with an error message.
Ensuring rsync is Installed
if ! command -v rsync > /dev/null 2>&1;
then
echo "The script requires rsync to be installed"
echo "Please use your distribution package manager to install it and try again"
exit 2
fi
The script checks if rsync is installed. If it's missing, it prompts the user to install it before proceeding.
Creating a Timestamped Backup Directory
current_date=$(date +%Y-%m-%d_%H-%M-%S)
backup_dir="$target_dir/backup_$current_date"
mkdir -p "$backup_dir"
This generates a unique timestamp (YYYY-MM-DD_HH-MM-SS) and creates a backup directory within the target directory.
Running rsync to Perform the Backup
rsync -av --delete "$source_dir/" "$backup_dir/" > "$target_dir/backup_$current_date.log"
The -a flag ensures that symbolic links, permissions, and timestamps are preserved.
The -v flag enables verbose mode to show progress.
The --delete flag removes files in the backup directory that no longer exist in the source directory.
The log of the backup process is saved in the target directory.
Retaining Only the Last 3 Backups
find "$target_dir" -maxdepth 1 -type d -name "backup_*" | sort -r | tail -n +4 | xargs -r rm -rf
This command:
Finds all backup directories (backup_*) in the target folder.
Sorts them in reverse order (newest first).
Retains only the latest three.
Deletes any older backups.
Printing the Success Message
echo "Backup completed successfully. Backup folder - $backup_dir"
How to Use the Script
Save the script as backup.sh.
Make it executable:
chmod +x backup.sh
Run it with the source and target directories:
./backup.sh /path/to/source /path/to/backup/destination
Conclusion
This script is a simple yet powerful way to automate backups. By using rsync, it ensures efficient synchronization while maintaining historical versions. You can schedule it using cron for automatic periodic backups.
Let me know in the comments if you have any questions or improvements!
Feb 20, 2025 - 06:40
0
Backups are an essential part of maintaining data security and preventing loss due to accidental deletion or system failures. In this blog post, we'll walk through a simple yet effective Bash script that automates the backup process using rsync.
Script Overview
This script:
Ensures the user provides the correct number of arguments
Checks if rsync is installed
Creates a timestamped backup directory
Uses rsync to copy files efficiently
Retains only the last three backups, deleting older ones
Let's break it down step by step.
The Script
#! /bin/bash
# Check to make sure user has entered exactly two arguments
if [ $# -ne 2 ];
then
echo "Usage: backup.sh "
echo "Please try again"
exit 1
fi
# Check if rsync is installed
if ! command -v rsync > /dev/null 2>&1;
then
echo "The script requires rsync to be installed"
echo "Please use your distribution package manager to install it and try again"
exit 2
fi
# Capture the current date and store it in a format YYYY-MM-DD_HH-MM-SS
current_date=$(date +%Y-%m-%d_%H-%M-%S)
# Define the source and target directories
source_dir="$1"
target_dir="$2"
# Create a backup folder with a timestamp
backup_dir="$target_dir/backup_$current_date"
mkdir -p "$backup_dir"
# Run rsync to perform the backup
rsync -av --delete "$source_dir/" "$backup_dir/" > "$target_dir/backup_$current_date.log"
# Retain only the last 3 backups and delete older ones
find "$target_dir" -maxdepth 1 -type d -name "backup_*" | sort -r | tail -n +4 | xargs -r rm -rf
echo "Backup completed successfully. Backup folder - $backup_dir"
Step-by-Step Explanation
Checking the Number of Arguments
if [ $# -ne 2 ];
then
echo "Usage: backup.sh "
echo "Please try again"
exit 1
fi
The script requires two arguments:
Source directory: The folder you want to back up.
Target directory: The destination where backups will be stored.
If the user doesn't provide exactly two arguments, the script exits with an error message.
Ensuring rsync is Installed
if ! command -v rsync > /dev/null 2>&1;
then
echo "The script requires rsync to be installed"
echo "Please use your distribution package manager to install it and try again"
exit 2
fi
The script checks if rsync is installed. If it's missing, it prompts the user to install it before proceeding.
This script is a simple yet powerful way to automate backups. By using rsync, it ensures efficient synchronization while maintaining historical versions. You can schedule it using cron for automatic periodic backups.
Let me know in the comments if you have any questions or improvements!