DhillanC / github_template_1

This repository contains Dhillan's lab for agile planning
0 stars 0 forks source link

Backup password service #10

Open DhillanC opened 4 months ago

DhillanC commented 4 months ago

As a [role]
I need [function]
So that [benefit]

Details and Assumptions

DhillanC commented 4 months ago

!/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 "Target Directory: $targetDirectory" echo "Destination 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" # I'm adding "" to consider directories with spaces destAbsPath=$(pwd)

[TASK 7]

cd "$origAbsPath" cd "$targetDirectory"

[TASK 8]

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

declare -a toBackup

[TASK 9]

for file in $(ls -1t) do

[TASK 10]

if [[ $(date -r "$file" +%s) -gt $yesterdayTS ]]; then

I'm using -gt to make a numeric comparison instead of a string comparison with >

# [TASK 11]
toBackup+=("$file")

fi done

[TASK 12]

I'm adding a loop to incorporate error handling at tar command

if tar -czvf "$backupFileName" "${toBackup[@]}"; then echo "Backup archive created successfully: $backupFileName" else echo "Error: Failed to create backup archive." exit 1 # Exit the script with an error message fi

[TASK 13]

I'm adding a loop to incorporate error handling at mv command

if mv "$backupFileName" "$destAbsPath"; then echo "Backup file moved to destination directory: $destAbsPath" else echo "Error: Failed to move backup file to destination directory." exit 1 # Exit the script with an error status fi