This is a simple shell script that performs backup operations from a source directory to a destination directory. The script checks if the correct number of arguments is provided, validates the directory paths, and performs the backup based on specific tasks.
- This script requires Bash to be installed on your system.
./backup.sh target_directory_name destination_directory_name
target_directory_name
: The path to the directory you want to back up.destination_directory_name
: The path to the directory where the backup file will be stored.
- Checks if the correct number of arguments is provided. If not, it displays an error message and exits.
- Validates the provided directory paths, ensuring they exist.
- Prints the target directory and destination directory paths.
- Generates a backup file name using the current timestamp.
- Saves the current working directory as the original absolute path.
- Changes the working directory to the destination directory.
- Saves the destination directory's absolute path.
- Calculates the timestamp for yesterday.
- Iterates through the files in the target directory.
- Displays each file's modification date and compares it with yesterday's timestamp.
- Adds files modified within the last 24 hours to the backup list.
- Creates a tar.gz backup file using the generated backup file name and the files in the backup list.
- Moves the created backup file to the destination directory.
./backup.sh /path/to/source /path/to/destination
- The script will perform a backup operation from the
/path/to/source
directory to the/path/to/destination
directory. It will create a backup file with a name likebackup-[timestamp].tar.gz
containing files modified within the last 24 hours.