Accueil > Informatique > Sauvegarde différentielle sous Linux

Sauvegarde différentielle sous Linux

Toute la puissance de Linux… Ce script n’est pas de moi (je l’ai simplement légèrement adapté), mais j’ai malheureusement perdu la source…
Il permet de gérer des sauvegardes différentielles avec historique. En plus grace à la technique des « hardlinks » la consommation en espace est minimale. Je l’utilise partout et franchement ça marche du feu de de Dieu…


#!/bin/bash -x
#
# To launch it every day at 4AM, add the following line to /etc/crontab :
# 00 4 * * * root /usr/local/scripts/backupRsync.sh

### PARAMETRES A ADAPTER
LOGIN_USER="nom_utilisateur"
BACKUPSERVER="adresse_serveur"
SSH_PORT="port_ssh"
BACKUP_TO_DIR="repertoire_destination_backup"

DIRECTORIES2BACKUP="liste_des_reperoires_a_sauvegarder_separes_par_des_espaces"
EXCLUDES="liste_des_repertoires_ou_fichiers_a_exclure_separes_par_des_espaces"
NUMBER_OF_ARCHIVES=10
NICE_LEVEL=3
### FIN DES PARAMETRES A ADAPTER

#!/bin/bash
# ----------------------------------------------------------------------
# mikes handy rotating-filesystem-snapshot utility: daily snapshots
# See http://www.mikerubel.org/computers/rsync_snapshots/
# ----------------------------------------------------------------------

# make sure we're running as root
if (( `id -u` != 0 ));
then
  { echo "Sorry, must be root.  Exiting..."; exit; }
fi

# step 1: delete the oldest snapshot:
ssh -p $SSH_PORT -l $LOGIN_USER $BACKUPSERVER rm -rf \
    $BACKUP_TO_DIR/daily.$NUMBER_OF_ARCHIVES

# step 2: shift the middle snapshots(s) back by one, if they exist
for i in `seq $NUMBER_OF_ARCHIVES -1 2`; do
  PREVIOUS=`expr $i - 1`
  ssh -p $SSH_PORT -l $LOGIN_USER $BACKUPSERVER \
    mv $BACKUP_TO_DIR/daily.$PREVIOUS $BACKUP_TO_DIR/daily.$i
done

# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,
# if that exists

ssh -p $SSH_PORT -l $LOGIN_USER $BACKUPSERVER [ -d $BACKUP_TO_DIR/daily.0 ]
if [ $? -eq 0 ]
then
  echo "Moving DIRECTORY"
  ssh -p $SSH_PORT -l $LOGIN_USER $BACKUPSERVER \
    cp -al $BACKUP_TO_DIR/daily.0 $BACKUP_TO_DIR/daily.1
else
  echo "Creating DIRECTORY"
  ssh -p $SSH_PORT -l $LOGIN_USER $BACKUPSERVER mkdir $BACKUP_TO_DIR/daily.0
fi

# step 4: rsync from the system into the latest snapshot (notice that
# rsync behaves like cp --remove-destination by default, so the destination
# is unlinked first.  If it were not so, this would copy over the other
# snapshot(s) too!
for d in $DIRECTORIES2BACKUP
do
  nice --adjust $NICE_LEVEL rsync -va -e "ssh -p $SSH_PORT" --progress \
    --relative --delete --delete-excluded $EXCLUDES $d \
    $LOGIN_USER@$BACKUPSERVER:$BACKUP_TO_DIR/daily.0
done

# step 5: update the mtime of daily.0 to reflect the snapshot time
ssh -p $SSH_PORT -l $LOGIN_USER $BACKUPSERVER touch $BACKUP_TO_DIR/daily.0

Categories: Informatique Tags: .: GNU/Linux .: rsync .: sauvegarde .: script .: shell
  1. daniel
    26/03/2009 à 12:08 | #1

    merci !

  1. Pas encore de trackbacks