1

I am using Rsync to do the incremental backups of the data, but for some reason after 3-4 proper incremental backups, rsync performs Full backup, occupying twice the space, then I quickly run out of space on the backup machine. The scenario is that cron performs the incremental rsync everyday, and then additional script removes certain backups , leaving:

  • 7 daily backups
  • 4 (approx.) weekly backups
  • 12 monthly backups
  • 5 annual backups

Here is the script I am using:

#!/bin/bash

TODAY=$(date +%Y-%m-%d)
DATADIR=/mnt/remote_ananda
BACKUPDIR=/media/backup/data
SCRIPTDIR=/media/scripts
LASTDAYPATH=${BACKUPDIR}/$(ls ${BACKUPDIR} | tail -n 1)
TODAYPATH=${BACKUPDIR}/${TODAY}
if [[ ! -e ${TODAYPATH} ]]; then
        mkdir -p ${TODAYPATH}
fi

rsync -a --link-dest ${LASTDAYPATH} ${DATADIR} ${TODAYPATH} $@

${SCRIPTDIR}/deleteOldBackups.sh

And then the deleteOldBackups.sh looks like:

#!/bin/bash
BACKUPDIR=/media/backup/data

function listYearlyBackups() {
        for i in 0 1 2 3 4 5
                do ls ${BACKUPDIR} | egrep "$(date +%Y -d "${i} year ago")-[0-9]{2}-[0-9]{2}" | sort -u | head -n 1
        done
}

function listMonthlyBackups() {
        for i in 0 1 2 3 4 5 6 7 8 9 10 11 12
                do ls ${BACKUPDIR} | egrep "$(date +%Y-%m -d "${i} month ago")-[0-9]{2}" | sort -u | head -n 1
        done
}

function listWeeklyBackups() {
        for i in 0 1 2 3 4
                do ls ${BACKUPDIR} | grep "$(date +%Y-%m-%d -d "last monday -${i} weeks")"
        done
}

function listDailyBackups() {
        for i in 0 1 2 3 4 5 6
                do ls ${BACKUPDIR} | grep "$(date +%Y-%m-%d -d "-${i} day")"
        done
}

function getAllBackups() {
        listYearlyBackups
        listMonthlyBackups
        listWeeklyBackups
        listDailyBackups
}

function listUniqueBackups() {
        getAllBackups | sort -u
}

function listBackupsToDelete() {
        ls ${BACKUPDIR} | grep -v -e "$(echo -n $(listUniqueBackups) |sed "s/ /\\\|/g")"
}

cd ${BACKUPDIR}
listBackupsToDelete | while read file_to_delete; do
        rm -rf ${file_to_delete}
done

What am I doing wrong that after 3-4 incremental backups rsync does not link the previous, but make the new FULL one ? Thank you

1

Consider replacing your script with rsnapshot, which does exactly what you're trying to do.

It's hard to say exactly why your script is failing without more troubleshooting, but here are some ideas. I see three possible issues that could be causing your problem:

Incorrect $LASTDAYPATH

This could happen if there are stray files/directories getting written to $BACKUPDIR for some reason. Check for this by logging the value of $LASTDAYPATH to a file to make sure it's getting setting to what you expect. If this is the problem, you may be able to fix by using an appropriate glob when setting $LASTDAYPATH:

LASTDAYPATH=${BACKUPDIR}/$(ls -d "${BACKUPDIR}"/2???-??-?? | tail -n 1)

Backed up files or metadata are changing

--link-dest will only link files if the size, mtime, owner, group, and permissions match. So even if the data in the files isn't changing, you would still do a full copy of a file where the timestamp gets changed for some reason. To help debug this issue, compare two backups to see which files have different inode numbers. If the inodes match, the files are linked, if they don't match, then those particular files got copied. Use a command like this to compare $DIR1 and $DIR2:

diff <(cd $DIR1 && find -type f -printf "%i %p\n" | sort -k 2) <(cd $DIR2 && find -type f -printf "%i %p\n" | sort -k 2)

Previous day's backup is incomplete

If the rsync fails for any reason, you will end up with an empty or partial backup but you will still use it as --link-dest because the directory exists. It would be a good idea to rsync to a directory name like partial.${TODAY} and then rename that to $TODAY only if the rsync command completed without error. And combine that with the LASTDAYPATH change above.

1
  • I should have mentioned this earlier: +1 for recommending rsnapshot – roaima Dec 14 '20 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.