【问题标题】:How to improve a 'rsync' command in order to create all sub-directories needed if those doesn't exist yet?如果这些子目录尚不存在,如何改进“rsync”命令以创建所需的所有子目录?
【发布时间】:2011-09-15 11:08:39
【问题描述】:

我正在使用 Ruby on Rails 和 Capistrano gem。我想改进以下在配方中运行的命令(我受到博文“Uploading files for enki using capistrano”的启发):

rsync -qrpt --delete --rsh=ssh public/system/assets/users/001 #{user}@#{domain}:/www/.../shared/system/assets/001

这样可以“即时”创建子目录。此时,由于远程机器上assets/users/001目录还不存在,所以报错如下:

rsync: mkdir "/www/.../shared/system/assets/users/001" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(595) [Receiver=3.0.7]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /SourceCache/rsync/rsync-40/rsync/io.c(452) [sender=2.6.9]

如何创建所有需要的子目录以避免错误?

顺便说一句-qrpt 部分是什么意思?

【问题讨论】:

  • "man rsync" 告诉我 -qrpt 表示安静、递归、保留权限和保留时间戳。

标签: ruby-on-rails ruby ruby-on-rails-3 directory capistrano


【解决方案1】:

尝试添加 -R 选项:

-R, --relative              use relative path names

http://ubuntuforums.org/showthread.php?t=1670723

阅读http://ss64.com/bash/rsync.html

-r, --recursive             recurse into directories
-p, --perms                 preserve permissions
-q, --quiet                 decrease verbosity
-t, --times                 preserve times

【讨论】:

    【解决方案2】:

    不确定如何在 Rsync 中执行此操作,但这里有一个 Ruby 解决方案。

    在 rsync 脚本之前使用您想要的路径调用 safe_create_path

    # Creates a path with all subdirectories in case any doesn't exist.
    def safe_create_path(path_name)
      safe_create_helper(path_name)
    end
    
    def safe_create_helper(path_name)
      dir_name = File.dirname(path_name)
      if (!File.directory?(dir_name))
        safe_create_helper(path_name)
      end
      Dir.mkdir(path_name)
    end
    

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 2014-01-07
      • 1970-01-01
      • 2022-07-02
      • 1970-01-01
      • 2017-10-20
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多