Questo sito utilizza i cookie per migliorare l'esperienza di navigazione. Continuando la navigazione acconsenti all'utilizzo dei cookie.

incremental backup: RSYNC

incremental backup: RSYNC

this is not a tutorial,It 's just something I did during my free evenings and  it works ( likely  ) only on my machine.
You can copy or eventually modify it under your responsability.   

Sysadmin knows there are just two rules :
first rule : backup is your best friend. Second rule : never forget the first one.
So it's surely a good idea to think how keep  updated backups of your system

In the following we want to build a rsync script to make an incremental backup of a set of directories from our hard disk.   
First of all we have to decide which directories we want to backup:

to start working i think the following could be a good idea:

let's check how much space we need initially:  

:~$ sudo du -sh /etc /opt /home
6,0M    /etc
36M    /opt
16G    /home

The first task is to assign the user that will run the script the privilege to run it as root.
In my case the username is "massimo"
so edit as root /etc/sudoers
add the following line to define an alias for  /home/massimo/tools/bin/ 

Cmnd_Alias BACKUP = /home/massimo/tools/bin/

And then assign the your user the privilege to run the script as root without asking any password

The step below is mandatory if you want run your backup scheduled with crontab.

let's have a look on the rsync syntax :

 rsync [OPTION...] SRC... [DEST]

we need a Destination directory, and a lot of options to make the script.
Talking about the destination directory we will mount an external disk:
sudo mount /dev/sdb1 /mnt/hde/

To know the options we can rn rsync with --help :
 -v, --verbose               increase verbosity
 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
     --delete                delete extraneous files from destination dirs
     --link-dest=DIR         hardlink to files in DIR when unchanged
     --exclude=PATTERN       exclude files matching PATTERN
 -P                          same as --partial --progress


we will use the following command :
rsync -av -P --delete $SRC  $BACKUP_HOME/snapshot0 | tee -a  $LOG_FILE

the effect of this command is to copy SRC to $BACKUP_HOME/snapshot0 preserving permissions, ownership, groups, timestamp and soft-links.
the option --delete will remove from destinations files no more present in the source directory.

Finally let's go scripting :
we will make two files, one for the declarations of static constants :
/home/massimo/tools/env/backup.env :

    LOG_FILE=/home/massimo/tools/log/backup_$(date '+%y-%m-%d').log
    echo  "$(date '+%y-%m-%d_%H:%M:%S') - $LOGSTRING"| tee -a  $LOG_FILE

And the script that phisically will do the stuff:

set -x

if [ -f ${ENV_DIR}/backup.env ];then
    . ${ENV_DIR}/backup.env
    logOnFile "env file not found - exit with code: 1"  && exit 1
USERID=$(id -u )
test $USERID -ne 0 && logOnFile "sorry you are not allowed to  execute this backup" && exit 1
df -h  | grep $EXT_MOUNT_POINT > /dev/null
if [ $? -eq 0 ];then
    ### remove the third backup and switch the others
    test -d $BACKUP_HOME/snapshot3  &&   rm -rf $BACKUP_HOME/snapshot3
    logOnFile "removed snapshot3"

    test -d $BACKUP_HOME/snapshot2 &&   mv $BACKUP_HOME/snapshot2 $BACKUP_HOME/snapshot3
    logOnFile "moved snapshot2 on snapshot3"

    test -d $BACKUP_HOME/snapshot1  &&  mv $BACKUP_HOME/snapshot1 $BACKUP_HOME/snapshot2
    logOnFile "moved snapshot 1 on snapshot3"

    test -d $BACKUP_HOME/snapshot0 &&   cp -al  $BACKUP_HOME/snapshot0 $BACKUP_HOME/snapshot1  ||  sudo mkdir -p  $BACKUP_HOME/snapshot0
    for SRC in "${dirs[@]}"
        logOnFile "backup $SRC " # && rsync -av -P --delete  $i    
            rsync -av -P --delete $SRC  $BACKUP_HOME/snapshot0 | tee -a  $LOG_FILE
    logOnFile "external destination not found "     && exit 1

The basic idea is to keep four differents snapshots of the source directories.
The cp -al command will hard link only files instead of creating a phisical copy. This could be magic if you want to save a lot of disk space.

advices to learn more about hard links and rsync :

Please post any comment you want :-)
Scarica questo file (bck.tar.gz)scripts backup rsync[ ]0.7 Kb

Aggiungi commento

Codice di sicurezza