Introduction:
This script automates the process of backing up a specified source directory to a destination directory. It appends a timestamp to the backup and provides a log of the action. This approach ensures that backups are organized and easy to manage.
Instructions:
To use this script, follow these steps:
The first command makes the script executable and the second command runs the script using the source and destination directories.
Example with Output:
Here is how the output might look after running the script:
This script automates the process of backing up a specified source directory to a destination directory. It appends a timestamp to the backup and provides a log of the action. This approach ensures that backups are organized and easy to manage.
Bash:
#!/bin/bash
# Validate input parameters
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <source_directory> <destination_directory>"
exit 1
fi
SOURCE_DIR="$1"
DEST_DIR="$2"
# Ensure the source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "Source directory does not exist: $SOURCE_DIR"
exit 1
fi
# Create the destination directory if it doesn't exist
mkdir -p "$DEST_DIR"
# Create a timestamped backup file name
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="$DEST_DIR/backup_$TIMESTAMP.tar.gz"
# Create the backup
tar -czf "$BACKUP_FILE" -C "$SOURCE_DIR" .
# Confirm backup creation
if [ $? -eq 0 ]; then
echo "Backup successfully created at: $BACKUP_FILE"
else
echo "Backup failed"
fi
exit 0
Instructions:
To use this script, follow these steps:
Code:
chmod +x backup.sh
./backup.sh /path/to/source /path/to/destination
Example with Output:
Here is how the output might look after running the script:
Code:
Backup successfully created at: /path/to/destination/backup_20231010_153045.tar.gz