RSYNC

  • Synchronize files between multiple systems

  • Can perform one-time copies

  • Can be used to setup scheduled synchronizations

  • Operations can be one-way or bidirectional

  • General usage

    • rsync <options> <source> <destination>

Backup a folder to another location

rsync -azurP ./Documents/ ./Backup/

-a Archive (copy attributes) -z Compress with gzip -u Skip files that are already there -r Recursive -P Display progress

Output:

touk@k8snode:~$ rsync -azurP ./Documents/ ./Backup/
sending incremental file list
created directory ./Backup
./
apache-deployment.yml
            332 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/7)
deployment-file.yml
            347 100%   28.24kB/s    0:00:00 (xfr#2, to-chk=4/7)
helloworld-pod.yml
            139 100%    7.14kB/s    0:00:00 (xfr#3, to-chk=3/7)
nginx-deployment.yml
            321 100%    3.87kB/s    0:00:00 (xfr#4, to-chk=2/7)
service.yml
            160 100%    1.74kB/s    0:00:00 (xfr#5, to-chk=1/7)
webserver.yml
            214 100%    2.15kB/s    0:00:00 (xfr#6, to-chk=0/7)

Remote Backup

rsync Listener

  • Requires rsync running on the remote host

  • TCP port 873

  • Fairly dated, not secure

    rsync -azurP ./Documents touk@192.168.1.12:/home/touk/Backup

    Now, if run this and make sure the port 873 is open, it’s going to wok but this is not secure

Another way to do this is by telling rsync to use ssh tunnel, and you won’t need port 873 to be open

rsync -azurP -e ssh ./Documents touk@192.168.1.12:/home/touk/Backup

NOW, BIG NOTE!!

You are going to be prompted for a password if you didn’t set up a certificate between the machines, so do that first

how to?

  1. On your local machine, generate an SSH keypair (in case you don’t have one)

ssh-keygen -t rsa

  1. Copy the public key to the remote server. This appends it to the authorized_keys file:

ssh-copy-id touk@192.168.1.12

  1. Try to SSH and you won’t be asked for password

Output on local machine:

touk@k8snode:~$ rsync -azurP -e ssh ./Documents touk@192.168.1.12:/home/touk/Backup
sending incremental file list
Documents/
Documents/apache-deployment.yml
            332 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/7)
Documents/deployment-file.yml
            347 100%  338.87kB/s    0:00:00 (xfr#2, to-chk=4/7)
Documents/helloworld-pod.yml
            139 100%  135.74kB/s    0:00:00 (xfr#3, to-chk=3/7)
Documents/nginx-deployment.yml
            321 100%  156.74kB/s    0:00:00 (xfr#4, to-chk=2/7)
Documents/service.yml
            160 100%   78.12kB/s    0:00:00 (xfr#5, to-chk=1/7)
Documents/webserver.yml
            214 100%  104.49kB/s    0:00:00 (xfr#6, to-chk=0/7)

  • Output on Remote Machine:

    touk@ubuntu:~$ ls ./Backup
    apache-deployment.yml  deployment-file.yml  Documents  helloworld-pod.yml  nginx-deployment.yml  service.yml  webserver.yml

  • Transfer all except:

    rsync -azurP -e ssh --exclude="*.yml" ./Documents touk@192.168.1.12:/home/touk/Backup

  • Perform a test run:

    rsync -azurP -e ssh --exclude="*.yml" --dry-run ./Documents touk@192.168.1.12:/home/touk/Backup

Does not actually copy/delete files

Preserving permissions

Requires root

-ogA

-o Owner

-g Group

-p Permissions

-A ACLs (implies -p)

Last updated