How to keep only the five newest backup folders an

2019-09-22 06:32发布

问题:

I want to use a batch file to make a backup regularly, every day. Consider these folders:

C:\game folder
D:\backup1
D:\backup2
D:\backup3
D:\backup4
D:\backup5

There are inside game folder:

c:\game folder\remote\                       »Folder
c:\game folder\remote\remotecache.vdf        ».vdf file
c:\game folder\remote\FullProfile            »No extension file
c:\game folder\remote\ProfileSaves\          »Folder
c:\game folder\remote\ProfileSaves\1054678   »No extension file

I need a batch file that

  • verifies if time stamp on backup1 (either folder or any file inside it) is older than 1 day and if YES then continue, else stop;
  • deletes backup5 folder or its contents; copies folders/files of backup4 to backup5 folder;
  • deletes backup4 folder or its contents; copies folders/files of backup3 to backup4 folder;
  • etc. up to backup1 folder.
  • Finally copies game folder to backup1 folder.

Is it possible?

My idea is to run a scheduled task (Windows), and the .bat file verifies if backup is needed or not by using time stamp of backup1 or any file inside it, comparing with actual date and checking if backup1 is older than one day.


Considering the code below, what should I place in order to make it works as I've described before?

@echo off

echo  
echo ------------------------------------------------------------------
echo Daily script that backs-up game folder
echo ------------------------------------------------------------------

echo ------------------------------------------------------------------
echo Calculation of date
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

How to make some kind of IF here to check time stamp of backup1 folder and if it's older than 1 day run the code below? And how to set the @path?

echo ------------------------------------------------------------------
echo Delete old backup folder
FORFILES -p "" /D -1 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

echo ------------------------------------------------------------------
echo Make new Backup folder
md D:\Backup1 

echo ------------------------------------------------------------------
echo Copy files into backup folder
xcopy /s /y "C:\game folder" "D:\Backup1"

回答1:

What about this simple batch file for this daily backup operation task?

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "FolderToBackup=C:\game folder"
set "BackupParentFolder=D:"
set "BackupNamePrefix=Backup_"

if not exist "%FolderToBackup%\" goto EndBackup

rem Get region independent current date in format YYYY-MM-DD.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "FolderNameDate=%%I"
set "TodayBackupFolder=%BackupParentFolder%\%BackupNamePrefix%%FolderNameDate:~0,4%-%FolderNameDate:~4,2%-%FolderNameDate:~6,2%"

rem Was a backup folder created already today?
if exist "%TodayBackupFolder%\" goto EndBackup

rem Create a backup of the folder to backup by doing a simple folder copy.
%SystemRoot%\System32\xcopy.exe "%FolderToBackup%" "%TodayBackupFolder%\" /C /H /I /K /Q /R /S /Y >nul

rem Keep only the newest 5 backup folders and delete all others.
for /F "skip=5 delims=" %%I in ('dir "%BackupParentFolder%\%BackupNamePrefix%????-??-??" /AD /B /O-N 2^>nul') do rd /Q /S "%BackupParentFolder%\%%I"

:EndBackup
endlocal

It uses command WMIC to get current date in format YYYY-MM-DD as described in detail in answer on %date% produces different result in batch file when run from Scheduled Tasks in Server 2016. The international date format YYYY-MM-DD is good readable. It has additionally the big advantage that on using the date in this format in a folder name, the list of folders sorted alphabetically by name are automatically also sorted by date on which the folder was created. It is surely good to know when a backup folder was created and so it is better to use 2017-12-22, 2017-12-23, 2017-12-24, ... instead of just 1, 2, 3, ...

The batch file next checks if today a backup folder was created already which is simple as the backup date is included in folder name in international date format. The batch file does nothing else if on same day a backup was created already before.

Otherwise a backup of the folder to backup is made in configured parent directory with configured prefix. BackupParentFolder must be specified in the batch file, but BackupNamePrefix can be not defined at all.

After making a backup the command DIR is executed for listing all backup folders with backup date in folder name sorted reverse by name which means the newest backup folders are output by DIR first and the oldest last.

The five newest backups are ignored and all other backup folders are deleted which means usually removing one directory from existing backup folders list. This very simple, but very effective backup strategy is explained in detail in answer on Bat file to delete files only when younger files are present.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?
  • xcopy /?

Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.