开发者

How to improve a 'rsync' command in order to create all sub-directories needed if those doesn't exist yet?

开发者 https://www.devze.com 2023-04-05 05:38 出处:网络
I am using Ruby on Rails and the Capistrano gem. I would like to improve the following command that runs in a recipe (I was inspired by the blog post \"Uploading files for enki using capistrano\"):

I am using Ruby on Rails and the Capistrano gem. I would like to improve the following command that runs in a recipe (I was inspired by the blog post "Uploading files for enki using capistrano"):

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

so to make possible to create sub-directories "on the fly". At this time, since on the remote machine the assets/users/001 directory does not exist yet, I get the following error:

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]

How can I create all needed sub-directories so to avoid errors?

BTW: what the -qrpt 开发者_高级运维part means?


Try adding the -R option:

-R, --relative              use relative path names

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

read http://ss64.com/bash/rsync.html

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


Not sure how you'd do it from within Rsync, but here's a Ruby solution.

Call safe_create_path with your desired path before the rsync script.

# 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
0

精彩评论

暂无评论...
验证码 换一张
取 消