-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
79 lines (61 loc) · 1.55 KB
/
backup.sh
File metadata and controls
79 lines (61 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/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 "First argument: $1"
echo "Second argument: $2"
# [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"
destDirAbsPath=$(pwd)
# [TASK 7]
cd "$origAbsPath"
cd "$targetDirectory"
# [TASK 8]
yesterdayTS=$(($currentTS - 24 * 60 * 60))
#find /path/to/directory -type f -newermt "$(date -d @$yesterdayTS '+%Y-%m-%d %H:%M:%S')"
declare -a toBackup
for file in * # [TASK 9]
do
# [TASK 10]
file_last_modified_date=$(date -r "$file" +%s)
if [[ $file_last_modified_date -gt $yesterdayTS ]]
then
# [TASK 11]
toBackup+=($file)
fi
done
# [TASK 12]
if [ ${#toBackup[@]} -gt 0 ];
then
tar -czvf $backupFileName ${toBackup[@]}
# [TASK 13]
mv "$backupFileName" "$destDirAbsPath"
echo "Backup moved to: $destDirAbsPath"
else
echo "No files were modified in the last 24 hours."
fi