#!/bin/sh############################################################ This script uses rsync to backup directories on a media # (ex: USB disk) with a copy and incremental method. ############################################################ 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA############################################################## Written by Spip <unpeudescience at gmail dot com># Version 1.0##############################################################to be nice...ionice -c3 -p$$renice +15 -p $$THEDATE=`date +%F_%Hh%M`FILE='/media/disk/sauvegarde'LOGFILE='/var/log/sauvegarde'SPLIT='====================================================================='#check logfile directoryif ! [ -e $LOGFILE ]then /bin/mkdir -p $LOGFILEfi#check if the media is mountedif ! [ -e $FILE ]then FILE='/media/disk-1/sauvegarde' if ! [ -e $FILE ] then echo "$THEDATE : péripherique non connecté. Pas de sauvegarde possible" >> "$LOGFILE/save.log" echo $SPLIT >> "$LOGFILE/save.log" exit 0 fifi#prevent a second taskLOCKFILE="/var/lock/sauvegarde.lock"[ -f $LOCKFILE ] && exit 0#if the script are stopped, remove lockfiletrap "rm -f $LOCKFILE" EXITtouch $LOCKFILEecho "$THEDATE : Début de sauvegarde" >> "$LOGFILE/save.log"# $1:time to keep files; $2: name of the backup; $3: target; $4: exclusionfunction save() { echo "$SPLIT" >> "$LOGFILE/$2.log" echo "$THEDATE" >> "$LOGFILE/$2.log" #Incremental & copie INC="$FILE/$2/INC/$THEDATE" BAK="$FILE/$2/BAK" TOSAVE="$3" /bin/mkdir -p "$INC" if ! [ -e "$BAK" ] then /bin/mkdir -p "$BAK" fi#Mr Proper with tmpwatch: 3d indicates 3 days, time before cleaning if [ `ls $FILE/$2/INC | wc -l` -gt 6 ] then /usr/sbin/tmpwatch -a $1 "$FILE/$2/INC" >> "$LOGFILE/save.log" fi#a: archivage :recurcif, preserve dates, persmissions, groupes...#v: verbose mode#delete : supprime les fichiers n'etant plus chez l'émeteur >> copie conforme. /usr/bin/rsync -av --stats --delete-excluded --backup --backup-dir="$INC" "$TOSAVE" "$BAK" --exclude=$4 >> "$LOGFILE/$2.log"#bz2 compression & remove files cd $FILE/$2/INC tar -cjvf "$INC.tar.bz2" "$THEDATE" rm -rf "$THEDATE"}#List here all repositories to backupsave 5d thunderbird_gnu /home/gnu/.thunderbird/save 4d firefox_gnu /home/gnu/.mozilla/ /home/gnu/.mozilla/firefox/soue3i2s.default/Cache/save 20d source_code /home/gnu/Bureau/source_code/save 20d rpm /home/package/rpmsave 120d irssi /home/gnu/.irssisave 120d screen /home/gnu/.screenrcsave 120d conky /home/gnu/.conkyrc#save 120d granu /home/gnu/Bureau/granusave 120d granu_project /home/gnu/Bureau/granu/projet#to log the end of the backupTHEDATE=`date +%F_%Hh%M`echo "$THEDATE : Sauvegarde effectuée" >> "$LOGFILE/save.log"exit 0