Rsync to copy Files Between Servers and Computers

I found rsync when I wanted to use scp to copy files to a server but not overwrite files already there. Rsync is actually more efficient no matter what (it seems) but it is really great if there are a bunch of duplicate files (Rsync will just skip them).

To copy files from your current computer to a server:

rsync -azv -r ssh [directory] [username]@remotehost:[location]

[bash]$ sudo rsync -azv -e ssh directory-on-my-computer/ [email protected]:~/files/go/here[/bash]

a = archive mode
z = compress file data
v = verbose
r = recurse through subdirectories and copy all of them

Compressing file data saves bandwidth so if that is an issue it is another big win over scp. And in my reading it seems rsync can restart a broken file transfer in process (while scp you have to redo the whole file transfer).

To copy from the server to your computer just reverse the order of the locations. And you can even just put in two addresses not on your current computer and copy between then.

As a reminder, I realize this blog is made up of stuff that is obvious to a large number of people. It is really aimed at me (so I can quickly find what I found before), and to a lessor extent others like me (who use cli some but are not system administrators or programmers to any significant extent).

Related: Making Sure You Donโ€™t Run Out of Space on Your VPSBash Profile Adjustments (Scrolling History)Customizing the Command Prompt for Terminal in Ubuntu

Using scp (secure copy) to Copy Files Between Computers

Copy a file from your local computer to a remote host using secure copy, scp (which uses ssh for data transfer and provides the same security as using ssh).

scp [filename] [username]@remotehost:[location]

[bash]scp file_to_copy.txt [email protected]:/some/remote/directory[/bash]

copy a directory to your home computer from the remote computer.

[bash]scp -r directory_to_copy [email protected]:[/bash]

copy a directory from a remote server to the current directory on your computer.

scp -r folder [username]@remotehost:[location] .

[bash]scp -r [email protected]:/some/remote/directory .[/bash]

If you don’t have automated keys setup you will be asked for the password for that user.

An example for copying a MySQL database. Including the : without a location puts the file in the home folder.

[bash]mysqldump database_name -uroot > database_dump.sql
scp database_dump.sql [email protected]:[/bash]

Then ssh into the remote server and open the mysql prompt
[bash]mysql -uroot -p
mysql> create database database_name;
mysql> exit[/bash]

Then run the mysqldump file

[bash]mysql database_name -uuser -p < database_dump.sql[/bash]

Remember to create the database user on the new machine (this has to match what is in the wp-config.php file).