Example of rsync backup data in Linux

Posted by celestineweb on Wed, 02 Feb 2022 01:15:26 +0100

Reading guideThe rsync tool is used to synchronize files and directories from one location to another. The location of synchronization can be on a local server or a remote server.

stay Centos Use the following in command Install rsync:

[root@localhost ~]# yum -y install rsync

Example 1: synchronize two directories in this machine

To synchronize two directories on the local computer, use rsync -zvr command:

[root@localhost ~]# rsync -zvr /var/log/ /root/temp/
sending incremental file list
btmp
dnf.librepo.log
...
sssd/sssd_implicit_files.log
sssd/sssd_nss.log
tuned/tuned.log

sent 516,136 bytes  received 605 bytes  1,033,482.00 bytes/sec
total size is 5,451,242  speedup is 10.55

Parameter interpretation:

  • -z enable compression
  • -v output details
  • -r means recursion

Check the / root/temp directory and find that rsync does not retain a timestamp during synchronization.

Example 2: use rsync -a to retain the timestamp during synchronization

The - a option of the rsync command indicates archive mode- A option recursive synchronization, reserved symbolic links, reserved permissions, reserved timestamps, reserved owners and groups.

Now execute the following command and view the time of the file:

[root@localhost ~]# rsync -azv /var/log/ /root/temp/
sending incremental file list
./
btmp
dnf.librepo.log
dnf.log
dnf.rpm.log
...
sssd/sssd_nss.log
tuned/
tuned/tuned.log

sent 516,231 bytes  received 629 bytes  1,033,720.00 bytes/sec
total size is 5,451,789  speedup is 10.55

As shown below, rsync retains the timestamp during synchronization.

Example 3: synchronize files from local to remote directory

rsync allows you to synchronize files / directories between local and remote systems, provided that rsync is installed on both local and remote systems, otherwise the following information will be prompted:

[root@localhost ~]# rsync -avz /root/temp/ root@192.168.43.137:/root/temp
root@192.168.43.137's password: 
sending incremental file list
created directory /root/temp
./
btmp
dnf.librepo.log
dnf.log
dnf.rpm.log
...
sssd/sssd_nss.log
tuned/
tuned/tuned.log

sent 516,231 bytes  received 662 bytes  206,757.20 bytes/sec
total size is 5,451,789  speedup is 10.55


The following is to view the synchronized directory in the remote system:

As you can see above, you need to enter a password when synchronizing. Sometimes you don't want to enter a password when backing up files from the local server to the remote server. You can set a password free login between the two hosts.

Example 4: synchronize files from remote directory to local directory

To synchronize files from the remote system to the local, specify the remote path in the source and the local path in the target as follows:

[root@localhost ~]# rsync -avz root@192.168.43.137:/root/temp /root/temp
root@192.168.43.137's password: 
receiving incremental file list
temp/
temp/btmp
temp/dnf.librepo.log
temp/dnf.log
...
temp/tuned/
temp/tuned/tuned.log

sent 634 bytes  received 516,247 bytes  206,752.40 bytes/sec
total size is 5,451,789  speedup is 10.55

Example 5: do not overwrite the modified file at the target location

If the file is modified in the destination location, we may not want to overwrite the file with the old file in the source location. You can do this with the - u option. In the following example, test. Exe is set locally Txt file modified the content. It will not be tested by the remote system Txt file overwritten by:

# Check test. In the temp directory of the remote system Txt file size
[root@localhost ~]# ssh root@192.168.43.137 ls -l /root/temp
root@192.168.43.137's password: 
total 4
-rw-r--r--. 1 root root 7 Apr  7  2021 test.txt
# Check the test.exe in the temp directory of this machine Txt file size, local test Txt file has been modified, so it is better than test.txt in the remote system Txt file large
[root@localhost ~]# ll /root/temp/
total 4
-rw-r--r--. 1 root root 77 Apr  7 21:10 test.txt
# Perform rsync -avzu synchronization
[root@localhost ~]# rsync -avzu root@192.168.43.137:/root/temp /root/
root@192.168.43.137's password: 
receiving incremental file list

sent 25 bytes  received 76 bytes  40.40 bytes/sec
total size is 7  speedup is 0.07

Let's check the test in the / root/temp directory of this machine Txt is overwritten:

Found not covered.

Example 6: viewing rsync progress during transmission

Use the -- progress option to display the detailed progress of rsync execution, as shown below:

[root@localhost ~]# rsync -avz --progress /root/temp/ root@192.168.43.137:/root/temp

Example 7: delete files in the target directory that do not exist in the source directory

If the file does not exist in the source but in the destination, you may want to delete the file on the destination during rsync synchronization. In this case, use the -- delete option:

# Check the files in the source directory
[root@localhost ~]# ll /root/temp/
total 0
-rw-r--r--. 1 root root 0 Apr  7 21:46 name.csv
# Check the files in the target directory
[root@localhost ~]# ssh root@192.168.43.137 ls -l /root/temp
root@192.168.43.137's password: 
total 944
drwxr-xr-x. 2 root root      6 Apr  7  2021 anaconda
drwx------. 2 root root      6 Apr  7  2021 audit
-rw-------. 1 root root      0 Apr  7  2021 btmp
-rw-------. 1 root root      0 Apr  7  2021 btmp-20210406
drwxr-xr-x. 2 root root      6 Apr  7  2021 chrony
-rw-------. 1 root root   8432 Apr  7  2021 cron
-rw-------. 1 root root  12200 Apr  7  2021 cron-20210221
-rw-------. 1 root root  48130 Apr  7  2021 cron-20210228
-rw-------. 1 root root   3910 Apr  7  2021 cron-20210308
-rw-------. 1 root root  22455 Apr  7  2021 cron-20210406
-rw-------. 1 root root 383369 Apr  7  2021 dnf.librepo.log
-rw-------. 1 root root 476949 Apr  7  2021 dnf.librepo.log-20210221
# rsync uses the -- delete option to delete files in the target directory that do not contain the source directory
[root@localhost ~]# rsync -avz --delete /root/temp root@192.168.43.137:/root
root@192.168.43.137's password: 
sending incremental file list
deleting temp/chrony/
deleting temp/audit/
deleting temp/anaconda/
deleting temp/dnf.librepo.log-20210221
deleting temp/dnf.librepo.log
deleting temp/cron-20210406
deleting temp/cron-20210308
deleting temp/cron-20210228
deleting temp/cron-20210221
deleting temp/cron
deleting temp/btmp-20210406
deleting temp/btmp
temp/
temp/name.csv

sent 123 bytes  received 281 bytes  161.60 bytes/sec
total size is 0  speedup is 0.00


Check whether the target directory is deleted:

Example 8: include and exclude modes during file transfer

rsync allows you to provide a mode to include and exclude files or directories during synchronization.

[root@localhost ~]# rsync -avz --include 'P*' --exclude '*' root@192.168.43.137:/var/lib/rpm/ /root/temp/


In the above example, it includes only files or directories that start with 'P' and excludes all other files.

Example 9: do not transfer large files

You can use the rsync -- max size option to tell rsync not to transfer files larger than the specified size.

[root@localhost ~]# rsync -avz --max-size='1M' root@192.168.43.137:/var/lib/rpm/ /root/temp/


--Max size = 1M causes rsync to transfer only files less than or equal to 1M. The units can be K,M,G, etc.

You can also use the -- min size = parameter to specify the minimum file size to transfer.

summary

The rsync tool is used to synchronize files and directories from one location to another. The location of synchronization can be on a local server or a remote server.