med*_*duz 12 sync rsync unison
我正在本地和远程文件夹之间进行同步。碰巧我通过一个愚蠢的命令丢失了一个文件。
在进行实际文件传输之前,我习惯于一致和您确认(“去?”)的方式。
rsync 有这样的选项吗?(服务器端没有一致)
谢谢!
您可以将 -n 选项与 rsync 一起使用来执行试运行。rsync 会告诉你它会在不实际执行的情况下执行哪些操作。如果您对结果感到满意,请在没有 -n 选项的情况下重新运行。
您可以使用 --backup 选项使 rsync 在覆盖时备份文件,
-b, --backup make backups (see --suffix & --backup-dir)
--backup-dir=DIR make backups into hierarchy based in DIR
--suffix=SUFFIX backup suffix (default ~ w/o --backup-dir)
Run Code Online (Sandbox Code Playgroud)
运行rsync后可以扫描backup-dir中的文件,并一一询问是否需要恢复。
遗憾的是,在撰写本文时,Rsync 中没有内置方法。
Mike Fitzpatrick 的解决方案可以正常工作,但是如果您有一个非常大的目录树,您可能想要做一些不会让 rsync 再次遍历所有文件的事情
编辑:还有一个错误,它不会删除目标文件......我看它的次数越来越多,这个解决方案被破坏了......我将它保留下来,因为它可能适用于你的情况,如果有人想要的话要解决这个问题。另外,应该有人向https://bugzilla.samba.org/enter_bug.cgi?product=rsync提交正式的功能请求
我写了这个脚本:
#! /bin/bash
# Make a temp file for storing the output of rsync
tmpfile=$( mktemp ) &&
# Do all the hard work ( get the list of files we need to update ),
# but dont actually change the filesystem
rsync --dry-run --out-format='RSYNC_CONFIRM %i %n%L' "$@" | grep RSYNC_CONFIRM | awk '{ print $3 }' > $tmpfile &&
# Output to the user what we propose to do
rsync --dry-run --itemize-changes --files-from=$tmpfile "$@" &&
# Alternatively, we could just output $tmpfile... but whatever...
read -p "Continue? (y/n)?" &&
if [[ $REPLY = [yY] ]]
then
{
rsync --files-from=$tmpfile "$@"
}
fi
rm $tmpfile
Run Code Online (Sandbox Code Playgroud)
尝试将脚本粘贴到名为 rsync-confirm.bash
然后 chmod +x rsync-confirm.bash
然后 ./rsync-confirm.bash -rvh /etc/ /tmp/etc/
这个脚本可能有点问题,我注意到如果源目录上没有斜杠,它就不太喜欢它......
代替官方的“--confirm”选项,可以利用 rsync 自己的“批处理模式”来绕过两次计算两条路径之间的差异。批处理模式旨在将更改分发到镜像(计算一次,更新许多相同的树)。创建两个文件:一个包含更新的批处理文件和一个用于运行更新的简单便捷脚本( rsync 手册页末尾有一个包含详细信息的部分)。
这是一个用于抽象进程的 bash 包装器:
#!/bin/bash
cleanup ()
{
rm ${BFILE} ${BFILE}.sh &>/dev/null
}
# generate tmpfile
BFILE=$( mktemp )
# rsync command
if ! rsync --only-write-batch="${BFILE}" --verbose "$@"; then
cleanup
exit 1
fi
# confirmation
read -p "Continue (y/N)? " confirm
if [ "$confirm" != "y" ]; then
echo "Aborting"
cleanup
exit 1
fi
# carve up arguments
dest="${@: -1}" # last argument
host="${dest%%:*}" # host:path
path="${dest#*:}"
opts="${@:1:$(($#-2))}" # everything but the last two args
if [ "$host" = "${path}" ]; then
# local
sh "${BFILE}.sh"
else
# remote
ssh "$host" rsync --read-batch=- "${opts}" "${path}" <"${BFILE}"
fi
cleanup
Run Code Online (Sandbox Code Playgroud)
请注意,由于此脚本写入$TMP_DIR,因此如果您要移动非常大的数据(例如大于 /tmp ),您可能会遇到空间限制。
| 归档时间: |
|
| 查看次数: |
9793 次 |
| 最近记录: |