๐Ÿ”„ Rsync Data from Local Folder to Remote Folder (with SSH Key Setup)

๐Ÿ“„ Overview

This guide walks you through securely copying files from a local folder to a remote server using rsync over SSH โ€” including setting up SSH key-based authentication.


โœ… Prerequisites

  • SSH access to the remote server
  • rsync installed (dnf install rsync or yum install rsync)
  • ssh and ssh-keygen available on your local system
  • Firewall allows outbound connections on SSH port (default 22)

๐Ÿ” Step 1: Set Up SSH Key Authentication

1.1 Generate SSH Key (on the local server)

ssh-keygen -t rsa -b 4096 -C "[email protected]"
  • Press Enter to accept the default file path (~/.ssh/id_rsa)
  • Choose whether or not to set a passphrase

1.2 Copy SSH Public Key to Remote Server

ssh-copy-id -p 22 [email protected]
  • Replace user123 and example-backup-server.com with your details
  • You’ll be prompted to enter the remote user’s password once

Alternatively, manually append the public key to the remote file:

cat ~/.ssh/id_rsa.pub | ssh -p 22 [email protected] 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh'

๐Ÿ› ๏ธ Step 2: Run Rsync Command

Basic Rsync

rsync -e 'ssh -p22' -ahvP /home/exampleuser/public_html/wp-content/uploads [email protected]:./backups

Now that SSH keys are in place, rsync can run without prompting for a password.


๐Ÿ’ก Optional Rsync Enhancements

๐Ÿงน Delete Remote Files Not Present Locally

rsync -e 'ssh -p22' -ahvP --delete /home/exampleuser/public_html/wp-content/uploads [email protected]:./backups

๐Ÿ“‰ Limit Bandwidth Usage (e.g. 1MB/s)

rsync -e 'ssh -p22' -ahvP --bwlimit=1024 /home/exampleuser/public_html/wp-content/uploads [email protected]:./backups

โฐ Step 3: Automate with Cron

Open the cron editor:

crontab -e

Add this to run rsync daily at 3:00 AM:

0 3 * * * rsync -e 'ssh -p22' -ahvP /home/exampleuser/public_html/wp-content/uploads [email protected]:./backups

๐Ÿงฐ Troubleshooting

ErrorSolution
Permission deniedCheck file permissions or SSH key setup
Host key verification failedManually SSH into the remote server to accept fingerprint
Connection timed outVerify firewall and SSH port availability
No such file or directoryDouble-check source and destination folder paths

โœ… Done!

You now have a secure, passwordless method for syncing local data to a remote server using rsync with SSH keys โ€” ideal for automated backups or file transfers.

Scroll to Top