Managing files in Linux often requires more than simple copying or secure copying. This is where the rsync command comes in, unlike the cp command, rsync is designed for synchronisation and complex transfer.
Mostly utilised for backup tasks that might require mirror directories or transferring between local and remote systems. In this guide you’ll learn how to use rsync hopefully making your job as a linux administrator easier.
It’s perfect for tasks like backups, mirroring directories, or transferring files between local and remote systems. In this guide, you’ll learn how to use rsync effectively, whether you’re copying files locally or across servers.
To copy files or directories using rsync, you specify the source and destination paths and include the -r (recursive) flag. Using this command will copy the entire Documents directory into the Backup directory but will only transfer updated or new files saving time and server resources over transferring the entire contents of the directory.
rsync -r /home/cpusername/public_html/ /home/cpusername/backup/
When you want to preserve (retain) file attributes (permissions, timestamps, symlinks, etc) the -a (archive) flag is more comprehensive than possible with copy. This is especially useful for accurate backups or migration tasks, ensuring copied files match the source exactly .
rsync -a /home/cpusername/public_html/ /home/cpusername/backup/
For large file transfers, it’s helpful to track the progress in real time. By using the -P flag combines “–progress” and “–partial“, you can see detailed transfer stats. Providing a clear visual transfer status, including completion percentage and estimated time remaining.
rsync -aP /home/cpusername/public_html/ /home/cpusername/backup/
vs
rsync -a --partial--progress /home/cpusername/public_html/ /home/cpusername/backup/
A standout feature of rsync is its ability to copy files over SSH. This eliminates the need for manual uploads via FTP and allows a more seamless file synchronisation between systems over SCP, in the example below you would replace sshusername and remoteaddress with the corresponding details.
rsync -a /home/cpusername/public_html/ sshusername@remoteaddress:/home/cpusername/backup/
You can see how using rsync might be more beneficial over SCP in the command below, as this will:
rsync -azP -e "ssh -p 9248" /home/cpusername/public_html/ root@192.168.1.1:/home/cpusername/backup/