skip to Main Content
Laravel Sync Remote Files To Local Homestead

To do a Laravel Sync Remote Files to local Homestead Vagrant box you can use rsync. And you should use it over scp or sftp because it is way more efficient.

Why Rsync

Well, there is an awesome SO thread on this. And I will quote Rafa:

  • rsync will check files sizes and modification timestamps of both A and B, and skip any further processing if they match.
  • If the destination file B already exists, the delta transfer algorithm will make sure only differences between A and B are sent over the wire.
  • rsync will write data to a temporary file T, and then replace the destination file B with T to make the update look “atomic” to processes that might be using B.

As you can see this is way more efficient than sftp or scp because that is basically dumm copying of files and overwriting what you have already.

Rsync Command

The rsync command I use is:

rsync -chavzP --stats web@domain.com:/var/www/domain.com/shared/public/uploads/* .

Here a couple of remarks on this:

  • We assume you are in the directory locally where you want to store files. In most cases that is public/uploads/.
  • Two, we use user web here, but on your server or a Laravel Forge server this will be another user entirely.

Now on the flags -chavzP:

  • c checksum
  • h human readable
  • a recursion and preserve almost anything
  • v verbose
  • P partial progress
  • stats print a verbose set of statistics

Example

It will look like something like:

rsync -chavzP --stats web@domain.com:/var/www/domain.com/shared/public/uploads/* .
receiving file list ... 
1225 files to consider
1/
1/rotate_mobile.png
      33.14K 100%   31.61MB/s    0:00:00 (xfer#1, to-check=1223/1225)
1/rotate_mobile_thumbnail.jpg
       1.60K 100%  780.76kB/s    0:00:00 (xfer#2, to-check=1222/1225)
100/
100/2001356401.png
     141.57K 100%    1.05MB/s    0:00:00 (xfer#3, to-check=1220/1225)
......

Also see explainshell.com and SO thread on rsync command

Bonus Trellis Usage

If you are using Roots Trellis and would like to sync all files from the production server you can use the same command from the local uploads folder. Just a different path really:

rsync -chavzP --stats web@domain.com:/srv/www/domain.com/current/web/app/uploads/* .

 

Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top