Rsync – simple incremental backup v.1.1

Łukasz Piotrowski Comments 0

    Hi.

    A long time ago I published very simple script to making incremental backup. I've updated it changing some functionality.


    Changelog

    v.1.1
    – changed way searching of last directory to make diff
    – changed way removing old backups

    v.1.0
    – First version

    Description

    Script creates incremental backup from local, remote or other place supported by rsync.
    You can set Soruce, Destination and count of saved backups.

    Requirements: rsync, sudo, linux/mac

    Source

    #!/bin/sh
    
    # Destination dir (without last /)
    BACKUP_DIR=/mnt/backup_vpslpiot
    
    # Source Dir
    SOURCE_DIR=root@piotrows.pl:/
    
    # Num of last backups
    NUM_BACKUP=4
    
    
    # Jeżeli nie ma katalogu dla kopii to go tworzymy
    if [ ! -d $BACKUP_DIR ]
    then
            mkdir -p $BACKUP_DIR
    fi
    
    # Wywalamy najstarszy backup
    COUNT=0
    for i in $(find "$BACKUP_DIR" -maxdepth 1 -type d | sort -Vr); 
    do
        if [ "$i" != "$BACKUP_DIR" ]
        then
            COUNT=`expr $COUNT + 1`
    
            if [ $COUNT -gt $NUM_BACKUP ]
            then
                echo "Deleting: $i"
                sudo rm -rf "$i"
            fi
        fi
    done
    
    # Pobieramy ostatni katalog backupu
    i=1
    while [ $i -le 30 ];
    do
        #statements
        LAST_BACKUP=$(date -I -d "$i day ago")
    
        if [ -d $BACKUP_DIR/$LAST_BACKUP ]
        then
            echo "Found newst backup: $LAST_BACKUP"
            break
        fi
        i=`expr $i + 1`
    done
    
    # echo "$LAST_BACKUP"
    
    CURRENT_BACKUP=$(date -I)
    IGNORE_DIR='--exclude=/dev --exclude=/proc --exclude=/tmp --exclude=/sys' 
    
    echo "sudo rsync -avh --progress $IGNORE_DIR --link-dest=$BACKUP_DIR/$LAST_BACKUP $SOURCE_DIR $BACKUP_DIR/$CURRENT_BACKUP"
    notify-send 'Start make Backup'
    
    sudo rsync -ah $IGNORE_DIR --link-dest=$BACKUP_DIR/$LAST_BACKUP $SOURCE_DIR $BACKUP_DIR/$CURRENT_BACKUP
    
    notify-send 'End make Backup'
    Top