Guide to copying data using Rsync in Linux
Rsync — is a powerful tool for synchronizing and copying files in Linux. It allows you to quickly transfer data between local directories or remote servers, preserving structure, permissions and timestamps. In this article, we'll walk you through how to use rsync for efficient copying: from basic commands to advanced settings. You will learn how to avoid errors, speed up the process and protect your data.Advantages of rsync:
- Efficiency: copies only the differences;
- Flexible: supports local and remote operations via SSH;
- Reliability: preserves rights, owners and references.
Installing rsync
Most modern Linux distributions already have the rsync utility installed. Check to see if it is installed:rsync --version
If the command outputs a version (e.g., 3.2.7), skip to the next section. If not, install it.- Ubuntu/Debian:
apt install rsync
- CentOS/RHEL:
yum install rsync
- Arch Linux:
pacman -S rsync
rsync --version
Basic copying of local data
To copy a local directory, use:rsync -av /source_directory/ /target_directory/
- -a (archive) — preserves rights, owners, links and structure;
- -v (verbose) — shows the copying process.
Example: copying /home/user/docs to /backup:
rsync -av /home/user/docs/ /backup/
Make sure the files are copied:
ls -l /backup
To update the copy, repeat the command - rsync will only transfer the changes.Copying to remote server
Copying from local to remote server
To transfer data to the server:rsync -avz /source_directory/ user@server_address:/target_directory/
Example: copying /home/user/docs to the server:
rsync -avz /home/user/docs/ [email protected]:/home/user/backup/
Copying from remote server to local server
For the reverse operation:rsync -avz user@server_address:/source_directory/ /target_directory/
- -z (compress) - compresses file data during transfer to save traffic when copying.
Example: downloading /var/www from the server:
rsync -avz [email protected]:/var/www/ /home/user/www_backup/
Working with non-standard port
If the server uses a non-standard SSH port (for example, 2222), specify it with the -e option:rsync -av -e "ssh -p 2222" /source_directory/ user@server_address:/target_directory/
Example:
rsync -av -e "ssh -p 2222" /home/user/docs/ [email protected]:/home/user/backup/
Note: Make sure the port is open in the firewall settings for example:
ufw allow 2222
Advanced rsync features
File exception
To skip certain files or directories, use --exclude:rsync -av --exclude '*.log' /source_directory/ /target_directory/
Example: exclusion of logs and temporary files:
rsync -av --exclude '*.log' --exclude 'temp/' /home/user/docs/ /backup/
Data compression
To save traffic on remote copying, add -z (compress):rsync -avz /source_directory/ user@server_address:/target_directory/
Deleting unnecessary files
To synchronize directories with delete files that are not in the source, use --delete:rsync -av --delete /source_directory/ /target_directory/
Note: Use --delete with caution - deleted files cannot be recovered without a backup.Typical errors and their elimination
Error: rsync: command not foundReason: The utility is not installed. Install it, e.g. for Debian:
apt install rsync
Error: Permission denied
Cause: insufficient permissions. Run with sudo or check permissions:
chmod 755 /directory
Error: Connection refused
Cause: SSH server is unavailable. Check the connection:
ssh user@server_address
Copy automation with rsync
Set up automatic copying via cron. Open the editor:crontab -e
Add a task (for example, copying every day at 2:00):
0 2 * * * rsync -av /home/user/docs/ /backup/
Useful tips
- Test copying in test mode with --dry-run: rsync -av --dry-run /source/ /destination/
- Use --progress to display progress: rsync -av --progress /source/ /destination/
Conclusion
Rsync is a versatile tool for copying and synchronizing data. We've covered its installation, basic and advanced commands, and automation. Use these recommendations to create backups or transfer data to a server. It is recommended to test the commands and analyze the result - this will help you master rsync in practice.07 Apr 2025, 16:04:12