Skip to content

Vrajeshask/Bash_Final_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bash_Final_project

#!/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="$2" destinationDirectory="$1"

[TASK 2]

echo "Target Directory: $targetDirectory" echo "Target Directory: $destinationDirectory"

[TASK 3]

currentTS=$(date +%s)

[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)

[TASK 6]

cd "$destinationDirectory" || exit 1 # <- destDirAbsPath=$(pwd) cd "$origAbsPath" || exit 1

[TASK 7]

cd "$origAbsPath" || exit 1 # Return to the original directory or exit on failure cd "$targetDirectory" || exit 1

[TASK 8]

yesterdayTS=$((currentTS - 24 * 60 * 60))

declare -a toBackup

for file in $(ls) # [TASK 9] do

[TASK 10]

if ((date -r $file +%s > $yesterdayTS)) then toBackup+=($file) # [TASK 11] fi done

[TASK 12]

tar -czvf b a c k u p F i l e N a m e {toBackup[@]}

[TASK 13]

mv "$backupFileName" "$destinationDirectory/"

Congratulations! You completed the final project for this course!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages