|
1 | 1 | # Linux-commands-and-Shell-Scripts
|
2 | 2 | Hands-on Introduction to Linux Commands and Shell Scripting by IBM on Coursera
|
| 3 | + |
| 4 | +Finel Project File: |
| 5 | +backup.sh: |
| 6 | + |
| 7 | +#!/bin/bash |
| 8 | + |
| 9 | +# This checks if the number of arguments is correct |
| 10 | +# If the number of arguments is incorrect ( $# != 2) print error message and exit |
| 11 | +if [[ $# != 2 ]] |
| 12 | +then |
| 13 | + echo "backup.sh target_directory_name destination_directory_name" |
| 14 | + exit |
| 15 | +fi |
| 16 | + |
| 17 | +# This checks if argument 1 and argument 2 are valid directory paths |
| 18 | +if [[ ! -d 1ドル ]] || [[ ! -d 2ドル ]] |
| 19 | +then |
| 20 | + echo "Invalid directory path provided" |
| 21 | + exit |
| 22 | +fi |
| 23 | + |
| 24 | +# [TASK 1] |
| 25 | +targetDirectory=1ドル |
| 26 | +destinationDirectory=2ドル |
| 27 | + |
| 28 | +# [TASK 2] |
| 29 | +echo "targetDirectory is 1ドル" |
| 30 | +echo "destinationDirectory is 2ドル" |
| 31 | + |
| 32 | +# [TASK 3] |
| 33 | +currentTS=$(date +%s) |
| 34 | + |
| 35 | +# [TASK 4] |
| 36 | +backupFileName="backup-$currentTS.tar.gz" |
| 37 | + |
| 38 | +# We're going to: |
| 39 | + # 1: Go into the target directory |
| 40 | + # 2: Create the backup file |
| 41 | + # 3: Move the backup file to the destination directory |
| 42 | + |
| 43 | +# To make things easier, we will define some useful variables... |
| 44 | + |
| 45 | +# [TASK 5] |
| 46 | +origAbsPath=$(pwd) |
| 47 | + |
| 48 | +# [TASK 6] |
| 49 | +cd $destinationDirectory |
| 50 | +destAbsPath=$destinationDirectory |
| 51 | + |
| 52 | +# [TASK 7] |
| 53 | +cd $origAbsPath |
| 54 | +cd $targetDirectory |
| 55 | + |
| 56 | +# [TASK 8] |
| 57 | +yesterdayTS=$(($currentTS - 24 * 60 * 60)) |
| 58 | + |
| 59 | +declare -a toBackup |
| 60 | + |
| 61 | +for file in $(ls) # [TASK 9] |
| 62 | +do |
| 63 | + # [TASK 10] |
| 64 | + if ((`date -r $file +%s` > $yesterdayTS)) |
| 65 | + then |
| 66 | + # [TASK 11] |
| 67 | + toBackup+=($file) |
| 68 | + fi |
| 69 | +done |
| 70 | + |
| 71 | +# [TASK 12] |
| 72 | +tar -czvf $backupFileName ${toBackup[@]} |
| 73 | + |
| 74 | +# [TASK 13] |
| 75 | +mv $backupFileName $destAbsPath |
| 76 | + |
| 77 | +# Congratulations! You completed the final project for this course! |
| 78 | + |
0 commit comments