2016-06-01 2 views
0

다음 배치 파일을 사용하여 Windows 서버 시스템의 특정 디렉토리를 오랫동안 백업했습니다. 배치 파일은 특정 시간에 매일 Windows 예약 작업에 의해 실행되며 가장 오래된 .7z 파일 (7z = www.7-zip.org)을 삭제 한 후 특정 폴더를 백업 위치로 압축합니다.디렉토리를 보관하고 오래된 7z 아카이브를 삭제하는 배치 파일

이렇게하면 백업 폴더에 BackupNr에 지정된 .7z 파일 수만 있으므로 가장 오래된 .7z 파일을 수동으로 삭제하지 않아도됩니다.

문제는 "Set BackupNr = 1"행입니다. BackupNr = 1 사용하기 배치 파일이 실행 된 후에는 항상 내 백업 폴더에 .7z 아카이브가 2 개 있습니다.

배치 파일이 실행될 때마다 하나의 아카이브 만 유지되도록 변경해야 할 항목을 파악할 수 없습니다. 이 문제를 어떻게 해결할 수 있습니까?

@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 스위치는 헤더도 요약도 사용하지 않습니다. result는 아카이브의 일반 목록입니다.이 경우에는 작성자가 가장 최근에 생성 한 순으로 내림차순으로 정렬됩니다.

문제는 첫 번째 줄을 건너로

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 
) 

에서 = %의 BackupNr %건너 뛰기, 그래서 삭제 명령은 가장 최근의 경우를 제외하고 모든 아카이브가 삭제됩니다.

백업을 실행하면 새 파일이 디렉토리에 추가됩니다. 결과, 두 파일.

마지막 백업 만 수행하려는 경우 먼저 백업을 수행 한 다음 삭제 후에 수행해야합니다. 또는 skip = % BackupNr %을 for 명령에서 제거해야합니다.