Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
robertadigre authored Dec 9, 2022
1 parent ade4888 commit c9ce3c2
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions backup.sh
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!

0 comments on commit c9ce3c2

Please sign in to comment.