Script pour sauvegardes sur Disque USB

De WindowsLinux.net - Astuces pour Windows et Linux !.

Sous Linux

Exemple basé pour un Disque USB.

#!/bin/sh
 
###############Définition des variables####################
 
##Fichier dans lequel est stocké temporairement les log
LOGFILE="/var/log/backup.log" 
 
DATE=`date +%d/%m/%Y`
HEURE=`date +%H:%M`
 
## Récupère le nom du jour
JOUR=`date +%A`
 
## Interface du disque USB (dmesg pour connaitre son nom)
DDUR=/dev/sdb1
 
## Point de montage ( à créer)
MP=/mnt/usb
 
## Le dossier que vous voulez sauvegarder
SOURCE=/home/user/
 
DESTINATION=/mnt/usb/sauvegarde/$JOUR
 
## le file system de votre disque USB
FS=vfat
 
## Adresse e-mail ou vous voulez recevoir les rapports
EMAIL=mail@domain.com
 
###############################################################################################
 
sauvegarde() 
{
	rm -rf $DESTINATION/*
 	FREEDDUR=`df -h | grep $DDUR | tr -s ' ' |tail -n 1 | cut -d' ' -f4`
	echo "Taillle disponible sur $DDUR: $FREEDDUR avant la sauvegarde"  >> $LOGFILE
 
	cp -R $SOURCE $DESTINATION >> $LOGFILE
	DATE=`date +%d/%m/%Y`
	HEURE=`date +%H:%M`
	echo "Sauvegarde effectuee correctement" >> $LOGFILE
	echo "Fin de la sauvegarde le $DATE a $HEURE" >> $LOGFILE
	FREEDDUR=`df -h | grep $DDUR | tr -s ' ' |tail -n 1 | cut -d' ' -f4`
	echo "Taillle disponible sur $DDUR: $FREEDDUR apres la sauvegarde"  >> $LOGFILE
 
}
 
 
# Debut !
rm $LOGFILE
echo "Debut de la sauvegarde le $DATE a $HEURE" >> $LOGFILE
 
TAILLESOURCE=`du -hs $SOURCE`
echo "Sauvegarde de $SOURCE, Taille: $TAILLESOURCE" >> $LOGFILE
 
 
mount $DDUR $MP -t $FS >> LOGFILE
if [ $? -ne 0 ]
then
        echo "Erreur lors du montage du disque !!!" >> $LOGFILE
         echo "Sauvegarde NON effectuee" >> $LOGFILE
elif [ -d  $DESTINATION ]
	then
		sauvegarde
	else
		echo "Pas de dossier du: $JOUR on va le creer" >> $LOGFILE
		mkdir $DESTINATION
		sauvegarde
fi
 
umount $DDUR >>$LOGFILE
 
cat $LOGFILE | mail -s backup $EMAIL

Sous Windows

Pour les versions précédentes à Windows 7, une solution peut être l'utilisation de ntbackup. D'autres utilitaires sont disponibles sur la page USB de ce site (comme USBFlashCopy).


Un autre script (vbscript) est cependant disponible ici :

'---------------------------------------------------------
'Written by Vic Laurie, January, 2006
'All rights reserved. Provided as is
'with no guarantees, express or implied
'User assumes all responsibility
'---------------------------------------------------------
 
'Description- Copies any new or updated files from user selected folder
'and its subfolders to user selected backup folder
'Uses Xcopy with switches /e /q /h /k /i /y /r /d /c
 
Option Explicit
Dim sFldrInput1, sFldrInput2, introMsg
 
introMsg = msgBox("This program backs up new and changed files."& vbCrLf & "If you are copying many files it may take a few minutes."& vbCrLf & "A message will appear when copying is finished.",vbOKCancel)
If introMsg = vbCancel Then Wscript.Quit
 
ChooseFolder sFldrInput1,"Select the source folder: "
ChkForSystemFldr sFldrInput1
ChooseFolder sFldrInput2, "Select the backup folder: "
ChkForSystemFldr sFldrInput2
CopyFolder sFldrInput1, sFldrInput2
Wscript.Quit
 
 
sub ChooseFolder(sFldrChoice, sSelectionString)
dim objShell, objFolder, objFolderItem, strPath, msgValue
Const DESK_TOP = &H10&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
sFldrChoice = ""
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(DESK_TOP)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, sSelectionString, OPTIONS, strPath) 
 
If objFolder Is Nothing Then
    Wscript.Quit
End If
 
Set objFolderItem = objFolder.Self
sFldrChoice = objFolderItem.Path
 
 
End sub
 
sub CopyFolder (sSourceFldr,sBkupFldr)
 
Const CopyX = "xcopy "
Const sSwitches = " /e /q /h /k /i /y /r /d /c"
Const sWildCard = "\*.*"
dim sStatement
dim objWshell
Dim oIE, oIEDoc, sMsg
sStatement= copyX  & chr(34) & sSourceFldr& sWildcard & chr(34) & " " & chr(34) & sBkupFldr & chr(34) & sSwitches
 
'The next part is just to display a message while copying
set objWshell=Wscript.CreateObject("Wscript.Shell")
Set oIE = Wscript.CreateObject("InternetExplorer.Application")
oIE.Navigate "about:blank"
do while oIE.busy : wscript.sleep 10 : loop
Set oIEDoc = oIE.Document
oIE.AddressBar = False
oIE.StatusBar = False
oIE.ToolBar = False
oIE.height=200
oIE.width=300
oIE.Resizable = False
oIE.Visible = True
sMsg= "<p><center>Files are being copied.<br>Please wait.<br>Large folders may take several minutes.</center></p>"
oIEDoc.Body.Innerhtml= sMsg
 
'copy the files
objWshell.Run sStatement,7,true
 
Set oIEDoc = Nothing
oIE.Quit
Set oIE = Nothing
set objWshell = Nothing
msgBox "Copying job done"
End sub
 
'This is necessary to remove trailing slash on drive letters
Sub chkForDrv(sFldrChoice)
Dim oRe, bMatch
set oRe = New RegExp
oRe.pattern = "[a-zA-Z]:\\$"
bMatch= oRe.Test(sFldrChoice)
If bMatch Then sFldrChoice= Left(sFldrChoice, 2)
End sub
 
Sub chkForSystemFldr(sFldrChoice)
dim msgWarn, msgValue
If Left(sFldrChoice, 1) = ":" then
msgWarn = msgBox("You have selected a special system folder. Please select a different folder", vbRetryCancel+vbCritical)
Select case msgWarn
Case vbcancel
  Wscript.Quit
Case VbRetry 
 ChooseFolder sFldrChoice,"Select a folder: "
 If Left(SfldrChoice, 1) = ":" then Wscript.Quit
 msgValue = msgBox("You selected "& sFldrChoice, vbOKCancel)
   If msgValue = vbCancel Then Wscript.Quit
   If Len(sFldrChoice) = 3 then chkForDrv sFldrChoice
End select
Else 
  msgValue = msgBox("You selected "& sFldrChoice, vbOKCancel)
  If msgValue = vbCancel Then
	Wscript.Quit
End If
If Len(sFldrChoice) = 3 then 
   chkForDrv sFldrChoice
End if
 
 
end if
End sub
Outils personnels