-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ade4888
commit c9ce3c2
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/bin/bash | ||
|
||
# This checks if the number of arguments is correct | ||
# If the number of arguments is incorrect ( $# != 2) print error message and exit | ||
if [[ $# != 2 ]] | ||
then | ||
echo "backup.sh target_directory_name destination_directory_name" | ||
exit | ||
fi | ||
|
||
# This checks if argument 1 and argument 2 are valid directory paths | ||
if [[ ! -d $1 ]] || [[ ! -d $2 ]] | ||
then | ||
echo "Invalid directory path provided" | ||
exit | ||
fi | ||
|
||
# [TASK 1] | ||
targetDirectory=$1 | ||
destinationDirectory=$2 | ||
|
||
# [TASK 2] | ||
echo $targetDirectory | ||
echo $destinationDirectory | ||
|
||
# [TASK 3] | ||
currentTS=`date +"%S"` | ||
echo $currentTS | ||
|
||
# [TASK 4] | ||
backupFileName="backup-$currentTS.tar.gz" | ||
|
||
# We're going to: | ||
# 1: Go into the target directory | ||
# 2: Create the backup file | ||
# 3: Move the backup file to the destination directory | ||
|
||
# To make things easier, we will define some useful variables... | ||
|
||
# [TASK 5] | ||
origAbsPath=`pwd` | ||
echo $origAbsPath | ||
|
||
# [TASK 6] | ||
cd $destinationDirectory # <- | ||
destAbsPath=`pwd` | ||
echo $destAbsPath | ||
|
||
# [TASK 7] | ||
cd $origAbsPath # <- | ||
cd $targetDirectory # <- | ||
|
||
# [TASK 8] | ||
yesterdayTS=$((24 * 60 * 60 - $currentTS)) | ||
|
||
declare -a toBackup | ||
|
||
for file in $(ls) # [TASK 9] | ||
do | ||
# [TASK 10] | ||
if ((`date -r $file +%s` > $yesterdayTS)) | ||
then | ||
# [TASK 11] | ||
toBackup+=($file) | ||
fi | ||
done | ||
|
||
# [TASK 12] | ||
tar -czvf $backupFileName ${toBackup[*]} | ||
|
||
# [TASK 13] | ||
mv $backupFileName $destAbsPath | ||
# Congratulations! You completed the final project for this course! |