-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
65 lines (48 loc) · 1.64 KB
/
backup.sh
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
#!/bin/bash
# Check for the correct number of command-line arguments
if [[ $# != 2 ]]
then
echo "Usage: backup.sh <target_directory_name> <destination_directory_name>"
exit 1
fi
# Validate that both arguments are directories
if [[ ! -d $1 ]] || [[ ! -d $2 ]]
then
echo "Invalid directory path provided"
exit 1
fi
# Assign command-line arguments to variables for better readability
targetDirectory=$1
destinationDirectory=$2
# Display the directories to be used for backup and destination
echo "Target Directory: $targetDirectory"
echo "Destination Directory: $destinationDirectory"
# Define a variable for the current timestamp
currentTS=$(date +%s)
# Define the backup file name using the current timestamp
backupFileName="backup-${currentTS}.tar.gz"
# Define a variable for the original directory's absolute path
origAbsPath=$(pwd)
# Change to the destination directory and capture its absolute path
cd "$destinationDirectory"
destDirAbsPath=$(pwd)
# Change back to the original directory, then change to the target directory
cd "$origAbsPath"
cd "$targetDirectory"
# Calculate the timestamp for 24 hours ago
yesterdayTS=$(($currentTS - 24 * 60 * 60))
# Initialize an array to hold the list of files to backup
declare -a toBackup
# Populate the toBackup array with files modified in the last 24 hours
for file in $(ls)
do
if [[ $(date -r "$file" +%s) -gt $yesterdayTS ]]
then
toBackup+=("$file")
fi
done
# Create a compressed archive of the files to be backed up
tar -czvf "$backupFileName" "${toBackup[@]}"
# Move the backup archive to the destination directory
mv "$backupFileName" "$destDirAbsPath/"
echo "Backup completed successfully."