I have the following shell script. I am trying to use rsync to copy files from /Desktop/Source to /Desktop/Backup. Before it copies, it is supposed to compare the source to the most recent backup in the backup directory. If the files exist in a prior backup, it is not supposed to copy. So, this is basically rsync using the --compare-dest=DIR option.
For some reason, all files get copied to the new backup directory, even if the exact same file existed in a prior backup. I'm guessing I have an error in my script. I'd appreciate if anyone can spot it. Thanks.
A note: My backup directories within /Desktop/Backup are named with the date/time of backup. So, alphabetical sorting is the same and sorting by date modified, which is how the LastBackup value works.
For some reason, all files get copied to the new backup directory, even if the exact same file existed in a prior backup. I'm guessing I have an error in my script. I'd appreciate if anyone can spot it. Thanks.
A note: My backup directories within /Desktop/Backup are named with the date/time of backup. So, alphabetical sorting is the same and sorting by date modified, which is how the LastBackup value works.
Code:
#!/bin/bash
#Get the name of the most recent directory that was saved:
LastBackup=$(ls ~/Desktop/Backup/ | tail -1)
#A new directory name using today's date:
NewBackup=$(date "+%Y_%m_%d__%H_%M_%S")
#The source directory:
SRC="Users/myUserName/Desktop/Source/"
#The link destination directory:
LNK="Users/myUserName/Desktop/Backup/$LastBackup"
#The rsync options:
OPT="-avh --delete --compare-dest=$LNK"
#The target directory:
TRG="Users/myUserName/Desktop/Backup/$NewBackup"
#Execute the backup
rsync $OPT $SRC $TRG
Last edited by a moderator: