仅当文件“新鲜”时,Rclone 才会复制

仅当文件“新鲜”时,Rclone 才会复制

我每天运行一个简单的脚本,将 RAMDisk 复制到 Google Drive,并为其指定 YYYYMMDD_name 命名格式。这很有效,但有点浪费,因为我实际上不会每天都更改磁盘上的内容。

我想自动检查源 RAMDisk,并且仅在以下情况下执行备份(整个磁盘)任何文件,任何地方在磁盘上,比根目录修改得更近(RAMDisk 每天在启动时从本地驱动器重新创建,因此根目录总是具有系统上次启动时的修改日期/时间。在该日期/时间之后修改的任何内容显然都是较新的,因此应该备份所有内容)。

我对 Windows 脚本不是很熟练,所以我无法弄清楚如何实现这一点。我特别不想使用同步,因为我想要每日备份,这样我就可以随时回滚到任何一天(有变化的一天),如果我需要的话。

我非常感谢任何指点或建议。

答案1

在 chatGPT 的帮助下,我设法找到了解决方案。这是我让它工作后的最终代码。

@echo off
setlocal enabledelayedexpansion
set datepath=20%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
set y=%DATE:~10,4%
set m=%DATE:~4,2%

REM Define source folder, destination folder, and list of extensions to ignore
set "sourceFolder=V:\test"
set "ignoreExtensions=.log;.data;.scid;.dly"  REM Add extensions to ignore separated by semicolon

REM Get the modification timestamp of the source folder (to the minute)
for %%F in ("%sourceFolder%") do (
    set "sourceTimestamp=%%~tF"
    set "sourceTimestamp=!sourceTimestamp:~0,16!"
)

REM Initialize a flag to determine if a newer file is found
set "newerFileFound="

REM Check files in the root directory of the source folder
for %%A in ("%sourceFolder%\*.*") do (
    REM Get the modification timestamp of the current file (to the minute)
    for %%B in ("%%A") do (
        set "fileTimestamp=%%~tB"
        set "fileTimestamp=!fileTimestamp:~0,16!"
    )

    REM Compare the file timestamp with the source folder timestamp
    if "!fileTimestamp!" neq "!sourceTimestamp!" (
        set "newerFileFound=1"
        goto :copyFolder
    )
)

REM Check files in subdirectories of the source folder
for /r "%sourceFolder%" %%A in (*) do (
    REM Get the modification timestamp of the current file (to the minute)
    for %%B in ("%%A") do (
        set "fileTimestamp=%%~tB"
        set "fileTimestamp=!fileTimestamp:~0,16!"
    )

    REM Check if the file extension is in the list of extensions to ignore
    set "fileExtension=%%~xA"
    set "ignore=0"
    for %%E in (%ignoreExtensions%) do (
        if /i "!fileExtension!" == "%%~E" (
            set "ignore=1"
        )
    )

    REM Compare the file timestamp with the source folder timestamp
    if "!ignore!" neq "1" (
        if "!fileTimestamp!" gtr "!sourceTimestamp!" (
            set "newerFileFound=1"
            goto :copyFolder
        )
    )
)

:copyFolder
REM Copy the entire source folder to the destination if any newer file is found
if defined newerFileFound (

rclone copy V:\test GDrive:20%y%/%m%/%datepath%_backup -P --copy-links 

) else (
    echo No newer files found in the source folder.
)

endlocal

相关内容