Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux rsync Command: Local Backup and Remote Backup

The rsync command is a powerful tool for synchronizing files and directories between two locations in Linux. It's efficient because it only transfers changes rather than entire files, minimizing the amount of data that needs to be transferred. In this tutorial, we'll cover the basics of the rsync command and provide examples to help you understand its usage.

  • Basic syntax:

The basic syntax for the rsync command is:

rsync [OPTIONS] SOURCE DESTINATION
  • OPTIONS: Flags or options used to control the behavior of the command.
  • SOURCE: The source file or directory.
  • DESTINATION: The destination file or directory.
  • Common options:
  • -a, --archive: Archive mode; equal to -rlptgoD. It enables recursion, preserves symbolic links, permissions, timestamps, group, and owner information.
  • -v, --verbose: Increase verbosity.
  • -z, --compress: Compress file data during the transfer.
  • -n, --dry-run: Perform a trial run with no changes made.
  • -h, --human-readable: Output numbers in a human-readable format.
  • -P: Equivalent to --partial --progress. Show progress during the transfer and keep partially transferred files.
  • --delete: Delete extraneous files from the destination directory.
  • -e, --rsh=COMMAND: Specify the remote shell to use. Example: -e "ssh -p 2222".
  • Examples:

a) Synchronize local directories:

To synchronize the contents of a local source directory (/source/dir) with a local destination directory (/destination/dir), run:

rsync -av /source/dir/ /destination/dir

Note the use of a trailing slash / after the source directory. It ensures that only the contents are copied, not the source directory itself.

b) Synchronize remote directories:

To synchronize the contents of a remote source directory with a local destination directory, run:

rsync -av user@remote-host:/source/dir/ /destination/dir

To synchronize the contents of a local source directory with a remote destination directory, run:

rsync -av /source/dir/ user@remote-host:/destination/dir

c) Synchronize with a specific remote port:

To synchronize files using a specific remote port (e.g., 2222), use the -e option with the ssh command:

rsync -av -e "ssh -p 2222" /source/dir/ user@remote-host:/destination/dir

d) Synchronize with progress and partial transfers:

To show the progress during the transfer and keep partially transferred files, use the -P option:

rsync -avP /source/dir/ /destination/dir

e) Perform a dry run:

To see the changes that would be made without actually transferring any files, use the -n option:

rsync -avn /source/dir/ /destination/dir

The rsync command is a versatile and efficient way to synchronize files and directories in Linux. You can use it for local or remote transfers and take advantage of its many options to customize its behavior. For more information and options, refer to the man page by running man rsync or visiting the online documentation.

  1. How to use rsync for local backups in Linux: rsync is a powerful tool for synchronizing files locally. For a basic local backup:

    rsync -av source_directory/ destination_directory/
    
  2. Creating local backups with rsync on Unix-like systems: Use rsync to copy files locally with options like -a for archive mode and -v for verbose output. Example:

    rsync -av /path/to/source/ /backup/destination/
    
  3. Setting up scheduled local backups using rsync: Automate backups using cron to schedule rsync commands. Edit the crontab with crontab -e and add an entry like:

    0 2 * * * rsync -av /path/to/source/ /backup/destination/
    
  4. rsync for remote backups in Linux: rsync can synchronize files between local and remote systems. For example:

    rsync -av /local/source/ user@remote:/remote/destination/
    
  5. Securing remote backups with SSH and rsync: Use SSH for secure remote backups with rsync. Example:

    rsync -av -e ssh /local/source/ user@remote:/remote/destination/
    
  6. Incremental backups with rsync on Linux: Perform incremental backups by using the --link-dest option to create hard links for unchanged files. Example:

    rsync -av --link-dest=/previous/backup/ /path/to/source/ /new/backup/