Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« Flawed Microbenchmarks
Writing an XPath expression evaluator »

rsync to remote server via ssh

March 1st, 2005

If you ever need to repeatedly upload to a remote server, here is the command line of the day:

rsync -zrptL --delete-after -e "ssh"  --include=core --include=tags --exclude=.DS_Store --cvs-exclude /local/dir user@host:/remote/dir/

rsync sends only the files that have changed. I just tried rsync today for the first time and I’m impressed. Its far faster than all of the ftp based synchronization tools I’ve used. Unfortunately, it only goes in one direction. Here are the meanings of the options:

z – use compression
r – recurse directories
p – preserve permissions
t – preserve times
L – copy (flatten) symbolic links

–delete-after – gets rid of any files in the remote directory that are not in the local directory. It prevents old files from getting orphaned on the server.

Put any ssh parameters you need inside the quotes.

I develop on Mac OS X, so –exclude=.DS_Store gets rid of those annoying little files.

–cvs-exclude gets rid of all the crud that CVS leaves lying around.

–include=core and –include=tags are my very hard won lesson of today. My program has a core and a tags directory. Tags is a special directory for cvs and removed by the –cvs-exclude option. Any file or directory named core is also removed by this option. –include puts em back. The other –cvs-exclude patterns are less likely to collide. I think –include has to come before the –cvs-exclude on the command line.

The trailing slash (or absence of one) on the source and destination directories matter.

Filed Under

  • Mac, Web Design

Related Posts

  • Knocked off the internet, a story of Windows and Macintosh
  • Web Page Loading Performance
  • A Comparison of the PHP and Java Job Markets
  • Manual Memory Management is Dead
  • PHP Games
You can leave a response, or trackback from your own site.

38 Responses to “rsync to remote server via ssh”

  1. Wez says:
    3/1/2005 at 4:30 pm

    rsync works both ways; pushing and pulling data.

  2. John McAdams says:
    3/1/2005 at 7:03 pm

    rsync goes up and down. To sync from remote to local just reverse the source and destination.

    Try this link for a nice shell script and a modified version of rsync that can handle HFS+ resource forks. .

  3. Sencer says:
    3/1/2005 at 7:45 pm

    Take a look at Unison:
    http://www.cis.upenn.edu/~bcpierce/unison/

    It’s for synchronizing and also implements the rsync-algo for efficient bandwidth usage.

  4. Bill Lovett says:
    3/1/2005 at 8:44 pm

    Look into Unison. It’s like rsync, but works in both directions.

    http://www.cis.upenn.edu/~bcpierce/unison/

  5. Jeff Moore’s Blog » Blog Archive » Knocked off the internet, a story of Windows and Macintosh says:
    3/15/2005 at 9:41 am

    [...] ter now, but I still decided that I didn’t want to try that route. Besides, I just got my automated deployment scripts working on the Mac. So Monday morni [...]

  6. Mike says:
    3/24/2005 at 4:10 pm

    From the root of my project, I use:
    rsync -avzu –exclude-from=rsync-exclude.txt * host:`pwd`

    from my development server to the live server where the paths are the same.

    I list a bunch of files in rsync-exclude.txt like images used only in production, files like CHANGELOG, etc. This file gets added to my CVS repository with each project.

    I don’t use the –cvs-exclude because i tag my project before a release/upload and `cvs export -r TAGNAME project` so you won’t have CVS/ directories, etc.

    The -n option is great and I encourage you use it before running it without. It will print you a list of all the files it wants to upload, allowing you to double check that you are about to upload anything by accident.

  7. JTK - Notes and Ramblings » Blog Archive » Jeff Moore%u2019s Blog � Blog Archive � rsync to remote server via ssh says:
    4/1/2005 at 8:10 am

    [...] Jeff Moore%u2019s Blog � Blog Archive � rsync to remote server via ssh Jeff Moore%u2019s Blog � Blog Archive � rsync to remote server vi [...]

  8. James says:
    5/19/2005 at 12:10 am

    –exclude-cvs considered harmful.

    Well, it’s harmed me at least. Consider the case where you’re editing files on a ‘remote’ machine prior to committing them via a central machine ‘closer’ to the CVS repository(*).

    Consider the following: you cvs update, rsync ‘away’ to another machine and do some editing there, cvs update again (whoops), then then rsync ‘back’ (disaster). You’ve now got ‘CVS/entries’ that say you’ve got the most recent files on the central system, but you’ve actually just rsync’d old versions of the files back. CVS will think you’re trying to ‘un-modify’ all the files back to where they were when you first CVS updated.

    If you always keep the CVS meta files with the files they relate to, that sequence of events will simply result in CVS complaing you have state files.

    Trust me. If you think you can simply just remember where you are and always update/sync in the right order, you’re kidding yourself. If you’re rsyncing checked-out CVS trees, its far too easy to mess up: blow the cost: sync your CVS meta data!

    (*) pserver seems like a solution here, but consider the case where you need to test code on more than one platform *before* committing the code.

  9. Burton Haynes says:
    3/5/2010 at 11:18 pm

    Thanks for this post, answers a bunch of questions I was having.

  10. David Carrington says:
    9/20/2010 at 1:17 am

    Thanks for this blog post. It helped me confirm the cause of a problem during our latest code release: the “core” folder was not rsync’d across to the live server because of the –cvs-exclude option. –include=core solved this :)

  11. Petr Halounek says:
    10/6/2011 at 8:25 am

    Hello all,

    I need some help with rsync functionality for ‘files-from=’.

    I tried to build automatic script, to getting file list (1), which are old more than one year .. and passing them in file to remote rsync(2). But without reason there is about 50% of unprocessed files, no error are returned. Its look like the whole file.list isn’t processed, but some files are processed.

    (0)You need to have your public key in your authorized_hosts.(you can do ’ssh localhost’ whitout password)
    cat /root/.ssh/id_dsa.pub >> /root/.ssh/authorized_keys

    (1) this works (you can find result in file called files.list)
    ssh -T localhost -o StrictHostKeyChecking=no “sshpass -p ‘remote_password’ ssh remote_server -p 22 -o ConnectTimeout=2 -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o NumberOfPasswordPrompts=1 ‘find /var/spool/asterisk/monitor/ -type f -mtime +365 -exec echo \{\} \; ‘” 2>&1> files.list

    (2) this doesn’t work (you can see in rsync.log, that there are no all files processed but why ?
    rsync –bwlimit=2000 –timeout=2 –files-from=files.list -avH –rsh=”sshpass -p ‘remote_password’ ssh -l root” remote_server:/ /mnt/backup/ 2>&1> rsync.log
    echo $? // returning (0)

    PS: when you try ‘retarded’ command that don’t use ssh remote, whole files.list is procesed / moved
    rsync –files-from=files.list -avH / /mnt/backup/ 2>&1> rsync.log

    Any ideas how to solve this (strange issue)?

  12. Briana says:
    10/17/2011 at 6:00 am

    After 2 hours, I will be at home. There I will be able to say more on this.

  13. Sophia Skirving says:
    10/27/2011 at 8:58 pm

    Hey! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My site discusses a lot of the same subjects as yours and I think we could greatly benefit from each other. If you’re interested feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!

  14. a4268918 says:
    11/4/2011 at 9:03 pm

    I’ve said that least 4268918 times. The problem this like that is they are just too compilcated for the average bird, if you know what I mean

  15. 775highbench says:
    11/8/2011 at 12:49 am

    hi, demonstration gratitude you your script appearance is impressive. exactly saved your set on bing. skyway blow later for affirmatory :)

  16. aboutmyzimbabwe says:
    11/8/2011 at 8:28 pm

    hey, i found your blog upon yahoo.it’s certainly hale written also it helped me lots. continue the suitable work.

  17. a keyword research says:
    11/8/2011 at 11:38 pm

    Thanks for the good article. I even have learned from this article, Love the site!

  18. blackfriday says:
    11/8/2011 at 11:49 pm

    To purchase online. This item has been discounted at this link.| Black Friday Sale Philips AS351/37 Fidelio Docking Speaker for Android | Black Friday Philips AS351/37

  19. Debbra Cordner says:
    11/9/2011 at 6:59 am

    I follow

  20. Online Shopping Black Friday says:
    11/10/2011 at 9:23 pm

    I am usually to blogging and that i truly recognize your content. The article has really peaks my interest. I’m progressing to bookmark your site and hold checking for whole fresh data.

  21. Replace Laptop Screen says:
    11/12/2011 at 2:42 am

    Thank you for your nice post! it’s been terribly helpful. I hope that you simply will continue sharing your information with us.

  22. Replace Laptop Screen says:
    11/12/2011 at 4:06 am

    Good article and right to the purpose. I don’t know if this is often truly the most effective place to ask however does one individuals have any thoughts on where to hire some skilled writers? Thanks before

  23. Clark Weagle says:
    11/15/2011 at 8:29 pm

    good job thanks for the post

  24. Dui Lawyers LosAngeles says:
    11/16/2011 at 2:11 am

    Ha, fun to learn something new. Tho I’m not sure I agree with the point. Nonetheless, share more :)

  25. Harvey Ferbrache says:
    11/16/2011 at 2:31 am

    good work thanks for the publish

  26. Elbert Hershberg says:
    11/17/2011 at 12:28 am

    an attention-grabbing viewpoint!

  27. Rosanna Surman says:
    11/20/2011 at 8:25 am

    a useful and helpful however of info

  28. Ashlyn Dimarzio says:
    12/19/2011 at 2:59 am

    Heya i am for the first time here. I came across this board and I find It truly helpful & it helped me out much. I am hoping to give something back and help others such as you aided me.

  29. Stefan Enger says:
    12/27/2011 at 4:16 am

    Interesting.
    I guess they analyzed that they might get the half percent passed, otherwise why not ask for 1 percent?

  30. Natalie Rodarta says:
    1/1/2012 at 7:55 pm

    Cool info it is without doubt. My father has been searching for this update.

  31. atomboygame says:
    1/9/2012 at 6:21 am

    As I hyperlink somebody i consider the volume concern here is rattling blow up, value anybody for your causes. You should relate it up perpetually! fortunate of fortune.

  32. Minta Constantineau says:
    2/8/2012 at 2:54 am

    Howdy, i read your blog occasionally and i own a similar one and i was just questioning in the event you get a lot of spam comments? If so how do you avoid it, any plugin or anything you’ll be able to suggest? I’m acquiring so a lot lately it’s driving me mad so any aid is exceptionally much appreciated.

  33. ??????? says:
    2/20/2012 at 9:07 am

    Beautiful site. I like your websites.Its usefull for me website in the World Its usefull for me. Thanks.I Like to Play baccarat online.

  34. ????????? says:
    2/21/2012 at 10:55 am

    This is a Beautiful site. I like your websites.Its usefull for me blogs in the galaxy Its usefull for me. Thanksyou.I Like to Play baccarat online.

  35. ????????????????? says:
    2/22/2012 at 7:51 am

    This is a great site. I like your blogs.Its usefull for me blogs in the galaxy Its usefull for me. Thanksyou.I Like to Play travel online.

  36. ????? says:
    2/22/2012 at 11:11 am

    Thats is a great websites. I like your blogs.Its usefull for me blogs in the wold Its profit for me. Thanks you.I Like to join travel online.

  37. Rodney Kohnen says:
    3/13/2012 at 11:26 am

    What i don’t realize is actually how you’re not actually much more well-liked than you may be right now. You are so intelligent. You realize therefore considerably relating to this subject, made me personally consider it from so many varied angles. Its like women and men aren’t fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs excellent. Always maintain it up!

  38. city break barcelona says:
    5/2/2012 at 8:03 pm

    city break barcelona…

    [...]rsync to remote server via ssh – Professional PHP[...]…

Leave a Reply

Click here to cancel reply.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (18)
    • Open Source (14)
    • PHP (99)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  258
      Tuan Lal, Lavagem de estofados, Edward L. Kind [...]
    • php | tek 2008  36
      how to mend ice machine, Akademija Debelih, Odbacena [...]
    • goto in PHP  59
      kasor, Thomas Valdivieso, Murray Ziadie [...]
    • Firefox Extensions for Web Developers  33
      kasor, Website Design Toronto, mobila bistrita [...]
    • Why PHP is easier to learn than Java  68
      kasor, Justina Calvery, Guy Lipton [...]
    • Meta Tag Refresh Faux Paux  43
      html email templates, E-Juice Reviews, image [...]
    • Improved Error Messages in PHP 5  49
      Carroll Tina, Przeprowadzka, Emery Harari [...]
    • Benchmarking PHP's Magic Methods  33
      kayu oyunlar?,dora,oyun,oyna, Benjamin Bejjani, paypal website [...]
    • Microbenchmarks of single and double qouting.  24
      kefir grains minneapolis, sexshop dildo, tuim688 [...]
    • PEAR Templates  17
      Kandice Sansing, car insurance estimates for teenagers, Dale Brence [...]
  • Recent Posts

    • Richard Thomas
    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
  • Site

    • Archives
    • Log in
  • Search