Search code examples
linuxubuntubackupcronrsync

Ubuntu Cronjob with rsync


Currently on workplaces server we run a daily backup, due to size limits we need it to be only each third day (or something like that). We use Rsync to do the backup. What I'm thinking to do is to just change the run time of the script, so insted of daily it will run every third day.

So I want to know wether this is possible? My Concerns are that the size wont shrink because the backup will still do a "3-days backup" insted of just one day. It's hard to explain so I'll show it by exampel.

What I want:

  • Day 1 - Run Backup
  • Day 2
  • Day 3
  • Day 4 - Run Backup
  • Day 5

What I fear will happen:

  • Day 1 - Run Backup
  • Day 2 - Backup applied from Day 4
  • Day 3 - Backup applied from Day 4
  • Day 4 - Run Backup
  • Day 5

the crontab job looks like this:

5 7 * * * ../rsyncsnapshot.sh daily 30

the script looks like this

if [ $# != 2 ]; then
echo "Usage: backup.sh interval_name count"
exit 1
fi

NAME=$1
COUNT=$2

TIMESTAMP=`date -u "+%Y-%m-%d %H:%M:%S%z"`
echo "*** Backup started $TIMESTAMP (interval $NAME, count $COUNT) ***"

echo "Deleting $DEST_DIR/$NAME.$((COUNT-1))"
ssh $DEST_HOST rm -rf $DEST_DIR/$NAME.$(($COUNT-1))

for i in `seq $(($COUNT-1)) -1 2`;
do
  j=$(($i-1))
  echo "Moving $DEST_DIR/$NAME.$j to $DEST_DIR/$NAME.$i"
  ssh $DEST_HOST mv $DEST_DIR/$NAME.$j $DEST_DIR/$NAME.$i
done

echo "Copying $DEST_DIR/$NAME.0 to $DEST_DIR/$NAME.1"
ssh $DEST_HOST cp -al $DEST_DIR/$NAME.0 $DEST_DIR/$NAME.1

echo "Copying source ($SRC) to $DEST_HOST:$DEST_DIR/$NAME.0/"
rsync $RSYNC_ARGS $SRC $DEST_HOST:$DEST_DIR/${NAME}.0/
ssh $DEST_HOST touch $DEST_DIR/$NAME.0

TIMESTAMP=`date -u "+%Y-%m-%d %H:%M:%S%z"`
echo "*** Backup ended $TIMESTAMP ***"
echo "Quota as follows:"
ssh $DEST_HOST quota

Solution

  • To reduce the amount of space you use significantly, you'll need to reduce the number of copies you keep. This is the 2nd argument to the script. So if you run every 3 days, and want to keep a month of backups, change it to:

    ../rsyncsnapshot.sh daily 10