DeuAq.com >> Leben >  >> Elektronik

Effizientes Windows-Batch-Skript für zuverlässige Datensicherung

Als IT-Experte mit jahrelanger Praxis in der Systemadministration weiß ich: Früher steuerten Nutzer PCs ausschließlich über die Befehlszeile. Heute, trotz moderner grafischer Oberflächen, bleibt sie unverzichtbar – besonders für automatisierte Datensicherungen. Batch-Skripte sind integriert, flexibel und erfordern keine zusätzliche Software.

Effizientes Windows-Batch-Skript für zuverlässige Datensicherung

Grafische Tools sind benutzerfreundlich, doch Befehlszeilen-Skripte überzeugen durch Automatisierung ohne Interaktion. Sie eignen sich ideal für Backups und sind ein fester Bestandteil von Windows.

Effizientes Windows-Batch-Skript für zuverlässige Datensicherung

Warum Batch-Skripte für Backups wählen?

Trotz vieler Backup-Programme bieten Batch-Skripte einzigartige Vorteile, die ich in meiner täglichen Arbeit nutze:

  • Natives Arbeiten: Nutzen Sie Windows-eigene Befehle – das System kennt seine Daten am besten.
  • Volle Kontrolle: Jeder Schritt ist transparent und anpassbar.
  • Hohe Geschwindigkeit: Native Befehle minimieren Overhead.
  • Maximale Leistungsfähigkeit: Kaum eine Backup-Aufgabe ist unmöglich – ich habe es mit komplexen Szenarien getestet.
  • Kostenlos und skalierbar: Keine Lizenzkosten, einfach auf viele Systeme kopierbar.

Überblick über das Backup-Batch-Skript

Batch-Scripting wirkt kompliziert, ist aber einfach zu erlernen. Dieses Skript sichert Ihre Daten vollständig oder inkrementell – konfigurierbar ohne Programmierkenntnisse. Es dient mir als Einstieg in Windows-Scripting.

Funktionen des Skripts:

  1. Erstellt vollständige oder tägliche inkrementelle Backups von Dateien/Ordnern aus einer Konfigurationsdatei.
    • Ordner: Inklusive Unterordner.
    • Dateien: Nur spezifizierte.
  2. Komprimiert mit 7-Zip (separat installieren).
  3. Datiert und verschiebt ins Zielverzeichnis (z. B. externes Laufwerk).
  4. Löscht temporäre Dateien automatisch.

Anforderungen: Windows 2000/XP/2003/Vista oder neuer, 7-Zip (kostenlos).

Konfigurationsdatei: "BackupConfig.txt" im Skript-Ordner. Ein Eintrag pro Zeile, # für Kommentare. Beispiel:

# Dateien und Ordner, einer pro Zeile.
C:\Documents and Settings\Jason Faulkner\Desktop
C:\Documents and Settings\Jason Faulkner\My Documents\Important Files
C:\Scripts\BackupScript.bat

Beispiel sichert Desktop, Unterordner und spezifische Datei.

Sicherungsarten:

  • Vollständig: Alle Dateien/Ordner inkl. Unterordner.
  • Inkrementell: Ordner: Nur heute geänderte Dateien. Dateien: Immer.

Das vollständige Datensicherungs-Skript

Dieses einfache Skript kopiert Dateien und komprimiert sie. Konfigurierbar:

  • Speicherort für Backups.
  • Wochentag für Voll-Backup.
  • 7-Zip-Pfad (Standard: Program Files).

Feedback willkommen – ich plane Updates basierend darauf. Anleitungen zu Batch-Dateien und geplanten Tasks siehe Links unten.

Hier das Skript:

Hinweis: Kopieren Sie aus der reinen Textquelle unten für exakte Anführungszeichen.

@ECHO OFF

REM BackupScript
REM Version 1.01, Updated: 2008-05-21
REM By Jason Faulkner (articles[-at-]132solutions.com)

REM Performs full or incremental backups of folders and files configured by the user.

REM Usage---
REM > BackupScript

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

REM ---Configuration Options---

REM Folder location where you want to store the resulting backup archive.
REM This folder must exist. Do not put a '' on the end, this will be added automatically.
REM You can enter a local path, an external drive letter (ex. F:) or a network location (ex. \serverbackups)
SET BackupStorage=C:\Backup

REM Which day of the week do you want to perform a full backup on?
REM Enter one of the following: Sun, Mon, Tue, Wed, Thu, Fri, Sat, *
REM Any day of the week other than the one specified below will run an incremental backup.
REM If you enter '*', a full backup will be run every time.
SET FullBackupDay=*

REM Location where 7-Zip is installed on your computer.
REM The default is in a folder, '7-Zip' in your Program Files directory.
SET InstallLocationOf7Zip=%ProgramFiles%\7-Zip

REM +-----------------------------------------------------------------------+
REM | Do not change anything below here unless you know what you are doing. |
REM +-----------------------------------------------------------------------+

REM Usage variables.
SET exe7Zip=%InstallLocationOf7Zip%\7z.exe
SET dirTempBackup=%TEMP%\backup
SET filBackupConfig=BackupConfig.txt

REM Validation.
IF NOT EXIST %filBackupConfig% (
 ECHO No configuration file found, missing: %filBackupConfig%
 GOTO End
)
IF NOT EXIST "%exe7Zip%" (
 ECHO 7-Zip is not installed in the location: %dir7Zip%
 ECHO Please update the directory where 7-Zip is installed.
 GOTO End
)

REM Backup variables.
FOR /f "tokens=1,2,3,4 delims=/ " %%a IN ('date /t') DO (
 SET DayOfWeek=%%a
 SET NowDate=%%d-%%b-%%c
 SET FileDate=%%b-%%c-%%d
)

IF {%FullBackupDay%}=={*} SET FullBackupDay=%DayOfWeek%
IF /i {%FullBackupDay%}=={%DayOfWeek%} (
 SET txtBackup=Full
 SET swXCopy=/e
) ELSE (
 SET txtBackup=Incremental
 SET swXCopy=/s /d:%FileDate%
)

ECHO Starting to copy files.
IF NOT EXIST "%dirTempBackup%" MKDIR "%dirTempBackup%"
FOR /f "skip=1 tokens=*" %%A IN (%filBackupConfig%) DO (
 SET Current=%%~A
 IF NOT EXIST "!Current!" (
 ECHO ERROR! Not found: !Current!
 ) ELSE (
 ECHO Copying: !Current!
 SET Destination=%dirTempBackup%!Current:~0,1!%%~pnxA
 REM Determine if the entry is a file or directory.
 IF "%%~xA"=="" (
 REM Directory.
 XCOPY "!Current!" "!Destination!" /v /c /i /g /h /q /r /y %swXCopy%
 ) ELSE (
 REM File.
 COPY /v /y "!Current!" "!Destination!"
 )
 )
)
ECHO Done copying files.
ECHO.

SET BackupFileDestination=%BackupStorage%\Backup_%FileDate%_%txtBackup%.zip

REM If the backup file exists, remove it in favor of the new file.
IF EXIST "%BackupFileDestination%" DEL /f /q "%BackupFileDestination%"

ECHO Compressing backed up files. (New window)
REM Compress files using 7-Zip in a lower priority process.
START "Compressing Backup. DO NOT CLOSE" /belownormal /wait "%exe7Zip%" a -tzip -r -mx5 "%BackupFileDestination%" "%dirTempBackup%"
ECHO Done compressing backed up files.
ECHO.

ECHO Cleaning up.
IF EXIST "%dirTempBackup%" RMDIR /s /q "%dirTempBackup%"
ECHO.

:End
ECHO Finished.
ECHO.

ENDLOCAL

Nur-Text-Quelle ist hier verfügbar: Backup

Tipps zur Nutzung:

  • So erstellen Sie eine Batch-Datei
  • So erstellen Sie einen geplanten Windows-Task

Dieses Skript sichert meinen Rechner täglich – bewährt und zuverlässig. Probieren Sie es aus!

Viel Erfolg!