Sauvegarde automatique 0.9

Ce billet concerne une application, de mon cru, écrite en bash.

Elle utilise l'application dar pour effectuer des sauvegardes et les copier sur un périphérique USB ou/et sur le réseau. Tous les patchs, corrections, améliorations sont les bienvenues sur cette application maison.

Pour fonctionner l'application se configure à l'aide d'un fichier du dossier /etc qui permet de déterminer une liste de dossier à sauvegarder (/etc/autobackup). Dans ce fichier, on y défini le dossier à sauvegarde, le dossier de destination, le nombre de sauvegarde incrémentale (ou dit d'une autre facon, au bout de combien fois faut-il créer une nouvelle sauvegarde compléte?).

Il permet aussi de définir des points de copie où l'on place les différentes sauvegardes. (Ex: un périphérique USB, un dossier SSH). Ces points sont, bien sûr, utilisés seulement s'ils existent. Le fichier est copié sur le périphérique USB, s'il est branché, sur la machine distante si elle est allumée, ....

Il ne reste plus qu'à configurer ces petits scripts et les mettre dans un cron

Contenu exemple du fichier de configuration

# This config file make relation for backup
 
# Options
BACKUP_OPT="-v -s 4613734400 -D -z9 -m 150"
BACKUP_EXCLUDE_EXT="-Z *.avi -Z *.bz2 -Z *.gif -Z *.gz -Z *.jpg -Z *.mov -Z *.mpg -Z *.pbm -Z *.pdf -Z *.png -Z *.pnm -Z *.Z -Z *.zip"
BACKUP_EXCLUDE_PATH="-P phoenix/.bogofilter -P phoenix/.fonts -P phoenix/.kde/cache-maxiding -P phoenix/.kde/socket-maxiding -P phoenix/.kde/tmp-maxiding -P phoenix/.spamassassin -P phoenix/.thumbnails -P phoenix/.turboprint -P phoenix/.unison -P phoenix/.wesnoth -P phoenix/.widelands -P phoenix/.wine* -P phoenix/usr -P phoenix/virtuel -P .Trash-* -P phoenix/tmp"
 
# Copy Backup
COPYTO="usbdrive network"
 
# Copy to usb drive
usbdrive_type="usb"
usbdrive_dev="/dev/usb/disk1"
usbdrive_mountpoint="/media/disque/"
usbdrive_pathname="backup/"
 
# Copy to network
network_type="net"
network_host="miniding"
network_user="phoenix"
# no network_pass : use ssh key for backup
network_dir="/home/phoenix/backup"
 
# Home
home_name=maxiding_home
home_source=/home/
home_destination=/gravure/dar/home/
home_inc_max=10
 
# Etc
etc_name=maxiding_etc
etc_source=/etc/
etc_destination=/gravure/dar/etc/
etc_inc_max=10

Sauvegarde automatique

#!/bin/sh
 
#############################################################################
##   Copyright (C) 2006 by Ulrich Van Den Hekke                            ##
##   ulrich.vdh@free.fr                                                    ##
##                                                                         ##
##   This program is free software; you can redistribute it and/or modify  ##
##   it under the terms of the GNU General Public License as published by  ##
##   the Free Software Foundation; either version 2 of the License, or     ##
##   (at your option) any later version.                                   ##
##                                                                         ##
# /var 
##   This program is distributed in the hope that it will be useful,       ##
##   but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
##   GNU General Public License for more details.                          ##
##                                                                         ##
##   You should have received a copy of the GNU General Public License     ##
##   along with this program; if not, write to the                         ##
##   Free Software Foundation, Inc.,                                       ##
##   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             ##
#############################################################################
 
if [ -e /etc/autobackup ] ; then
    . /etc/autobackup
else
  echo "Please configure the autobackup config file"
  exit 1
fi
 
if [ $# -ne 1 ] ; then
  echo "Please specify a project to backup"
  exit 2
fi
 
# Define name of variable to use
var_name="\$$1_name"
var_source="\$$1_source"
var_destination="\$$1_destination"
var_inc_max="\$$1_inc_max"
 
# Define content of variable for the project
BACKUP_SRC=`eval echo $var_source`
BACKUP_DST=`eval echo $var_destination`
BACKUP_NAME=`eval echo $var_name`
BACKUP_INCREMENTAL_MAX=`eval echo $var_inc_max`
 
if [ -z "$BACKUP_NAME" ] ; then
  echo "Project undefined"
  exit 3
fi
 
BACKUP_BIN=dar
DATE_BACKUP=`date +%Y%m%d`
BACKUP_ARCHIVE="${BACKUP_NAME}_${DATE_BACKUP}"
 
# From config file
# COUNT_INCREMENTAL_BACKUP
# LAST_BACKUP
# LAST_COMPLETE_BACKUP
 
# Some options
# -v verbose
# -c archive name
# -R root place
# -s slice size
# -p slice pause
# -D backup empty dir
# -z9 compression gzip level 9
# -m for minimal size
# -Z exclude from compression
# -P exclude file
 
function start_backup() {
    echo "Backup of $BACKUP_SRC in progress ..."
 
    $BACKUP_BIN -c ${BACKUP_DST}|>${BACKUP_ARCHIVE}|> -R $BACKUP_SRC $BACKUP_OPT $BACKUP_EXCLUDE_EXT $BACKUP_EXCLUDE_PATH > ${BACKUP_DST}|>${BACKUP_NAME}|>.log
}
 
function load_backup_config() {
    echo -n "Load config file ... "
 
    if [ -e ${BACKUP_DST}|>${BACKUP_NAME}|>.config ] ; then
        . ${BACKUP_DST}|>${BACKUP_NAME}|>.config
        echo "loaded."
    else
        COUNT_INCREMENTAL_BACKUP=$BACKUP_INCREMENTAL_MAX
        echo "not found"
    fi
}
 
function save_backup_config() {
    echo "Save backup config file ..."
 
    echo "COUNT_INCREMENTAL_BACKUP=$COUNT_INCREMENTAL_BACKUP" > ${BACKUP_DST}|>${BACKUP_NAME}|>.config
    echo "LAST_BACKUP=$DATE_BACKUP"                          >> ${BACKUP_DST}|>${BACKUP_NAME}|>.config
    echo "LAST_COMPLETE_BACKUP=$LAST_COMPLETE_BACKUP"        >> ${BACKUP_DST}|>${BACKUP_NAME}|>.config
}
 
function define_incremental() {
    echo "There is $COUNT_INCREMENTAL_BACKUP on $BACKUP_INCREMENTAL_MAX"
    if [ $COUNT_INCREMENTAL_BACKUP -lt $BACKUP_INCREMENTAL_MAX  ] ; then
        echo "Backup is incremental from $LAST_COMPLETE_BACKUP"
        COUNT_INCREMENTAL_BACKUP=`expr $COUNT_INCREMENTAL_BACKUP + 1`
        BACKUP_OPT="-A ${BACKUP_DST}${BACKUP_NAME}_${LAST_COMPLETE_BACKUP} ${BACKUP_OPT}"
    else
        echo "Backup is complete"
        COUNT_INCREMENTAL_BACKUP=1
        LAST_COMPLETE_BACKUP=$DATE_BACKUP
    fi
}
 
load_backup_config;
define_incremental;
start_backup;
save_backup_config;

Copie automatique

#!/bin/sh
 
#############################################################################
##   Copyright (C) 2006 by Ulrich Van Den Hekke                            ##
##   ulrich.vdh@free.fr                                                    ##
##                                                                         ##
##   This program is free software; you can redistribute it and/or modify  ##
##   it under the terms of the GNU General Public License as published by  ##
##   the Free Software Foundation; either version 2 of the License, or     ##
##   (at your option) any later version.                                   ##
##                                                                         ##
##   This program is distributed in the hope that it will be useful,       ##
##   but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
##   GNU General Public License for more details.                          ##
##                                                                         ##
##   You should have received a copy of the GNU General Public License     ##
##   along with this program; if not, write to the                         ##
##   Free Software Foundation, Inc.,                                       ##
##   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             ##
#############################################################################
 
if [ -e /etc/autobackup ] ; then
    . /etc/autobackup
else
  echo "Please configure the autobackup config file"
  exit 1
fi
 
if [ $# -ne 1 ] ; then
  echo "Please specify a project to backup"
  exit 2
fi
 
# Define name of variable to use
var_name="\$$1_name"
var_source="\$$1_source"
var_destination="\$$1_destination"
var_inc_max="\$$1_inc_max"
 
# Define content of variable for the project
BACKUP_SRC=`eval echo $var_source`
BACKUP_DST=`eval echo $var_destination`
BACKUP_NAME=`eval echo $var_name`
BACKUP_INCREMENTAL_MAX=`eval echo $var_inc_max`
 
if [ -z "$BACKUP_NAME" ] ; then
  echo "Project undefined"
  exit 3
fi
 
# From config file
# COUNT_INCREMENTAL_BACKUP
# LAST_BACKUP
# LAST_COMPLETE_BACKUP
 
 
function load_backup_config() {
    echo -n "Load config file ... "
 
    if [ -e ${BACKUP_DST}|>${BACKUP_NAME}|>.config ] ; then
        . ${BACKUP_DST}|>${BACKUP_NAME}|>.config
 
        BACKUP_ARCHIVE="${BACKUP_NAME}_${LAST_BACKUP}"
        echo "loaded."
    else
        echo "not found"
        exit 4
    fi
}
 
function save_by_network() {
    echo -n "Save on computer $HOST as $USER on $DIR"
 
    ping -c 3 -W 10 $HOST > /dev/null
 
    if [ $? -ne 0 ] ; then 
        echo "failed"
    else
        DESTINATION="${HOST}:${DIR}"
        if [ -n $USER ] ; then
            DESTINATION="${USER}@${DESTINATION}"
        fi
        scp ${BACKUP_DST}|>${BACKUP_ARCHIVE}|>* $DESTINATION
 
        if [ $? -ne 0 ] ; then 
            echo "failed"
        else
            echo "ok"
        fi
    fi
}
 
function save_by_usb() {
    echo -n "Save on drive $DEV "
 
    if [ -e $DEV ] ; then
        TOUMOUNT=1
        LINECOUNT=`mount | grep $DEV | cut -f 1 -d\  | wc -l `
        if [ $LINECOUNT -gt 0 ] ; then
            MOUNTPOINT=`mount | grep $DEV | cut -f 3 -d\ `
            TOUMOUNT=0
        else
            mount $MOUNTPOINT
 
            if [ $? -ne 0 ] ; then
                echo "mount failed "
            fi
        fi
        if [ -d ${MOUNTPOINT}|>${PATHNAME}|> ] ; then
            for FILE in `ls -1 ${BACKUP_DST}|>${BACKUP_ARCHIVE}|>*` ; do
                FILENAME=`basename $FILE` 
                if [ -e ${MOUNTPOINT}|>${PATHNAME}|>${FILENAME}|> ] ; then
                    echo "exist "
                else
                    cp ${FILE}|> ${MOUNTPOINT}|>${PATHNAME}|>
                fi
            done
 
            echo "ok"
        else
            echo "failed"
        fi
        if [ $TOUMOUNT -eq 1 ] ; then
            umount $MOUNTPOINT
        fi
 
    else
        echo "device not found"
    fi
}
 
function copy_to() {
    echo "Start make copy of backup $BACKUP_NAME"
 
    for NAME in $COPYTO ; do
        var_type="\$${NAME}_type"
        TYPE=`eval echo $var_type`
 
        echo "--> $NAME as $TYPE"
        case $TYPE in
            "usb")
                var_dev="\$${NAME}_dev"
                DEV=`eval echo $var_dev`
 
                var_mountpoint="\$${NAME}_mountpoint"
                MOUNTPOINT=`eval echo $var_mountpoint`
 
                var_pathname="\$${NAME}_pathname"
                PATHNAME=`eval echo $var_pathname`
 
                save_by_usb;
            ;;
 
            "net")
                var_host="\$${NAME}_host"
                HOST=`eval echo $var_host`
 
                var_user="\$${NAME}_user"
                USER=`eval echo $var_user`
 
                var_dir="\$${NAME}_dir"
                DIR=`eval echo $var_dir`
 
                save_by_network;
            ;;
        esac
    done
}
 
copy_to;
Vus : 272
Publié par Ulrich Van Den Hekke : 73