Rsync backup with X days retention

rsync is run on backup server, that login to remote servers and backup.

vi /usr/serverok/rsync-backup.sh

Add following code

#!/bin/sh


date

rsync --archive --verbose --exclude-from=/usr/serverok/.rsync-exclude root@SERVER-IP:/ /backup/HOSTNAME-HERE/sda1/
echo "RSYNC: HOSTNAME-HERE..........OK" | mail -s 'HOSTNAME-HERE RSYNC' [email protected]


date

Repeat the rsync/echo lines for each server you need to backup.

We exclude some of the files from backup, this is specified in .rsync-exclude file.

# cat .rsync-exclude 
/boot
/backup
/mnt
/tmp
/cdrom
/media
/proc
/sys
# 

Run it with cronjob

20 0 * * * /usr/serverok/rsync-backup.sh &> /var/log/serverok-rsync-backup.log

Backup Rotation

We rotate backup, so we have 20 days of backups. To do this, run cronjob

0 1 * * * /usr/serverok/rsync-backup-rotate.sh &>  /var/log/serverok-rsync-backup-rotate.log

Create script

vi /usr/serverok/rsync-backup-rotate.sh

With following content

#!/bin/bash

date
BACKUP_IDS=(01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20)
PATHES=(/backup/server10 /backup/server20 /backup/server22 /backup/server24 /backup/server32 /backup/server48)

# Create backup directories

for ((i=0; i < ${#BACKUP_IDS[*]}; i++)); do
        if [ ! -d /backup/${BACKUP_IDS[$i]} ]; then
                mkdir -p /backup/${BACKUP_IDS[$i]}
        fi
done

# Rotate backup directories

mv /backup/${#BACKUP_IDS[*]} /backup/00
for ((i=${#BACKUP_IDS[*]}-2; i >= 0; i-- )); do
        j=$i+1
        mv /backup/${BACKUP_IDS[$i]} /backup/${BACKUP_IDS[$j]}
done

mv /backup/00 /backup/01

# Backup into backup directory 01

touch -m /backup/01/
for ((i=0; i < ${#PATHES[*]}; i++)); do
        mkdir -p /backup/01${PATHES[$i]}/
        rsync -avl --delete --exclude-from=/usr/serverok/excludes.txt --link-dest=/backup/02${PATHES[$i]}/ ${PATHES[$i]}/ /backup/01${PATHES[$i]}/
done
date
exit

Here you need to add a folder for each server on line PATHES.

Here is content of excludes.txt

# cat /usr/serverok/excludes.txt
.svn
.DS_Store
._*
*.log
*.log.gz
*.log.*.gz
access_log
access_log.*
access_ssl_log
access_ssl_log.*
error_log
error_log.*
xferlog_regular
xferlog_regular.*
# 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *