How to RSYNC a single file?

后端 未结 4 1442
庸人自扰
庸人自扰 2021-01-31 06:33

Currently i only RSync-ing the Directories as like:

* * * * * rsync -avz /var/www/public_html/images root@:/var/www/public_html


        
相关标签:
4条回答
  • 2021-01-31 07:11

    You do it the same way as you would a directory, but you specify the full path to the filename as the source. In your example:

    rsync -avz   --progress  /var/www/public_html/.htaccess root@<remote-ip>:/var/www/public_html/
    

    As mentioned in the comments: since -a includes recurse, one little typo can make it kick off a full directory tree transfer, so a more fool-proof approach might to just use -vz, or replace it with -lptgoD.

    0 讨论(0)
  • 2021-01-31 07:12

    Basic syntax

    rsync options source destination
    

    Example

    rsync -az /var/www/public_html/filename root@<remote-ip>:/var/www/public_html
    

    Read more

    0 讨论(0)
  • 2021-01-31 07:14

    Michael Place's answer works great if, relative to the root directory for both the source and target, all of the directories in the file's path already exist.

    But what if you want to sync a file with this source path:

    /source-root/a/b/file

    to a file with the following target path:

    /target-root/a/b/file

    and the directories a and b don't exist?

    You need to run an rsync command like the following:

    rsync -r --include="/a/" --include="/a/b/" --include="/a/b/file" --exclude="*" [source] [target]
    
    0 讨论(0)
  • 2021-01-31 07:21

    To date, two of the answers aren't quite right, they'll get more than one file, and the other isn't as simple as it could be, here's a simpler answer IMO.

    The following gets exactly one file, but you have to create the dest directory with mkdir. This is probably the fastest option:

    mkdir -p ./local/path/to/file
    rsync user@remote:/remote/path/to/file/ -zarv --include "filename" --exclude "*" ./local/path/to/file/
    

    If there is only one instance of file in /remote/path, rsync can create directories for you if you do the following. This will probably take a little more time because it searches more directories. Plus it's will create empty directories for directories in /remote/path that are not in ./local

    cd ./local
    rsync user@remote:/remote/path -zarv --include "*/" --include "filename" --exclude "*" .
    

    Keep in mind that the order of --include and --exclude matters.

    0 讨论(0)
提交回复
热议问题