0

很长一段时间以来,我一直在使用以下批处理文件来备份 Windows Server 机器上的某个目录。该批处理文件由 Windows 计划任务每​​天在特定时间运行,并在删除最旧的 .7z 文件 (7z = www.7-zip.org) 后将特定文件夹压缩到备份位置。

这样,我的备份文件夹中只有 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

谢谢。

4

1 回答 1

1

dir 命令中的 /b 开关不使用标题或摘要。result 是一个简单的档案列表,在这种情况下 orderer 按创建时间降序,最近的在前。

问题是skip=%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
)

因为跳过第一行,所以删除命令会删除除最近的所有档案之外的所有档案。

然后你执行备份,所以一个新文件被添加到目录中。结果,两个文件。

如果您只想要最后一个,您应该先执行备份并在删除之后,或者从 for 命令中删除skip=%BackupNr% 。

于 2016-06-01T11:08:46.300 回答