@echo off
Rem Backup.cmd: Backup mit robocopy

Rem Typ des Backup. Default ist inkrementell, außer am Tag des "FullBackup".

Rem set DayOfFullBackup=Sun
set DayOfFullBackup=Son
set BackupType=I

Rem Ermittle Datum
for /f "tokens=2" %%i in ('robocopy /?^|find "Gestartet"') do ( set DayOfWeek=%%i )
set DayOfWeek=%DayOfWeek:~0,3%
set dat=%date:~-4%-%date:~-7,2%-%date:~-10,2% 

Rem Sicherungstyp
IF /I %DayOfWeek%==%DayOfFullBackup% (set BT=F) ELSE (set BT=%BackupType%)

Rem Log Datei
IF /I NOT EXIST "C:\Program Files\BAT\log" md "C:\Program Files\BAT\log"
set logfile="C:\Program Files\BAT\log\Robo_Backup_%dat%.log" 

Rem Job Datei kreieren
Rem robocopy Parameter:
Rem 	/nosd	no source directory (in Kommandozeile angegeben, s.u.)
Rem 	/nodd	no destination directory (dito)
Rem 	/e		Copies subdirectories. Note that this option includes empty directories
Rem 	/r:3	3 retries on failed copies (kleine Zahl, wichtig wenn z. B. Outlook noch seine Dateien gesperrt hat)
Rem 	/w:5	5 seconds wait time between retries (dito)
Rem 	/np		do not display progress (no. of files or dirs copied so far)
Rem		/eta	Show the estimated time of arrival (ETA) of the copied files.
Rem		/sl		Copies the symbolic link instead of the target
Rem		/xjd	Excludes junction points for directories
Rem		/xjf	Excludes junction points for files
Rem 	/m		Copy only files for which the Archive attribute is set, and reset the Archive attribute
Rem 	/a		Copy only files for which the Archive attribute is set
Rem 	/log+	Write the status output to the log file (appends the output to the existing log file)
Rem 	/save	Parameters are to be saved to the named job file
Rem 	/quit	Quit after processing command line

IF /I %BT%==C robocopy /NOSD /NODD  /E /R:3 /W:5 /NP /ETA /XJD /XJF      /LOG+:%logfile% /SAVE:Robo_Backup /QUIT & GOTO doit
IF /I %BT%==F robocopy /NOSD /NODD  /E /R:3 /W:5 /NP /ETA /XJD /XJF      /LOG+:%logfile% /SAVE:Robo_Backup /QUIT & GOTO doit
IF /I %BT%==I robocopy /NOSD /NODD  /E /R:3 /W:2 /NP /ETA /XJD /XJF /M   /LOG+:%logfile% /SAVE:Robo_Backup /QUIT & GOTO doit
IF /I %BT%==D robocopy /NOSD /NODD  /E /R:3 /W:5 /NP /ETA /XJD /XJF /A   /LOG+:%logfile% /SAVE:Robo_Backup /QUIT & GOTO doit
IF /I %BT%==M robocopy /NOSD /NODD  /E /R:3 /W:5 /NP /ETA /XJD /XJF /MIR /LOG+:%logfile% /SAVE:Robo_Backup /QUIT & GOTO doit
;
:doit

Rem Startmeldung ins log
echo .                                                                     >>%logfile%
echo --------------------------------------------------------------------- >>%logfile%
echo Starte Backup: %date% %time%  (%DayOfWeek%)                           >>%logfile%
echo --------------------------------------------------------------------- >>%logfile% 

Rem Sicherungsverzeichnis
IF /I %BT%==C set bd=Copy
IF /I %BT%==F set bd=%dat% (%BT%)
IF /I %BT%==I set bd=%dat% (%BT%)
IF /I %BT%==D set bd=%dat% (%BT%)
IF /I %BT%==M set bd=Mirror 

Rem Verzeichnisse festlegen (source/destination)
Rem Quellen nur unter C:\. D:\ wird extra behandelt und ganz gesichert.
REM set sources=("C:\Users" "C:\Program Files\BAT" "C:\Program Files (x86)\totalcmd" "C:\cygwin64\home" "C:\ProgramData")
set sources=("D:\test")
Rem Externe Festplatte als "X" voreingestellt. Klappt nur, wenn zwischendurch nicht andere temporäre Laufwerke alle
Rem Laufwerksbuchstaben verbraucht haben. Zwischen D: und X: ist aber noch viel Platz, sollte reichen.
set destin="X:\RoboBackup\%bd%"

Rem Festlegen von Unterverzeichnissen (exdir) und Dateien (exfil), die nicht mitgesichert werden sollen: 
set exdir=*temp* *tmp* *verlauf* *cookies* *recent* *cache* *old-C *RECYCLE.BIN *Nepomuk2*
set exfil=*ntuser* *UsrClass* *temp* *tmp* *mp3

Rem Los geht's.

setlocal ENABLEDELAYEDEXPANSION
Rem Das wird benötigt, damit die Zugriffe auf Teilstrings funktionieren, siehe
Rem 	http://www.administrator.de/forum/batch-anf%C3%A4nger-fragen-zu-setlocal-enabledelayedexpansion-set-for-166740.html

for %%s in %sources% do (
	Rem Ziel ist %destin%\{%%s ohne den Laufwerksbuchstaben, d. h. ab Zeichen 4}
	Rem Substringauswahl geht nicht mit Schleifenvariablen, sondern nur mit anderen Variablen %...%.
	Rem Daher braucht man noch eine lokale Hilfsvariable mit dem richtigen Namen.
	set tmp=%%s
	robocopy  %%s  %destin:~0,-1%\!tmp:~1,1!\!tmp:~4!  /XF %exfil% /XD %exdir%  /JOB:Robo_Backup
	IF /I %BT%==F (attrib -a %%s\*.* /s /d)
)

Rem Sonderbehandlung für D:\ wegen des "\" am Ende. 
Rem Fummelkram wegen der Anführungszeichen kann vermutlich optimiert werden
robocopy  D:\  %destin:~0,-1%\D%destin:~-1%  /XF %exfil% /XD %exdir%  /JOB:Robo_Backup
IF /I %BT%==F (attrib -a D:\*.* /s /d)

Rem Bei Kopieren von ganzen Laufwerken (hier D:\) setzt robocopy u. U. das Zielverzeichnis auf "versteckt". 
Rem Wird so rückgängig gemacht:
attrib -s -h %destin:~0,-1%\D%destin:~-1%
Rem Quelle: http://www.mediamill.de/blog/2010/02/05/entfernen-des-versteckt-hidden-attributs-von-einem-ordner-nach-einer-robocopy-aktion/
Rem Man könnte noch die Option /S hinzufügen, um alles rekursiv zu "entstecken".


echo --------------------------------------------------------------------- >>%logfile%
echo Backup Fertig: %date% %time%                                          >>%logfile%
echo --------------------------------------------------------------------- >>%logfile%
echo .

echo .
echo Backup fertig.

pause

