让 rsync 在复制之前对文件进行快照

Let rsync take a snapshot of files before copying

我有以下 bash 脚本。在脚本中,我使用 rsync 将文件从源复制到目标。在 rsync 的第一次调用中,我复制了所有文件,在第二次调用中,我仔细检查了文件,如果校验和有效,则复制的文件在源中被删除。

#!/bin/bash
set -e
rsync --info=progress2 -r --include='database/session_*.db' --exclude 'database/session*' /local/data/ /import/myNas/data
rsync --info=progress2 -r --include='database/session_*.db' --exclude 'database/session*' --checksum --remove-source-files /local/data/ /import/myNas/data

现在的问题是当 rsync 运行ning 新文件被写入 /local/data。我希望 rsync 在第一次 运行 时拍摄源 (/local/data) 中文件列表的快照,然后只复制这些文件。在第二个 运行 中,rsync 也应该只 运行 来自快照的这些文件(即计算校验和,然后删除文件)。这意味着不应触及新添加的文件。

这可能吗?

在 运行 rsync 之前使用此列表填充 null 分隔的文件同步列表:

#!/usr/bin/env bash

##### Settings #####

# Location of the source data files
declare -r SRC='/local/data/'

# Destination of the data files
declare -r DEST='/import/myNas/data/'

##### End of Settings #####

set -o errexit # same as set -e, exit if command fail

declare -- _temp_fileslist

trap 'rm -f "$_temp_fileslist"' EXIT

_temp_fileslist=$(mktemp) && typeset -r _temp_fileslist

# Populate files list as null delimited entries
find "$SRC" \
  -path '*/database/session_*.db' \
  -and -not -path '*/database/session*' \
  -fprinf "$_temp_fileslist" '%P[=10=]'

# --from0 tells rsync to read a null delimited list
# --files-from= tells to read the include list from this file
if rsync --info=progress2 --recursive \
  --from0 "--files-from=$_temp_fileslist" -- "$SRC" "$DEST";
then rsync --info=progress2 --recursive \
    --from0 "--files-from=$_temp_fileslist" \
    --checksum --remove-source-files -- "$SRC" "$DEST"
fi