LimitDiskUsage

From RdiffBackupWiki

Jump to: navigation, search

This scripts currently works only if the backup destination is local, but it should be easy to adapt this for remote backups by using ssh to call du remotely.

#!/bin/bash

# backup destination dir
backupdir=/mnt/backups/foo

# rdiff-backup binary
rdiffbackup=/usr/bin/rdiff-backup

# remove old increments from the backup as long as du reports
# more bytes used by the backup dir than the following limit
maxbytes=$((350 * 1024 * 1024 * 1024))

# but never delete increments younger than the following limit in seconds
keepseconds=$((7 * 60 * 60 * 24))

# gather the list of timestamps of all backup increments into an array
increments=($($rdiffbackup --list-increments --parsable-output $backupdir | while read a b; do echo $a; done))

# while we still have at least two increments,
# and we are allowed to delete the oldest increment,
# and du reports more disk usage than allowed,
# remove the oldest increment
while ((
  ${#increments[*]} > 1 &&
  $(date +%s) - ${increments[0]} > $keepseconds &&
  $(du --block-size=1 --summarize $backupdir | while read a b; do echo $a; done) > $maxbytes ))
do
  $rdiffbackup --remove-older-than $((${increments[0]} + 1)) $backupdir || exit
  unset increments[0]
  increments=(${increments[*]})
done
Personal tools