How to Use rsync to Back Up, Move, or Copy Data on Linux? Print

  • Linux commands, file transfer, rsync, data backup, move files, Linux backup tool, copy files
  • 423

Introduction

The rsync command is a robust and efficient tool for synchronizing files and directories across different locations. It’s widely used for backup, data transfer, and mirroring on Linux systems. This guide provides detailed instructions for using rsync on various Linux distributions, along with tips and best practices.

Installing rsync

The rsync tool is typically pre-installed on most Linux distributions. If it’s missing, use the following commands to install it:

Ubuntu/Debian:

sudo apt update && sudo apt install rsync -y

CentOS/RHEL:

sudo yum install rsync -y

Fedora:

sudo dnf install rsync -y

Arch Linux:

sudo pacman -S rsync

Basic Usage of rsync

1. Backing Up Data Locally

To back up files from one directory to another on the same system:

rsync -av /source/directory/ /destination/directory/

Options explained:

  • -a: Archive mode (preserves symbolic links, permissions, timestamps, etc.).
  • -v: Verbose mode for detailed output.

2. Backing Up Data to a Remote Server

Use rsync over SSH to securely back up data to a remote system:

rsync -avz -e ssh /source/directory/ user@remote_host:/destination/directory/

Additional options:

  • -z: Enables compression for faster transfer.
  • -e ssh: Uses SSH for secure connection.

Advanced Usage

1. Synchronizing Directories

To synchronize two directories, ensuring only changes are copied:

rsync -av --delete /source/directory/ /destination/directory/

The --delete option removes files from the destination that no longer exist in the source.

2. Excluding Files and Directories

Exclude specific files or directories from being copied:

rsync -av --exclude 'file_or_directory' /source/ /destination/

3. Limiting Bandwidth

To limit the bandwidth used by rsync:

rsync --bwlimit=5000 -av /source/ /destination/

The --bwlimit option sets the bandwidth limit in kilobytes per second.

Troubleshooting Tips

  • Permission Denied Errors: Ensure proper file and directory permissions. Use sudo if required.
  • Connection Issues: Check SSH configuration and network connectivity.
  • Performance Problems: Use the -z option for compression or limit bandwidth with --bwlimit.

Best Practices

  • Always use the --dry-run option to preview actions before executing.
  • Automate backups with cron jobs or systemd timers.
  • Enable logging to track changes and errors.

Examples

1. Backing Up a Home Directory

rsync -av /home/user/ /backup/home/user/

2. Incremental Backup

rsync -av --link-dest=/previous/backup /current/data/ /new/backup/

3. Remote Synchronization with Specific Port

rsync -avz -e 'ssh -p 2222' /source/ user@remote_host:/destination/

Conclusion

rsync is a versatile and efficient tool for file backup, transfer, and synchronization on Linux. By mastering its options and features, you can ensure secure and reliable data management across different systems.


Was this answer helpful?

« Back