Occasionally, there arises a need for me to download directories stored on my remote server, often for backup purposes. Yet, on each occasion, I find myself scouring the Internet to determine the precise arguments required for the rsync utility.
Those days are now relegated to history, as I have chosen today to write down a future-oriented reminder right here. 😅
Without further ado, here is the definitive command to download a remote directory on your computer:
# Adapt the variables below
SSH_PORT=22
SSH_USER=root
SSH_SERVER=138.197.142.29
REMOTE_SOURCE_DIRECTORY=/var/opt/mealie
LOCAL_DESTINATION_DIRECTORY=.
# From your local machine
rsync \
--archive \
--compress \
--human-readable \
--partial \
--progress \
--rsh "ssh -p ${SSH_PORT}" \
--stats \
--verbose \
"${SSH_USER}@${SSH_SERVER}:${REMOTE_SOURCE_DIRECTORY}" \
"${LOCAL_DESTINATION_DIRECTORY}"
Explanation of the arguments
--archive
: Preserves file permissions, ownership, timestamps, and more.--compress
: Enables compression of data during transfer, reducing the amount of data transmitted.--human-readable
: File sizes are displayed in a human-readable format (e.g., kB, Mb or gB).--partial
: If the transfer is interrupted, partially transferred files are keep instead of discarding them.--progress
: Displays a progress indicator.--rsh "ssh -p ${SSH_PORT}"
: The communication should be done over SSH using port 22. It is useful if your server use another port for SSH.--stats
: Provides a summary of the transfer statistics once the process is completed.--verbose
: Increases the verbosity of the output, offering more detailed information."${SSH_USER}@${SSH_SERVER}:${REMOTE_SOURCE_DIRECTORY}:
: The source directory on the remote server that you wish to back up."${LOCAL_DESTINATION_DIRECTORY}"
: The destination directory on your local machine where the backup will be saved.