- Introduction
- Project Overview
- Tasks
- Task 1: Set Target and Destination Directories
- Task 2: Display Command Line Arguments
- Task 3: Define Current Timestamp
- Task 4: Define Backup File Name
- Task 5: Define Original Absolute Path
- Task 6: Define Destination Absolute Path
- Task 7: Change to Target Directory
- Task 8: Calculate Yesterday's Timestamp
- Task 9: Get Files in Current Directory
- Task 10: Check File Modification Date
- Task 11: Add Files to Backup List
- Task 12: Archive Files
- Task 13: Move Backup File
- Task 14: Make Script Executable
- Task 15: Schedule Backup Using Cron
- Usage
- Technologies
- Contributing
- Acknowledgments
- License
Welcome to the Automatic Backup Script (backup.sh) project! This script is designed to automate the backup process for encrypted password files that have been updated in the past 24 hours. By eliminating manual intervention, it enhances security, reduces human error, and improves efficiency.
ABC currently suffers from a huge bottleneck: each day, interns must painstakingly access encrypted password files on core servers and back up any files that were updated within the last 24 hours. This process introduces human error, lowers security, and takes an unreasonable amount of work.
As a lead Linux developer at ABC International Inc., you have been tasked with creating a script called backup.sh
which runs every day and automatically backs up any encrypted password files that have been updated in the past 24 hours
This script performs the following tasks:
- Accepts two command-line arguments:
target_directory_name
anddestination_directory_name
. - Captures the current timestamp in seconds.
- Defines the name of the backup file with a timestamp.
- Identifies files in the target directory modified within the last 24 hours.
- Archives and compresses these files into the backup file.
- Moves the backup file to the destination directory.
The script ensures that recent updates to sensitive files are regularly backed up, enhancing data security and reducing manual effort.
targetDirectory=$1
destinationDirectory=$2
echo "Target Directory: $targetDirectory"
echo "Destination Directory: $destinationDirectory"
currentTS=$(date +%s)
backupFileName="backup-$currentTS.tar.gz"
origAbsPath=$(pwd)
cd $destinationDirectory
destAbsPath=$(pwd)
cd $origAbsPath
cd $targetDirectory
yesterdayTS=$((currentTS - 24 * 60 * 60))
for file in $(ls)
file_last_modified_date=$(date -r "$file" +%s)
if ((file_last_modified_date > yesterdayTS)); then
toBackup+=("$file")
tar -czvf "$backupFileName" "${toBackup[@]}"
mv "$backupFileName" "$destAbsPath"
chmod +x backup.sh
sudo cp /path/to/backup.sh /usr/local/bin/
crontab -e
*/1 * * * * /usr/local/bin/backup.sh /home/project/important-documents /home/project
sudo service cron start
sudo service cron stop
crontab -e
0 0 * * * /usr/local/bin/backup.sh /home/project/important-documents /home/project
- Clone this repository to your local machine.
- Navigate to the project directory.
- Open the
backup.sh
script. - Modify the script as needed for your specific use case.
- Save the script and make it executable using the
chmod
command. - Copy the script to
/usr/local/bin/
for system-wide access. - Set up a cron job to schedule backups according to your requirements.
This project leverages the following technologies:
- Bash: The scripting language used to create
backup.sh
. - Linux: The target environment for running the backup script.
- Cron: The cron job scheduler for automating backups.
Contributions to this project are welcome! If you have suggestions or would like to report issues, please open an issue or create a pull request.
This project was completed as part of an educational assignment and is based on the provided datasets from the Chicago Data Portal.
It serves as an assignment for Introduction to Linux Commands and Shell Scripting
course which is part of IBM Data Engineer
Professional Certificate on Coursera.
This project is licensed under the MIT License.