Quickly download the Magento 2 media folder with rsync

Downloading the media folder via SFTP can take hours. Fortunately, with rsync you can do this done easliy - and fast. All you need is the terminal and a working SSH connection to the server.

Step 1: create a bash script to download Magento 2 media

This command line script will download the entire Magento 2 media folder in seconds, and exclude the dreaded cache and tmp folders. You can use it to download a media folder to your local filesystem or sync media folders between servers.

Create a rsync_media.sh file in the root of your Magento 2 project. Make sure to set the variables under 1, 2 and 3.

#!/usr/bin/env bash
# Download all media from the production server, except for cached files.
# SET VARIABLES HERE

# 1. SSH connection
SSH_PRIVATE_KEY="~/.ssh/YOURPRIVATEKEY"
SSH_PORT=22
SSH_USER="YOURUSER"
SSH_HOST="YOURSERVER.COM"
# 2. Paths. Include a trailing slash!
REMOTE_PATH="/remote/path/to/magento2/pub/media/"
LOCAL_PATH="/local/path/to/magento2/pub/media/"
# 3. Paths and files to exclude, separated by a space. You can also include files
EXCLUDED_FILES="catalog/product/cache images/cache sitemap tmp"
EXCLUDED_FILES_FILENAME="excluded-media-files.txt"
# DO NOT EDIT BELOW THIS POINT
printf '%s\n' $EXCLUDED_FILES > $EXCLUDED_FILES_FILENAME rsync -arve "ssh -p $SSH_PORT -i $SSH_PRIVATE_KEY" $SSH_USER@$SSH_HOST:$REMOTE_PATH $LOCAL_PATH --exclude-from $EXCLUDED_FILES_FILENAME

Step 2: run the bash script you just created

  1. Open your terminal.
  2. Cd to the folder that contains script rsync_media.sh.
  3. Run sh rsync_media.sh
  4. The script will output receiving file list...
  5. After that, any files that are not yet local will be downloaded. Typical output is:
receiving file list … done

.thumbsBanners/EN_Outlet.jpg
.thumbsBanners/NL_Outlet.jpg
Banners/EN_Outlet.jpg
Banners/NL_Outlet.jpg
[...]
sent 2593 bytes received 10679999 bytes 1942289.45 bytes/sec
total size is 6291331341 speedup is 588.93

Of course, you can adjust the script to download an entire Magento 2 shop from the server. 

You can find the gist here.