macos 使远程目录保持最新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep Remote Directory Up-to-date
提问by Jake McGraw
I absolutely love the Keep Remote Directory Up-to-datefeature in Winscp. Unfortunately, I can't find anything as simple to use in OS X or Linux. I know the same thing can theoreticallybe accomplished using changedfilesor rsync, but I've always found the tutorials for both tools to be lacking and/or contradictory.
我非常喜欢Winscp 中的保持远程目录最新功能。不幸的是,我在 OS X 或 Linux 中找不到任何简单易用的东西。我知道理论上可以使用changedfiles或rsync完成同样的事情,但我总是发现这两种工具的教程都缺乏和/或相互矛盾。
I basically just need a tool that works in OSX or Linux and keeps a remote directory in sync (mirrored) with a local directory while I make changes to the local directory.
我基本上只需要一个在 OSX 或 Linux 中工作的工具,并在我对本地目录进行更改时使远程目录与本地目录同步(镜像)。
Update
更新
Looking through the solutions, I see a couple which solve the general problem of keeping a remote directory in sync with a local directory manually. I know that I can set a cron task to run rsync every minute, and this should be fairly close to real time.
查看解决方案,我看到一对解决了手动保持远程目录与本地目录同步的一般问题。我知道我可以设置一个 cron 任务来每分钟运行一次 rsync,这应该非常接近实时。
This is not the exact solution I was looking for as winscp does this and more: it detects file changes in a directory (while I work on them) and then automatically pushes the changes to the remote server. I know this is not the best solution (no code repository), but it allows me to very quickly test code on a server while I develop it. Does anyone know how to combine rsync with any other commands to get this functionality?
这不是我正在寻找的确切解决方案,因为 winscp 会这样做以及更多:它检测目录中的文件更改(当我处理它们时),然后自动将更改推送到远程服务器。我知道这不是最好的解决方案(没有代码存储库),但它允许我在开发代码时非常快速地在服务器上测试代码。有谁知道如何将 rsync 与任何其他命令结合使用来获得此功能?
采纳答案by dwj
How "real-time" do you want the syncing? I would still lean toward rsync since you know it is going to be fully supported on both platforms (Windows, too, with cygwin) and you can run it via a cron job. I have a super-simple bash file that I run on my system (this does notremove old files):
您希望同步有多“实时”?我仍然倾向于 rsync,因为您知道它将在两个平台(Windows 也是如此,使用 cygwin)上得到完全支持,并且您可以通过 cron 作业运行它。我有一个超级简单的bash的文件,我在我的系统上运行(这并不会删除旧文件):
#!/bin/sh
rsync -avrz --progress --exclude-from .rsync_exclude_remote . remote_login@remote_computer:remote_dir
# options
# -a archive
# -v verbose
# -r recursive
# -z compress
Your best bet is to set it up and try it out. The -n (--dry-run)
option is your friend!
最好的办法是设置并试用它。该-n (--dry-run)
选项是你的朋友!
Keep in mind that rsync (at least in cygwin) does not support unicode file names (as of 16 Aug 2008).
请记住,rsync(至少在 cygwin 中)不支持 unicode 文件名(截至 2008 年 8 月 16 日)。
回答by juwens
lsyncdseems to be the perfect solution. it combines inotify(kernel builtin function which watches for file changes in a directory trees) and rsync(cross platform file-syncing-tool).
lsyncd似乎是完美的解决方案。它结合了inotify(内核内置函数,用于监视目录树中的文件更改)和rsync(跨平台文件同步工具)。
lsyncd -rsyncssh /home remotehost.org backup-home/
Quote from github:
引用自 github:
Lsyncd watches a local directory trees event monitor interface (inotify or fsevents). It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync. Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or blockdevices and does not hamper local filesystem performance.
Lsyncd 监视本地目录树事件监视器接口(inotify 或 fsevents)。它聚合和组合事件几秒钟,然后生成一个(或多个)进程以同步更改。默认情况下,这是 rsync。因此,Lsyncd 是一种轻量级的实时镜像解决方案,安装相对容易,不需要新的文件系统或块设备,并且不会影响本地文件系统的性能。
回答by KevinButler
What you want to do for linux remote access is use 'sshfs' - the SSH File System.
您想要为 linux 远程访问做的是使用“sshfs”——SSH 文件系统。
# sshfs username@host:path/to/directory local_dir
Then treat it like an network mount, which it is...
然后把它当作一个网络挂载,它是......
A bit more detail, like how to set it up so you can do this as a regular user, on my blog
在我的博客上有更多细节,例如如何设置它以便您可以作为普通用户执行此操作
If you want the asynchronous behavior of winSCP, you'll want to use rsync combined with something that executes it periodically. The cron solution above works, but may be overkill for the winscp use case.
如果您想要 winSCP 的异步行为,您需要将 rsync 与定期执行它的东西结合使用。上面的 cron 解决方案有效,但对于 winscp 用例可能有点过分。
The following command will execute rsync every 5 seconds to push content to the remote host. You can adjust the sleep time as needed to reduce server load.
以下命令将每 5 秒执行一次 rsync 以将内容推送到远程主机。您可以根据需要调整睡眠时间以减少服务器负载。
# while true; do rsync -avrz localdir user@host:path; sleep 5; done
If you have a very large directory structure and need to reduce the overhead of the polling, you can use 'find':
如果你有一个非常大的目录结构并且需要减少轮询的开销,你可以使用'find':
# touch -d 01/01/1970 last; while true; do if [ "`find localdir -newer last -print -quit`" ]; then touch last; rsync -avrz localdir user@host:path; else echo -ne .; fi; sleep 5; done
And I said cron may be overkill? But at least this is all just done from the command line, and can be stopped via a ctrl-C.
我说 cron 可能有点矫枉过正?但至少这一切都是从命令行完成的,可以通过 ctrl-C 停止。
kb
千字节
回答by Marie Fischer
To detect changed files, you could try fam (file alteration monitor) or inotify. The latter is linux-specific, fam has a bsd port which might work on OS X. Both have userspace tools that could be used in a script together with rsync.
要检测更改的文件,您可以尝试 fam(文件更改监视器)或 inotify。后者是特定于 linux 的,fam 有一个可以在 OS X 上运行的 bsd 端口。两者都有可以与 rsync 一起在脚本中使用的用户空间工具。
回答by webasdf
I have the same issue. I loved winscp "keep remote directory up to date" command. However, in my quest to rid myself of Windows, I lost winscp. I did write a script that uses fileschanged and rsync to do something similar much closer to real time.
我有同样的问题。我喜欢 winscp“保持远程目录最新”命令。然而,为了摆脱 Windows,我失去了 winscp。我确实编写了一个脚本,它使用 fileschanged 和 rsync 来执行更接近实时的类似操作。
How to use:
如何使用:
- Make sure you have fileschanged installed
- Save this script in /usr/local/bin/livesync or somewhere reachable in your $PATH and make it executable
- Use Nautilus to connect to the remote host (sftp or ftp)
- Run this script by doing livesync SOURCE DEST
- The DEST directory will be in /home/[username]/.gvfs/[path to ftp scp or whatever]
- 确保你已经安装了文件更改
- 将此脚本保存在 /usr/local/bin/livesync 或 $PATH 中可访问的某个位置并使其可执行
- 使用 Nautilus 连接到远程主机(sftp 或 ftp)
- 通过执行 livesync SOURCE DEST运行此脚本
- DEST 目录将在 /home/[username]/.gvfs/[path to ftp scp 或其他地方]
A Couple downsides:
一些缺点:
- It is slower than winscp (my guess is because it goes through Nautilus and has to detect changes through rsync as well)
- You have to manually create the destination directory if it doesn't already exist. So if you're adding a directory, it won't detect and create the directory on the DEST side.
- Probably more that I haven't noticed yet
- Also, do not attempt to synchronize a SRC directory named "rsyncThis". That will probably not be good :)
- 它比 winscp 慢(我的猜测是因为它通过 Nautilus 并且还必须通过 rsync 检测更改)
- 如果目标目录不存在,您必须手动创建它。因此,如果您要添加目录,它不会在 DEST 端检测和创建目录。
- 可能还有更多我还没注意到
- 另外,不要尝试同步名为“rsyncThis”的 SRC 目录。那可能不太好:)
#!/bin/sh
upload_files()
{
if [ "$HOMEDIR" = "." ]
then
HOMEDIR=`pwd`
fi
while read input
do
SYNCFILE=${input#$HOMEDIR}
echo -n "Sync File: $SYNCFILE..."
rsync -Cvz --temp-dir="$REMOTEDIR" "$HOMEDIR/$SYNCFILE" "$REMOTEDIR/$SYNCFILE" > /dev/null
echo "Done."
done
}
help()
{
echo "Live rsync copy from one directory to another. This will overwrite the existing files on DEST."
echo "Usage: ##代码## SOURCE DEST"
}
case "" in
rsyncThis)
HOMEDIR=
REMOTEDIR=
echo "HOMEDIR=$HOMEDIR"
echo "REMOTEDIR=$REMOTEDIR"
upload_files
;;
help)
help
;;
*)
if [ -n "" ] && [ -n "" ]
then
fileschanged -r "" | "##代码##" rsyncThis "" ""
else
help
fi
;;
esac
回答by icco
You could always use version control, like SVN, so all you have to do is have the server run svn up on a folder every night. This runs into security issues if you are sharing your files publicly, but it works.
你总是可以使用版本控制,比如 SVN,所以你所要做的就是让服务器每晚在一个文件夹上运行 svn。如果您公开共享文件,这会遇到安全问题,但它有效。
If you are using Linux though, learn to use rsync. It's really not that difficult as you can test every command with -n. Go through the man page, the basic format you will want is
如果您使用的是 Linux,请学习使用 rsync。这真的不难,因为您可以使用 -n 测试每个命令。浏览手册页,您需要的基本格式是
rsync [OPTION...] SRC... [USER@]HOST:DEST
rsync [OPTION...] SRC... [USER@]HOST:DEST
the command I run from my school server to my home backup machine is this
我从学校服务器运行到家庭备份机的命令是这样的
rsync -avi --delete ~ me@homeserv:~/School/ >> BackupLog.txt
rsync -avi --delete ~ me@homeserv:~/School/ >> BackupLog.txt
This takes all of the files in my home directory (~) and uses rsync's archive mode (-a), verbosly (-v), lists all of the changes made (-i), while deleting any files that don't exist anymore (--delete) and puts the in the Folder /home/me/School/ on my remote server. All of the information it prints out (what was copied, what was deleted, etc.) is also appended to the file BackupLog.txt
这将获取我的主目录 (~) 中的所有文件并使用 rsync 的归档模式 (-a)、详细 (-v)、列出所做的所有更改 (-i),同时删除任何不再存在的文件(--delete) 并将其放在远程服务器上的文件夹 /home/me/School/ 中。它打印出的所有信息(复制的内容、删除的内容等)也附加到文件 BackupLog.txt
I know that's a whirlwind tour of rsync, but I hope it helps.
我知道这是 rsync 的旋风之旅,但我希望它有所帮助。
回答by Pat Notz
The rsync
solutions are really good, especially if you're only pushing changes one way. Another great tool is unison
-- it attempts to syncronize changes in both directions. Read more at the Unison homepage.
该rsync
解决方案是非常好的,特别是如果你只推变化的一种方式。另一个很棒的工具是unison
——它试图同步两个方向的变化。在Unison 主页上阅读更多信息。
回答by stimms
It seems like perhaps you're solving the wrong problem. If you're trying to edit files on a remote computer then you might try using something like the ftp plugin for jedit. http://plugins.jedit.org/plugins/?FTPThis ensures that you have only one version of the file so it can't ever be out of sync.
看来您可能正在解决错误的问题。如果您尝试在远程计算机上编辑文件,那么您可以尝试使用 jedit 的 ftp 插件之类的东西。 http://plugins.jedit.org/plugins/?FTP这确保您只有一个版本的文件,因此它永远不会不同步。
回答by Daniel Papasian
Building off of icco's suggestion of SVN, I'd actually suggest that if you are using subversion or similar for source control (and if you aren't, you should probably start) you can keep the production environment up to date by putting the command to update the repository into the post-commit hook.
基于 icco 对 SVN 的建议,我实际上建议,如果您使用 subversion 或类似软件进行源代码控制(如果不是,您可能应该开始),您可以通过放置命令来使生产环境保持最新将存储库更新为 post-commit 钩子。
There are a lot of variables in how you'd want to do that, but what I've seen work is have the development or live site be a working copy and then have the post-commit use an ssh key with a forced command to log into the remote site and trigger an svn up on the working copy. Alternatively in the post-commit hook you could trigger an svn export on the remote machine, or a local (to the svn repository) svn export and then an rsync to the remote machine.
您想如何做到这一点有很多变数,但我所看到的工作是让开发或实时站点成为工作副本,然后让提交后使用带有强制命令的 ssh 密钥登录远程站点并在工作副本上触发 svn。或者,在提交后挂钩中,您可以在远程机器上触发 svn 导出,或本地(到 svn 存储库)svn 导出,然后 rsync 到远程机器。
I would be worried about things that detect changes and push them, and I'd even be worried about things that ran every minute, just because of race conditions. How do you know it's not going to transfer the file at the very same instant it's being written to? Stumble across that once or twice and you'll lose all of the time-saving advantage you had by constantly rsyncing or similar.
我会担心检测变化并推动它们的东西,我什至会担心每分钟都在运行的东西,只是因为竞争条件。你怎么知道它不会在写入文件的同时传输文件?偶然发现一次或两次,您将失去通过不断 rsync 或类似方式获得的所有节省时间的优势。
回答by Scott
Will DropBox (http://www.getdropbox.com/) do what you want?
DropBox ( http://www.getdropbox.com/) 会做你想做的吗?