|
| 1 | +--- |
| 2 | +title: rsync demo 01 |
| 3 | +author: tianci li |
| 4 | +contributors: Steven Spencer |
| 5 | +update: 2021-11-04 |
| 6 | +--- |
| 7 | + |
| 8 | +# Preface |
| 9 | + |
| 10 | +`rsync` needs to perform user authentication before data synchronization. **There are two protocol methods for authentication: SSH protocol and rsync protocol (the default port of rsync protocol is 873)** |
| 11 | + |
| 12 | +* SSH protocol verification login method: use SSH protocol as the basis for user identity authentication (that is, use the system user and password of GNU/Linux itself for verification), and then perform data synchronization. |
| 13 | +* rsync protocol verification login method: use rsync protocol for user identity authentication (non-GNU/Linux system users, similar to vsftpd virtual users), and then perform data synchronization. |
| 14 | + |
| 15 | +Before the specific demonstration of rsync synchronization, you need to use the `rsync` command. In Rocky Linux 8, the rsync rpm package is installed by default, and the version is 3.1.3-12, as follows: |
| 16 | + |
| 17 | +```bash |
| 18 | +[root@Rocky ~ ] # rpm -qa|grep rsync |
| 19 | +rsync-3.1.3-12.el8.x86_64 |
| 20 | +``` |
| 21 | + |
| 22 | +```txt |
| 23 | +Basic format: rsync [options] original location target location |
| 24 | +Commonly used options: |
| 25 | +-a: archive mode, recursive and preserves the attributes of the file object, which is equivalent to -rlptgoD (without -H, -A, -X) |
| 26 | +-v: Display detailed information about the synchronization process |
| 27 | +-z: compress when transferring files |
| 28 | +-H: Keep hard link files |
| 29 | +-A: retain ACL permissions |
| 30 | +-X: retain chattr permissions |
| 31 | +-r: Recursive mode, including all files in the directory and subdirectories |
| 32 | +-l: still reserved for symbolic link files |
| 33 | +-p: Permission to retain file attributes |
| 34 | +-t: time to retain file attributes |
| 35 | +-g: retain the group belonging to the file attribute (only for super users) |
| 36 | +-o: retain the owner of the file attributes (only for super users) |
| 37 | +-D: Keep device files and other special files |
| 38 | +``` |
| 39 | + |
| 40 | +The author's personal use: `rsync -avz original location target location` |
| 41 | + |
| 42 | +## Environment Description |
| 43 | + |
| 44 | +|Item|Description| |
| 45 | +|---|---| |
| 46 | +| Rocky Linux 8(Server) | 192.168.100.4/24 | |
| 47 | +| Fedora 34(client) | 192.168.100.5/24 | |
| 48 | + |
| 49 | +You can use Fedora 34 to upload and download |
| 50 | + |
| 51 | +```mermaid |
| 52 | +graph LR; |
| 53 | +RockyLinux8-->|pull/download|Fedora34; |
| 54 | +Fedora34-->|push/upload|RockyLinux8; |
| 55 | +``` |
| 56 | + |
| 57 | +You can also use Rocky Linux 8 to upload and download |
| 58 | + |
| 59 | +```mermaid |
| 60 | +graph LR; |
| 61 | +RockyLinux8-->|push/upload|Fedora34; |
| 62 | +Fedora34-->|pull/download|RockyLinux8; |
| 63 | +``` |
| 64 | + |
| 65 | +## Demonstration based on SSH protocol |
| 66 | + |
| 67 | +!!! tip "Attention!" |
| 68 | + Here, both Rocky Linux 8 and Fedora 34 use the root user to log in. Fedora 34 is the client and Rocky Linux 8 is the server. |
| 69 | + |
| 70 | +### pull/download |
| 71 | + |
| 72 | +Since it is based on the SSH protocol, we first create a user in the server: |
| 73 | + |
| 74 | +```bash |
| 75 | +[root@Rocky ~ ] # useradd testrsync |
| 76 | +[root@Rocky ~ ] # passwd testrsync |
| 77 | +``` |
| 78 | + |
| 79 | +On the client side, we pull/download it, and the file on the server is /rsync/aabbcc |
| 80 | + |
| 81 | +```bash |
| 82 | +[root@fedora ~ ] # rsync -avz [email protected]:/rsync/aabbcc /root |
| 83 | + |
| 84 | +receiving incremental file list |
| 85 | +aabbcc |
| 86 | +sent 43 bytes received 85 bytes 51.20 bytes/sec |
| 87 | +total size is 0 speedup is 0.00 |
| 88 | +[root@fedora ~]# cd |
| 89 | +[root@fedora ~]# ls |
| 90 | +aabbcc |
| 91 | +``` |
| 92 | +The transfer was successful. |
| 93 | +
|
| 94 | +!!! tip "Attention" |
| 95 | + If the server's SSH port is not the default 22, you can specify the port in a similar way-`rsync -avz -e ' ssh -p [port] ' `. |
| 96 | + |
| 97 | +### push/upload |
| 98 | + |
| 99 | +```bash |
| 100 | +[root@fedora ~]# touch fedora |
| 101 | +[root@fedora ~]# rsync -avz /root/* [email protected]:/rsync/ |
| 102 | + |
| 103 | +sending incremental file list |
| 104 | +anaconda-ks.cfg |
| 105 | +fedora |
| 106 | +rsync: mkstemp " /rsync/.anaconda-ks.cfg.KWf7JF " failed: Permission denied (13) |
| 107 | +rsync: mkstemp " /rsync/.fedora.fL3zPC " failed: Permission denied (13) |
| 108 | +sent 760 bytes received 211 bytes 277.43 bytes/sec |
| 109 | +total size is 883 speedup is 0.91 |
| 110 | +rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1330) [sender = 3.2.3] |
| 111 | +``` |
| 112 | + |
| 113 | +**Prompt permission denied, how to deal with it? ** |
| 114 | +First check the permissions of the /rsync/ directory. Obviously, there is no permission. We can use `setfacl` to give permission: |
| 115 | + |
| 116 | +```bash |
| 117 | +[root@Rocky ~ ] # ls -ld /rsync/ |
| 118 | +drwxr-xr-x 2 root root 4096 November 2 15:05 /rsync/ |
| 119 | +``` |
| 120 | + |
| 121 | +```bash |
| 122 | +[root@Rocky ~ ] # setfacl -mu:testrsync:rwx /rsync/ |
| 123 | +[root@Rocky ~ ] # getfacl /rsync/ |
| 124 | +getfacl: Removing leading ' / ' from absolute path names |
| 125 | +# file: rsync/ |
| 126 | +# owner: root |
| 127 | +# group: root |
| 128 | +user::rwx |
| 129 | +user:testrsync:rwx |
| 130 | +group::rx |
| 131 | +mask::rwx |
| 132 | +other::rx |
| 133 | +``` |
| 134 | + |
| 135 | +Try again, success! |
| 136 | + |
| 137 | +```bash |
| 138 | +[root@fedora ~ ] # rsync -avz /root/* [email protected]:/rsync/ |
| 139 | + |
| 140 | +sending incremental file list |
| 141 | +anaconda-ks.cfg |
| 142 | +fedora |
| 143 | +sent 760 bytes received 54 bytes 180.89 bytes/sec |
| 144 | +total size is 883 speedup is 1.08 |
| 145 | +``` |
0 commit comments