rsync cheat
1 rsync
A remote file copy tool. Very flexible.
-v
verbose
rsync
uses ssh
beneath the surface so you must have ssh
set up including
having copied the ssh-copy-id
keys to the remote. Also make sure you have set
up the ssh-keygen
keys without entering a password.
1.1 See also rsnapshot
rsnapshot
is a filesystem snapshot utility that uses rsync behind the scenes.
Typically installed together, so
apk add rsnapshot rsync
# Alpine linuxpacman -S rsnapshot rsync
# Arch linux, Manjaroapt install rsnapshot rsync
# Debian, Ubuntu, Mint PopOS linuxdnf install rsnapshot rsync
# Redhat linux based distrosdnf install epel-release
# if not already installed, as rsync is on epel
2 rsync syntax
2.1 rsync [options] source [destination]
rsync -vz from to
rsync -vz fromdirectory todirectory
rsync -vz source destination
if [destination] is omitted, rsync acts like ls -l
on the source.
The source or destination may be local or remote, i.e. rsync works both ways. So source or destination can be:
user@10.1.1.1:/home/zintis/folder
The :
means use ssh or rsh as the transport. The other transport option is a
direct TCP connection, but that has to be set up.
If the user has shared public keys to remote host, you can skip the user@
and skip the password too, as ssh will handle the authentication
.
Then, the source or destination can be just 10.1.1.1:/home/zintis/folder
if source and destination are both local
, then rsync
acts like cp
command.
Remember that if you are copying from a remote host, you need to copy the local host's public ssh key to the remote host. (Not the other way around).
2.2 rsync as root user (i.e. at system level)
From the C8host (.111.1)
mkdir -p /backup/incremental/vm1 mkdir -p /backup/incremental/vm2 mkdir -p /backup/incremental/vm3 rsync -avz 192.168.111.11:/etc /backup/incremental/vm1 rsync -avz 192.168.111.12:/etc /backup/incremental/vm2 rsync -avz 192.168.111.13:/etc /backup/incremental/vm3
2.3 rsync options
-a
archive files (i.e. copy everything of importance, recusively, preserving almost everything, symbolic links, file permissions and user and group ownership. -only thing skipped is hardlinks, because finding multiply- linked files is expensive. (use -H option to include hard links)-v
verbose (by default rsync works silently.) -v will tell you what is being copied.-h
human readable verboseness-z
compress-c
decide to copy based on checksum, not the default mod-time and size
single colon separator after the hostname indicates a remote shell, such
as ssh, can be used as the transport
/etc the remote hosts directory as the copy source /back/incr…. the local directory as the copy target
man rsync - a plethora of choices and tweaks…
2.4 Changing permissions of files during the rsync copy
You can use chown and chmod during the rsync. For example
rsync -avhe ssh --chown=USER:GROUP /foo user@remote-host:/tmp
But, USER and GROUP must already be created in the target host.
2.5 Ignoring existing files (i.e. don't over-write files)
Example: rsync –ignore-existing -avhe /foo user@remote-host:/tmp