- To copy a source directory to a destination, use the following command:
$ rsync -av source_path destination_path
Consider this example:
$ rsync -av /home/slynux/data
slynux@192.168.0.6:/home/backups/data
In the preceding command:
- -a stands for archiving
- -v (verbose) prints the details or progress on stdout
The preceding command will recursively copy all the files from the source path to the destination path. The source and destination paths can be either remote or local.
- To backup data to a remote server or host, use the following command:
$ rsync -av source_dir username@host:PATH
To keep a mirror at the destination, run the same rsync command at regular intervals. It will copy only changed files to the destination.
- To restore the data from the remote host to localhost, use the following command:
$ rsync -av username@host:PATH destination
The rsync command uses SSH to connect to the remote machine hence, you should provide the remote machine's address in the user@host format, where user is the username and host is the IP address or host name attached to the remote machine. PATH is the path on the remote machine from where the data needs to be copied.
Make sure that the OpenSSH server is installed and running on the remote machine. Additionally, to avoid being prompted for a password for the remote machine, refer to the Password-less auto-login with SSH recipe in Chapter 8, The Old-Boy Network.
Make sure that the OpenSSH server is installed and running on the remote machine. Additionally, to avoid being prompted for a password for the remote machine, refer to the Password-less auto-login with SSH recipe in Chapter 8, The Old-Boy Network.
- Compressing data during transfer can significantly optimize the speed of the transfer. The rsync-z option specifies compressing data during transfer:
$ rsync -avz source destination
- To synchronize one directory to another directory, use the following command:
$ rsync -av /home/test/ /home/backups
The preceding command copies the source (/home/test) to an existing folder called backups.
- To copy a full directory inside another directory, use the following command:
$ rsync -av /home/test /home/backups
This command copies the source (/home/test) to a directory named backups by creating that directory.
For the PATH format, if we use / at the end of the source, rsync will copy the contents of the end directory specified in the source_path to the destination.
If / is not present at the end of the source, rsync will copy the end directory itself to the destination.
Adding the -r option will force rsync to copy all the contents of a directory, recursively.
If / is not present at the end of the source, rsync will copy the end directory itself to the destination.
Adding the -r option will force rsync to copy all the contents of a directory, recursively.