This is a simple Bash script to back up files that were modified in the last 24 hours from a target directory to a destination directory.
Run the script using the following command:
./backup.sh target_directory_name destination_directory_name./backup.sh /home/user/documents /home/user/backups- The script checks if exactly two arguments are provided (target and destination directories).
- It verifies that both arguments are valid directory paths.
- It calculates the current timestamp and creates a backup file name based on it.
- It identifies files in the target directory that were modified in the last 24 hours.
- If modified files exist, it compresses them into a
.tar.gzarchive. - The backup file is moved to the destination directory.
- If files have been modified in the last 24 hours, you will see:
Backup created: backup-<timestamp>.tar.gz Backup moved to: destination_directory - If no files were modified:
No files modified in the last 24 hours. No backup created. - If incorrect usage:
Usage: backup.sh target_directory_name destination_directory_name - If invalid directory paths are provided:
Invalid directory path provided
- Linux/macOS with Bash
tarinstalled
- Ensure the script has execution permissions:
chmod +x backup.sh
- Run the script with the correct directory paths.
- The backup file will be stored in the specified destination directory.
- Script by Ash Nicolus
This script is open-source and can be modified as needed.