2016-06-01 36 views
0

我一直在使用下面的批處理文件來長時間備份Windows Server計算機上的某個目錄。該批處理文件由Windows Scheduled Task每天在特定時間運行,並在刪除最舊的.7z文件(7z = www.7-zip.org)後將特定文件夾壓縮到備份位置。批處理文件歸檔一個目錄並刪除較舊的7z歸檔文件

這種方式我只有在備份文件夾中的BackupNr中指定的.7z文件的數量,我不必手動刪除最舊的.7z文件。

問題是「Set BackupNr = 1」行。使用BackupNr = 1在批處理文件運行後,我的備份文件夾中始終有兩個.7z存檔。

我無法弄清楚需要更改什麼,以便每當批處理文件運行時只保留一個存檔。我怎樣才能解決這個問題?

@echo off 

:: The name of the backup file that will be created. It will use the current date as the file name. 
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set FileName=%%A%%B%%C%%D 
) 

:: The name of the folders where all the backup .zip and report files will be stored. 
Set ArchiveFolder=D:\Backup\Manual\Archives 
Set ReportFolder=D:\Backup\Manual\Reports 

:: The name of the folder that will be backed up, i.e. the AppData folder. 
Set FolderToBackup=C:\AppData 

:: The number of .zip files and .txt reports to keep. Older archives will be deleted according to their age. This ensures we only keep the most recent X number of backups. 
Set BackupNr=1 

:: Delete oldest backup archives so we only keep the number of archives specified in "skip=". 
echo.>> DeletedBackups.txt 
date /t >> DeletedBackups.txt 
echo.>> DeletedBackups.txt 
echo These older backups were deleted:>> DeletedBackups.txt 
for /F "skip=%BackupNr% delims=" %%a in ('dir /b /o-d %ArchiveFolder%\*') do (
    echo %ArchiveFolder%\%%a%1>> DeletedBackups.txt 
    del /f /q %ArchiveFolder%\%%a%1 
) 

echo.>> DeletedBackups.txt 
echo These older reports were deleted:>> DeletedBackups.txt 
for /F "skip=%BackupNr% delims=" %%a in ('dir /b /o-d %ReportFolder%\*') do (
    echo %ReportFolder%\%%a%1>> DeletedBackups.txt 
    del /f /q %ReportFolder%\%%a%1 
) 

echo Starting the backup: %DATE% %TIME% >> %ReportFolder%\%FileName%.txt 

:: Adds all files to 7z archive using BCJ2 converter, LZMA with 8 MB dictionary for main output stream (s0), and LZMA with 512 KB dictionary for s1 and s2 output streams of BCJ2. 
C:\PROGRA~1\7-Zip\7z.exe a -t7z %ArchiveFolder%\%FileName%.7z %FolderToBackup%\* -m0=BCJ2 -m1=LZMA:d23 -m2=LZMA:d19 -m3=LZMA:d19 -mb0:1 -mb0s1:2 -mb0s2:3 >> %ReportFolder%\%FileName%.txt 

echo Backup finished at: %DATE% %TIME% >> %ReportFolder%\%FileName%.txt 

:: Write the backup Start and End times to the BackupInfo.csv file. 
gawk -f BackupRecord.awk %ReportFolder%\%FileName%.txt >> %ReportFolder%\BackupReport.csv 

:: Write the file size of the backup .zip file that was just created to the log file. 
set size=0 
call :filesize %ArchiveFolder%\%FileName%.7z 
echo Backup archive file size: %size% bytes >> %ReportFolder%\%FileName%.txt 

exit 

:: set filesize of 1st argument in %size% variable, and return 
:filesize 
    set size=%~z1 
    exit /b 0 

謝謝。

回答

1

dir命令中的/ b開關不使用標題或摘要。結果是檔案的簡單列表,在這種情況下,訂購者按創建時間遞減,最近的第一個。

的問題是跳過=%BackupNr%

for /F "skip=%BackupNr% delims=" %%a in ('dir /b /o-d %ReportFolder%\*') do (
    echo %ReportFolder%\%%a%1>> DeletedBackups.txt 
    del /f /q %ReportFolder%\%%a%1 
) 

爲跳過第一行,因此delete命令將刪除所有檔案,除了最近的。

然後您執行備份,以便將新文件添加到目錄。結果,兩個文件。

如果您只想要最後一個,則應首先執行備份並刪除之後,或從for命令中刪除skip =%BackupNr%