Copying files between servers: scp, rsync and sftp Print

  • ssh, ftp, költöztetés, linux
  • 0

If you access the server via SSH, you can also copy files through it over an encrypted connection, without the need for a separate FTP account. You can choose from three tools:

  • scp: simple, for one-off copying;
  • rsync: for large amounts of data, migration and repeated synchronisation, as it only transfers changes and can resume after an interruption;
  • SFTP: for interactive file management and use with graphical programs (FileZilla, WinSCP, Cyberduck).

All three tools use the same username, password or SSH key as for SSH login (see: How do I connect to my VPS via SSH?). On Linux, macOS and Windows 10/11 (PowerShell), the scp and sftp commands are available by default.

scp

# Upload a file to the server
scp mentes.sql janos@203.0.113.10:/home/janos/

# Download a file from the server to the current directory
scp janos@203.0.113.10:/var/log/nginx/error.log .

# Upload a whole folder
scp -r weboldal/ janos@203.0.113.10:/var/www/

# Non-default SSH port (capital P!)
scp -P 2255 mentes.sql janos@203.0.113.10:/home/janos/

rsync

rsync must be installed on both machines (sudo dnf install rsync or sudo apt install rsync).

rsync -avz --progress weboldal/ janos@203.0.113.10:/var/www/weboldal/
  • -a (archive): copies including subdirectories, and preserves permissions, the owner and the modification time,
  • -v: lists the copied files,
  • -z: compresses data during transmission (useful on slow connections, can be omitted on fast networks),
  • --progress: shows the progress.

If you run the command again, it will only copy the files that have changed, so an interrupted copy operation can easily be resumed.

Mind the trailing slash

A slash (/) at the end of the source changes the result:

  • rsync -a weboldal/ cel/a copies the contents of the weboldal folder to the cel folder;
  • rsync -a weboldal cel/ copies the weboldal folder itself, so the result will be cel/weboldal/.

In the test (AlmaLinux 10.2), following the first run, the target files were a.php, cache and e.log; in the second run, it was a single weboldal folder.

Other useful options

# Dry run: shows what it would do, but copies nothing
rsync -avn weboldal/ janos@203.0.113.10:/var/www/weboldal/

# Non-default SSH port
rsync -avz -e "ssh -p 2255" weboldal/ janos@203.0.113.10:/var/www/weboldal/

# Exclude folders
rsync -avz --exclude 'cache/' --exclude '*.log' weboldal/ janos@203.0.113.10:/var/www/weboldal/

# Deletes the files at the destination that are no longer in the source
rsync -avz --delete weboldal/ janos@203.0.113.10:/var/www/weboldal/

Always try the --delete option first with the -n (dry run) option: if you mistype the source or destination, the files at the destination may be deleted. During a dry run, rsync displays what it would delete (deleting regi.txt in the test), the output ends with (DRY RUN), and the file remains in place.

Migration between two servers

When migrating a server, the quickest way is to run rsync directly on the new server and transfer the data from the old one, so that it does not go via your own internet connection:

# Run on the new server
rsync -avz --progress janos@regi.szerver.ip:/var/www/ /var/www/

For longer copy operations, run the command in tmux or screen so that a connection drop does not interrupt it (see: Running commands over an intermittent SSH connection).

sftp

sftp establishes an interactive connection, allowing you to browse the server’s files using commands:

sftp janos@203.0.113.10
Command Report
ls, cd, pwd Listing and changing directories on the server
lls, lcd, lpwd the same on your own computer
put fajl Upload (use put -r mappa to upload an entire folder)
get fajl download (use get -r mappa to download the entire folder)
rm, mkdir, rename Delete, create folder, rename
exit log out

For a graphical interface, use a program that supports SFTP (such as FileZilla, WinSCP or Cyberduck): select SFTP as the protocol, and enter the server address, SSH port 22 (or your own port), and your username and password or key.

Transferring a large folder in one step

Transferring lots of small files (such as a WordPress site) is quicker if you compress them into a single archive as you go:

tar -czf - weboldal/ | ssh janos@203.0.113.10 "tar -xzf - -C /var/www/"

For more information on using tar: creating and extracting tar.gz archives.


Was this answer helpful?

« Back