Useful Features of Rsync Command in Linux
- 2022年3月08日
- 技術情報
Today, I would like to share about usefulness of rsync command in Linux and how to back up data with cron job. Let’s take a look.
Rsync (Remote Sync) is the most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. Data can be copied, synchronized and backup both remotely and locally across directories, disks & networks.
Advantages of Rsync Command
- It efficiently copies and sync files from source to destination remotely or locally.
- It supports copying links, devices, owners, groups and permissions.
- rsync allows to transfer just the differences between source and destination. The first time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed files/data to the destination.
- Due to using compression and decompression method while transferring data, rsync consumes less bandwidth.
Installing Rsync
rsync is preinstalled on most Linux distributions and mac-OS. If rsync hasn’t been installed on the system, it can be installed easily using your distribution’s package manager as follows.
$ sudo apt-get install rsync [On Debian/Ubuntu & Mint]
$ pacman -S rsync [On Arch Linux]
$ emerge sys-apps/rsync [On Gentoo]
$ sudo dnf install rsync [On Fedora/CentOS/RHEL and Rocky Linux/AlmaLinux]
$ sudo zypper install rsync [On openSUSE]
Rsync Basic Command Syntax
Local to Local
rsync [OPTION] [SRC] [DEST]
Local to Remote
rsync [OPTION] [SRC] [USER@]HOST:DEST
Remote to Local
rsync [OPTION] [USER@]HOST:SRC [DEST]
Common options[OPTION] used with rsync commands
-v : verbose
-r : copies data recursively (but don’t preserve timestamps and permission while transferring data.
-a : archive mode, which allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships, and timestamps.
-z : compress file data.
-h : human-readable, output numbers in a human-readable format.
For more options, the following command can be used.
$ rsync -h
For example, to backup data from local to local(external hard drive), the following command is commonly used.
rsync -azv /home/username/projects /media/username/SPHardDrive/BackupFolder
Schedule backups
To enable scheduled backups, cron can be used for the rsync commands.
Conclusion
This is all how to copy, synchronize and backup files & directories. And I recommend the link to learn more about rsync.
Hope you enjoy that.
By Asahi
waithaw at 2022年03月08日 10:00:00