Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 17System · SSH

How to copy files between servers

Moving a backup to another server, deploying a build, pulling logs to your laptop, migrating a website — all of it goes over SSH. scp is the quick tool for a few files; rsync is the right tool for folders, big files and repeated copies, because it sends only what changed and can resume. This how-to shows both, with ports, jump hosts, server-to-server copies and how to prove the copy is complete.

  • 5 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

17.1

The quick answer

scp backup.tar.gz web2:/tmp/
scp web2:/var/log/nginx/error.log .
scp -r ./site web2:/srv/
rsync -avzP ./site/ web2:/srv/site/
rsync -avzP --delete --dry-run ./site/ web2:/srv/site/

web2 is a host from your ~/.ssh/config (or user@IP). Both tools use your SSH keys and the same port rules as ssh.

17.2

Step by step

  1. One file or a few files → scp:
scp -P 2222 app.jar deploy@192.0.2.20:/opt/app/
scp -J bastion db-dump.sql.gz admin@10.0.1.21:/backup/
scp -p report.pdf web2:/srv/reports/

-P port (capital P), -J through a jump host, -p keep times and modes.

  1. Folders, big files, repeated syncs → rsync:
rsync -avzP ./site/ web2:/srv/site/
rsync -avzP -e 'ssh -p 2222' ./site/ deploy@192.0.2.20:/srv/site/
rsync -avzP --exclude='.git' --exclude='node_modules' ./ web2:/srv/app/
rsync -aHAX --numeric-ids /srv/data/ backup1:/backup/data/
OptionMeaning
-aArchive: folders, links, times, modes, owners (as root).
-v / -zShow files / compress on the way.
-PShow progress and keep partial files (resume a broken copy).
--deleteDelete files on the target that are gone in the source (a real mirror).
-n / --dry-runShow what would happen, change nothing.
-HAXKeep hard links, ACLs and extended attributes (system data).

Onthoud

the trailing slash matters. ./site/ = the contents of site; ./site = the folder site itself (you get /srv/site/site).

  1. **Always dry-run before --delete:**
rsync -avn --delete ./site/ web2:/srv/site/ | grep deleting
  1. Server to server (your laptop in the middle, no keys between the servers):
scp -3 web1:/backup/shop.tar.gz web2:/backup/

Or log in to web1 and push to web2 directly (faster, needs SSH access from web1 to web2).

  1. Interactive browsing (look first, then get files):
sftp web2
sftp> cd /var/log/nginx
sftp> get access.log
sftp> bye

17.3

Check that it worked

Compare checksums on both sides — size alone is not proof:

sha256sum backup.tar.gz
ssh web2 sha256sum /tmp/backup.tar.gz
rsync -avnc ./site/ web2:/srv/site/

The two hashes must be the same. rsync -avnc (checksum dry-run) lists no files when both sides are equal.

17.4

Common problems

ProblemCauseFix
Permission denied (publickey)Key not on the target, or wrong user.ssh web2 first; fix the key login (ssh-copy-id).
Permission denied on the target pathNo write right in that folder.Copy to /tmp or your home, then sudo mv; or fix the owner.
You get /srv/site/siteNo trailing slash on the source.Use ./site/ (with slash) to copy the contents.
A big copy broke in the middleNetwork cut.Run the same rsync -P again — it resumes.
rsync: command not foundrsync must be installed on both sides.dnf install rsync / apt install rsync on the target too.
Old scp script fails with "protocol" errorsSince OpenSSH 9, scp uses the SFTP protocol.Use scp -O (old protocol) for very old servers, or switch to rsync.

17.5

Go further

1Why is rsync better than scp for a 20 GB folder?

It sends only changed files, can resume a broken copy (-P), and can mirror with --delete.

2What is the difference between rsync ./site/ host:/srv/site/ and rsync ./site host:/srv/site/?

With the slash, the contents of site are copied; without it, the folder itself (you get /srv/site/site).

Alle commando's in dit hoofdstuk