Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

windows - How to keep only the five newest backup folders and delete the older?

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

C:game folder
D:ackup1
D:ackup2
D:ackup3
D:ackup4
D:ackup5

There are inside game folder:

c:game folder
emote                       ?Folder
c:game folder
emote
emotecache.vdf        ?.vdf file
c:game folder
emoteFullProfile            ?No extension file
c:game folder
emoteProfileSaves          ?Folder
c:game folder
emoteProfileSaves1054678   ?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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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%System32wbemwmic.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%System32xcopy.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 the current date in format yyyy-MM-dd as described in detail in answer on Why does %date% produce a different result in batch file executed as scheduled task? 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, etc. instead of just 1, 2, 3, etc.

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 could 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...