The Ultimate Guide to rsync on Ubuntu: File Sync & Backu

目次

1. Introduction

What is rsync?

rsync is a command-line tool for fast and efficient synchronization and copying of files and directories. Widely used on Linux and Unix-like systems, it utilizes a differential transfer mechanism, allowing for efficient synchronization of even large volumes of files without unnecessary data transfer. It is often included by default in Linux distributions like Ubuntu, making it an essential tool for server administrators and developers.

Benefits of using rsync on Ubuntu

On Ubuntu, rsync is highly effective for routine file backups and synchronizing multiple directories. Without relying on graphical interfaces, you can achieve high-speed file transfers, whether local or remote, by simply entering a few commands in the terminal.

It is particularly useful in the following scenarios:

  • Regular backups to external HDDs or NAS
  • Synchronization of project folders during development
  • Deployment tasks to remote servers
  • Improving availability through file mirroring

Differences from other commands

Compared to the cp command, rsync transfers only the differences between the source and destination, significantly reducing processing time. Compared to scp, it offers advantages such as the ability to resume after errors and flexible bandwidth control settings. Additionally, a major benefit is its ability to transfer files while preserving their attributes (owner, permissions, timestamps, etc.).

Purpose of this article

This article will provide a detailed explanation of how to install and effectively use rsync in an Ubuntu environment, including practical command examples and use cases. We will cover GUI tools and troubleshooting tips to help beginners get started easily, learning the practical usage of rsync step by step.

侍エンジニア塾

2. How to Install rsync (Ubuntu)

Check if rsync is pre-installed on Ubuntu

In most Ubuntu environments, rsync is typically installed by default. First, let’s check if rsync is already available using the following command.

rsync --version

If this command runs and displays version information, it is already installed and ready to use.

Steps if rsync is not installed

If you see messages like “command not found” or “rsync: command not found,” follow these steps to install it.

  1. Update the package information:
sudo apt update
  1. Install rsync:
sudo apt install rsync
  1. After the installation is complete, check the version again:
rsync --version

If the version information is displayed without issues, the installation was successful.

Is installation via snap or other methods necessary?

For Ubuntu, installing rsync via APT is the most recommended method. While a snap package exists, the APT version is sufficient for typical use and offers better stability and compatibility. Unless you have a specific reason, choose installation using APT.

Notes during installation

  • Older Ubuntu versions may require repository updates.
  • Remember to use sudo as root privileges are needed.
  • In environments with unstable network connections, installation errors may occur. It is recommended to perform the installation on a stable network.

3. Basic Usage of rsync

Basic rsync syntax

The rsync command syntax is simple yet very flexible. The basic form is as follows:

rsync [options] source destination

For example, to copy the contents of the /home/user/documents/ directory to /mnt/backup/, you would write:

rsync -av /home/user/documents/ /mnt/backup/

In this command, the following options are used:

  • -a: Archive mode (preserves directory structure, permissions, ownership, etc.)
  • -v: Verbose output (shows which files are being copied)

Caution with trailing slash in directory paths

In rsync, the behavior changes depending on whether a trailing slash is included in the source path.

  • /home/user/documents/ (with slash)
    → Only the contents are copied (e.g., file1.txt, file2.txt, etc.)
  • /home/user/documents (without slash)
    → The documents folder itself is copied (result: /mnt/backup/documents/file1.txt)

This difference is a key point unique to rsync. Use it according to your needs.

List of frequently used options

Rsync has many options, but the following are commonly used for basic operations:

OptionDescription
-aArchive mode (recursive, preserves attributes)
-vDisplay detailed transfer information
-nPerform a trial run with no changes (dry run)
--progressShow copy progress
--deleteDelete files in the destination that do not exist in the source

Want to see what will happen before execution?

To check which files will be processed before actually transferring them, use the -n or --dry-run option.

rsync -av --dry-run /home/user/documents/ /mnt/backup/

This allows you to preview “what will happen” beforehand, helping to prevent accidental data loss.

Handling of hidden files

Rsync automatically includes hidden files (starting with a dot .) in the transfer by default. This allows for easy synchronization of configuration files and environmental information.

4. Synchronization with Remote Servers

Remote synchronization using rsync via SSH

One of rsync’s major advantages is its ability to synchronize files with remote servers via SSH. This allows for data transfer over the network while maintaining a secure connection.

The basic syntax is as follows:

rsync -avz source user@remote_IP_or_hostname:destination_path

For example, to synchronize the local /home/user/documents/ directory to /var/www/backup/ on a remote server, you would use:

rsync -avz /home/user/documents/ user@example.com:/var/www/backup/
  • The -z option compresses data during transfer, which is effective when bandwidth is limited.
  • The user@host part specifies the SSH username and hostname of the destination.

First connection and setting up key authentication

When connecting to a remote destination for the first time, you may be prompted to confirm the host authenticity. If it’s safe to proceed, type “yes”.

For frequent rsync executions, it is common practice to set up SSH key authentication to avoid typing passwords.

  1. Generate SSH keys locally (if you don’t have them):
ssh-keygen -t rsa
  1. Copy the public key to the remote server:
ssh-copy-id user@example.com

After this, rsync can be executed without a password.

When specifying a port number is necessary

If the remote server is using a non-default SSH port (other than 22), you can explicitly specify it using the -e option.

rsync -avz -e "ssh -p 2222" /home/user/documents/ user@example.com:/var/www/backup/

Caution with bidirectional synchronization

Rsync is a unidirectional synchronization tool and only synchronizes from “source → destination”. If you need bidirectional synchronization, rsync alone is not sufficient, and you should consider using a different tool (e.g., unison).

5. Useful rsync Options and Usage Examples

Useful options beyond basic rsync usage

In addition to basic copy and synchronization functions, rsync offers various useful options. Here are some advanced options that are helpful in practical use.

–delete: Delete files not present in the source

This option allows you to automatically delete files in the destination that do not exist in the source. This is very useful when you want to perform mirroring (a complete replica).

rsync -av --delete /home/user/source/ /mnt/backup/

※ Since there is a risk of accidentally deleting necessary files, it is recommended to check with --dry-run beforehand.

–exclude: Exclude specific files or directories

To exclude certain files or directories from synchronization, use --exclude.

rsync -av --exclude '*.log' --exclude 'node_modules' /project/ /backup/

You can combine multiple patterns for flexible exclusion settings.

–bwlimit: Bandwidth limit

If you want to run rsync in the background without impacting other network traffic, the --bwlimit option is effective for limiting transfer speed.

rsync -av --bwlimit=5000 /data/ user@remote:/data/

(This example limits the speed to a maximum of 5MB/s)

Practical usage examples

rsync for backup purposes

Rsync can also be used as a simple and high-performance backup tool. Below is a command you can use for daily backups to an external HDD.

rsync -a --delete /home/user/ /media/usb/backup/

Here, --delete is used together to achieve full mirroring.

Automated execution combined with cron

By combining rsync with cron, you can automate regular synchronization. For example, to take a backup every night at 2 AM, edit your crontab as follows:

0 2 * * * rsync -a /home/user/ /mnt/backup/ >> /var/log/rsync.log 2>&1

This enables unattended automatic synchronization, significantly improving efficiency for tasks and server operations.

rsync enables both “flexibility” and “efficiency”

The appeal of rsync lies in its ability to achieve both “flexibility to adapt to any purpose” and “high transfer efficiency” thanks to its rich set of options. As a method for safely and quickly performing synchronization as intended, rather than just a simple copy, rsync is a very powerful choice.

6. Introduction to the GUI tool “Grsync”

Recommended for those who are not comfortable with the command line

rsync is a very powerful tool, but its operation primarily relies on command input using the terminal. For beginners unfamiliar with Linux or those who prefer to configure and use tools visually, this might feel daunting.

This is where Grsync comes in handy. Grsync is a GUI tool that uses rsync internally while providing a graphical interface for configuration and execution.

How to install Grsync (Ubuntu)

On Ubuntu, you can easily install Grsync using APT.

sudo apt update
sudo apt install grsync

After installation, “Grsync” will appear in your application list, and clicking it will launch the GUI.

Basic usage of Grsync

The Grsync interface is intuitive and allows you to configure items such as:

  • Enter source and destination paths
  • Checkboxes corresponding to major rsync options like “Recursive copy”, “Preserve attributes”, and “Delete option”
  • Specify exclusion patterns
  • Dry run execution button

After configuring, clicking the “Execute” button will run rsync in the background and perform the file synchronization.

Easy periodic tasks with the Profile feature

Grsync allows you to save your configuration settings as “Profiles”. This is very convenient for managing multiple backup settings or synchronization destinations.

For example:

  • Profile 1: Backup to USB drive
  • Profile 2: Sync with NAS
  • Profile 3: Mirroring to an external server

You can easily switch between these uses directly within the GUI.

Pros and cons of using Grsync

ProsCons
Intuitive operation without commandsSome advanced options may not be configurable through the GUI
Configure while confirming the meaning of optionsVisualization of process details is somewhat abstract and may be insufficient for those accustomed to rsync’s output
Easy to start safely (e.g., dry run with a button)Less flexible in some situations compared to using the terminal

7. Common Troubles and Solutions

File permissions are not preserved

Symptom: The owner or permissions of files in the destination change.
Cause: rsync is being run with normal user privileges or the -a (archive) option is not used.
Solution:

  • Explicitly specify the -a option.
  • Run rsync with sudo when necessary.
sudo rsync -a /source/ /destination/

Symbolic links are ignored

Symptom: Link files are not copied, or they are copied as actual files.
Cause: By default, symbolic links are copied as is, but the behavior differs depending on the options.
Solution:

  • To copy links as they are: Use -a.
  • To copy the target file itself: Add --copy-links.

“Permission denied” is displayed

Symptom: Synchronization fails with a “Permission denied” error for specific files or directories.
Cause: Insufficient access permissions.
Solution:

  • Run rsync with sudo.
  • Check and correct access permissions for the source or destination directories.
sudo rsync -a /source/ /destination/

“rsync: connection unexpectedly closed” is displayed (during remote connection)

Symptom: Synchronization is interrupted by an error during connection to the remote server.
Cause: SSH configuration issues, incorrect path specification, remote permissions settings, etc.
Solution:

  • Verify the SSH connection alone: Check if ssh user@host is successful.
  • Double-check if the destination path is correct.
  • Consider firewall or port issues.

“Argument list too long”

Symptom: An error occurs when trying to synchronize a directory containing a very large number of files.
Cause: Exceeding the limit on the length of command-line arguments.
Solution:

  • Narrow down the targets using --exclude or --include.
  • Synchronize in multiple steps, directory by directory.

8. FAQ (Frequently Asked Questions)

Here we have compiled common questions and issues users encounter when using rsync on Ubuntu. We will provide clear explanations focusing on points where beginners often get stuck.

Q1. How can I preserve file ownership and permissions with rsync?

A1. By using the -a option (archive mode), you can copy files while preserving their permissions, timestamps, owner, group, and more. This option is essential for tasks like backing up system configurations.

rsync -a /etc/ /backup/etc/

Q2. How do I exclude specific files or folders from synchronization?

A2. You can exclude specific files or patterns from the synchronization target using the --exclude option.

rsync -av --exclude '*.log' /project/ /backup/project/

Wildcards can also be used, making it easy to exclude directories like node_modules or .git.

Q3. How can I limit the network bandwidth with rsync?

A3. The --bwlimit option allows you to limit the transfer speed. The unit is KB/s.

rsync -av --bwlimit=5000 /data/ user@remote:/data/

In this example, the transfer speed is limited to a maximum of 5MB/s. This is useful when you don’t want to saturate the network connection.

Q4. I want to see what will happen before execution. How?

A4. Using the --dry-run (or -n) option simulates the transfer without actually moving any files. It shows you exactly what would happen.

rsync -av --dry-run /source/ /destination/

This verification step is extremely important when using rsync for the first time or performing operations that include --delete.

Q5. Are hidden files (dot files) copied?

A5. Yes, rsync includes files starting with a dot (.) in the copy target by default. However, if you wish to exclude specific hidden files, you will need to add an exclusion rule like --exclude '.*'.

Q6. What is the difference between rsync and scp?

A6. scp is a tool focused on simple file transfer and transfers all files every time. rsync is capable of differential transfer, efficiently copying only the parts that have changed, making it more suitable for backups and mirroring. It is also more robust in resuming interrupted transfers.

9. Conclusion

In this article, we covered a wide range of topics regarding rsync on Ubuntu, from installation methods and basic usage to synchronization with remote servers, useful options, utilizing the GUI tool “Grsync”, troubleshooting, and FAQs.

Recap of rsync’s appeal

  • Fast differential transfer prevents unnecessary data transfer.
  • Allows for backups and mirroring while preserving permissions and ownership.
  • Supports remote synchronization and can be used securely in conjunction with SSH.
  • Rich options enable fine-grained control for various situations, such as exclusion settings and bandwidth limits.
  • Can be integrated with cron to achieve automation and periodic execution.
  • The GUI tool Grsync makes it easy for beginners to operate with confidence.

rsync is an “essential tool for Linux users”

In Linux environments including Ubuntu, rsync is a fundamental tool for file operations, backups, and synchronization. Even for beginners, practicing the content introduced in this article should enable you to utilize rsync safely and efficiently.

Furthermore, while rsync has a simple syntax, mastering it allows for professional-level file management. Start by grasping the basics, and as needed, incorporate advanced options to find the optimal way to use rsync for your own environment.

 

侍エンジニア塾