【问题标题】:Batch file to archive a directory and delete older 7z archive用于归档目录并删除旧 7z 归档的批处理文件
【发布时间】:2016-06-01 10:23:35
【问题描述】:

长期以来,我一直在使用以下批处理文件来备份 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

谢谢。

【问题讨论】:

    标签: windows batch-file 7zip backups


    【解决方案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%

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多