rsync command - sync directories

You can synchronize directories using the rsync command. Synchronization means putting two directories in the same state.

It also includes the ability to copy directories, so the rsync command can also be used to copy directories.

rsync will only transfer the diffs. So even in a large directory, only the changes are transferred, which is good performance.

Synchronize directories using rsync

First, I'll show you how to sync directories using rsync. This is surprisingly a songwriter.

# Copy the directory. Synchronize the contents of src_dir into dist_dir
rsync -av src_dir/ dist_dir

There are many command options in rsync, but first remember only "-a" and "-v".

The "-a" option is a combination of other options that syncs nicely.

The "-v" option prints which files were synced.

This is the most important thing, but with rsync, the meaning depends on whether the directory name has a "/" after it or not.

If you don't understand this well, you'll get "that" every time you run rsync.

In rsync, a "/" at the end of the source directory means "the contents of the directory".

For the synchronization destination, simply write the directory name.

The above means syncing the contents of src_dir into dist_dir.

If the destination directory does not exist, it will be created automatically.

If there is no "/" at the end of the directory name of the synchronization source, it means to synchronize "src_dir" in "dist_dir". "Src_dir" will be created in "dist_dir".

Synchronize directories between local and remote

rsync supports local and remote synchronization using the SSH protocol, as well as local synchronization. This can be used for backup.

Describe how to specify the user name, host name, and port number. Other than that, it is the same as local synchronization.

rsync -e "ssh -p 55555" -av /foo/src_dir/ kimoto@

Username / hostname format

Write the remote host in the following format.


Specifying the port number

Specifying the port number is not an ordinary option and should be included as an option for the ssh command with the -e option.

-e "ssh -p port number"

I want to automate backup operations

If you want to automate the backup operation, create an SSH private key / public key pair on the backup server and use it to connect. With the key, the password is not required, so backup operations can be automated.

I will explain how to create an SSH key pair elsewhere.

Associated Information