Advanced File Backup with Versioning and Logging batch 01-02-2024, 02:40 AM
#1
@echo off
setlocal enabledelayedexpansion
set sourceFolder=C:\Source
set destinationFolder=C:\Backup
set logFile=backup_log.txt
set timestamp=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
echo Starting backup of files from %sourceFolder% to %destinationFolder%...
if not exist "%destinationFolder%" mkdir "%destinationFolder%"
set version=1
set backupFolder=%destinationFolder%\Backup_%timestamp%_v%version%
:check_version
if exist "%backupFolder%" (
set /a version+=1
set backupFolder=%destinationFolder%\Backup_%timestamp%_v%version%
goto check_version
)
mkdir "%backupFolder%"
for %%i in ("%sourceFolder%\*.*") do (
copy "%%i" "%backupFolder%"
)
echo Backup completed successfully. >> %logFile%
echo Date and Time: %date% %time% >> %logFile%
echo Backup Version: %version% >> %logFile%
endlocal
Explanation:
The script sets source and destination folders, a log file, and creates a timestamp for versioning.
It creates a backup folder with a timestamp and a version number.
If a backup folder with the same timestamp exists, it increments the version number until a unique folder name is found.
The script then copies all files from the source folder to the backup folder.
Success and details are logged to a log file.
This example demonstrates more advanced features such as versioning, dynamically creating folder names, and logging detailed information about the backup operation. Again, as tasks become more complex, consider using more advanced scripting languages like PowerShell or programming languages like Python for better maintainability and flexibility.
setlocal enabledelayedexpansion
set sourceFolder=C:\Source
set destinationFolder=C:\Backup
set logFile=backup_log.txt
set timestamp=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
echo Starting backup of files from %sourceFolder% to %destinationFolder%...
if not exist "%destinationFolder%" mkdir "%destinationFolder%"
set version=1
set backupFolder=%destinationFolder%\Backup_%timestamp%_v%version%
:check_version
if exist "%backupFolder%" (
set /a version+=1
set backupFolder=%destinationFolder%\Backup_%timestamp%_v%version%
goto check_version
)
mkdir "%backupFolder%"
for %%i in ("%sourceFolder%\*.*") do (
copy "%%i" "%backupFolder%"
)
echo Backup completed successfully. >> %logFile%
echo Date and Time: %date% %time% >> %logFile%
echo Backup Version: %version% >> %logFile%
endlocal
Explanation:
The script sets source and destination folders, a log file, and creates a timestamp for versioning.
It creates a backup folder with a timestamp and a version number.
If a backup folder with the same timestamp exists, it increments the version number until a unique folder name is found.
The script then copies all files from the source folder to the backup folder.
Success and details are logged to a log file.
This example demonstrates more advanced features such as versioning, dynamically creating folder names, and logging detailed information about the backup operation. Again, as tasks become more complex, consider using more advanced scripting languages like PowerShell or programming languages like Python for better maintainability and flexibility.