Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic ways to use rsync

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the basic uses of rsync". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic uses of rsync"?

Rsync is a tool that enables incremental backups. With task planning, rsync can achieve timing or interval synchronization, and with inotify or sersync, trigger real-time synchronization can be achieved.

I. the usage of the rsync command:

Basic format: rsync [options] original location target location common options:

-an archiving mode, recursive merge retains object attributes, equivalent to-rlptgoD

-v displays the verbose information of the synchronization process

-z compress (compress) when transferring files

-H retains hard link files

-A preserves the ACL attribute-delete deletes files that exist in the target location but not in the original location

-r recursive mode, which contains all files in directories and subdirectories

-l is still copied as a soft link file for a soft link file

-p keep the permission tag of the file

-t retain the time stamp of the file

-g retain the group tag of the file (for superusers only)

-o keep the owner tag of the file (for superusers only)

-D retain equipment files and other special documents

2. Configure rsync

Before configuring rsync, let's do a little test:

Server side

# write some content on the home page of the server website [root@localhost Desktop] # cd / var/www/html [root@localhost html] # vim index.html [root@localhost html] # cat index.htmlHello WorldIn Hello Jaking! [root@localhost html] # ifconfigeth0 Link encap:Ethernet HWaddr 00:0C:29:BE:68:3F inet addr:192.168.142.132 Bcast:192.168.142.255 Mask:255.255.255.0 inet6 Addr: fe80::20c:29ff:febe:683f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:580 errors:0 dropped:0 overruns:0 frame:0 TX packets:390 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:57739 (56.3 KiB) TX bytes:41856 (40.8 KiB) lo Link encap:Local Loopback Inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: 1Comp128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960. 0b) [root@localhost rsync] # service httpd restartStopping httpd: [OK] Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name Using localhost.localdomain for ServerName [OK]

Client

# the client can successfully access the content of the home page of the server website [root@localhost Desktop] # curl 192.168.142.132Hello wordings Hello Jaking!

The quiz just now is actually based on SSH. Rsync has two synchronization sources, one is based on SSH, and the other is based on rsync.

Synchronization sources based on SSH

Set ACL permissions: setfacl-m user: user name: rwx / server directory downlink synchronization: rsync-avz user name @ server address: / server directory / local directory uplink synchronization: rsync-avz / local directory user name @ server address: / server directory

In order to ensure that the data from the server can be synchronized to the client, next, I start the configuration from the synchronization source of SSH: before configuration, execute yum install-y rsync on the server and client respectively to make sure that rsync is installed.

1. Authorize a user on the server, that is, create a user:

[root@localhost html] # useradd server [root@localhost html] # passwd serverChanging password for user server.New password:BAD PASSWORD: The password is shorter than 8 charactersRetype new password:passwd: all authentication tokens updated successfully.

two。 Create a ssh directory on the client side and synchronize the server data:

[root@localhost Desktop] # mkdir / client [root@localhost Desktop] # cd / client/ [root@localhost client] # mkdir ssh [root@localhost client] # rsync-avz server@192.168.142.132:/var/www/html/* / client/sshserver@192.168.142.132's password:receiving incremental file listindex.htmlsent 68 bytes received 219 bytes 114.80 bytes/sectotal size is 27 speedup is 0.0930 bytes received 104 bytes 15.76 bytes/sectotal size is 27 speedup is 0.20 [root @ localhost client] # cd ssh [root@localhost ssh] # lsindex.html [root@localhost ssh] # cat index.htmlHello WorldIn Hello Jakinggirls # client has successfully synchronized server data

3. The synchronization just now is downlink synchronization, that is, synchronizing data from the server to the client. Next, I will demonstrate uplink synchronization, that is, synchronizing data from the client to the server:

# create a new file on the client side and prepare to synchronize it to the server. [root@localhost ssh] # touch a.txtb.txt [root@localhost ssh] # lsa.txt b.txt index.html [root@localhost ssh] # rsync-avz / client/ssh/* server@192.168.142.132:/var/www/htmlserver@192.168.142.132's password:sending incremental file lista.txtb.txtrsync: mkstemp "/ var/www/html/.a.txt.6JDDzO" failed: Permission denied (13) rsync: mkstemp "/ var/www/html/.b. Txt.p7hCLz "failed: Permission denied (13) sent 131 bytes received 50 bytes 40.22 bytes/sectotal size is 27 speedup is 0.15rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c (1052) [sender=3.0.9] # synchronization failed From the result of the error report, the server user has insufficient permissions, and the server user does not have write permission to the / var/www/html directory.

4. Set more secure ACL permissions on the server:

[root@localhost html] # setfacl-m user:server:rwx / var/www/html

5. Perform the uplink synchronization operation on the client again:

[root@localhost ssh] # rsync-avz / client/ssh/* server@192.168.142.132:/var/www/htmlserver@192.168.142.132's password:sending incremental file lista.txtb.txtsent 131bytes received 50 bytes 51.71 bytes/sectotal size is 27 speedup is 0.The synchronization process shows that index.html has not been uploaded, which shows that the synchronization mechanism used by rsync is the incremental backup mechanism.

View on the server:

[root@localhost html] # lsa.txt b.txt index.html# client data has been successfully synchronized to the server. 4. Rsync-based synchronization source

/ etc/rsyncd_users.db file permission must be 600. for uplink synchronization, nobody needs write permission. Rsync-avz user name @ server address:: share module name / local directory rsync-avz rsync:// user name @ server address / share module name / local directory

Using synchronous sources for SSH requires the creation of users, and for the server, having too many users is not a good thing. On the other hand, there is no need to create a user with a rsync-based synchronization source, the specified user only needs to be written in the configuration file, such a user is a virtual user.

1. Modify the configuration file:

Server side

[root@localhost html] # vim / etc/rsyncd.conf# create [root@localhost html] # cat / etc/rsyncd.confaddress = 192.168.142.132port 873pid file = / var/run/rsyncd.pidlog file = / var/log/rsyncd.log [share] comment = soft path = / server/rsync read only = yes dont compress = * .gz * .bz2 * .zip auth users = wang secrets file = / etc/rsyncd_users.db if the configuration file does not exist [root@localhost html] # vim / etc/rsyncd_ users.db[ root @ localhost html] # cat / etc/rsyncd_users.dbwang:123456 # rsync does not support complex passwords Try to keep it simple. [root@localhost html] # vim / etc/xinetd.d/rsync [root@localhost html] # cat / etc/xinetd.d/rsync# default: off# description: The rsync server is a good addition to an ftp server As it\ # allows crc checksumming etc.service rsync {disable = yes flags = IPv6 socket_type = stream wait = no user = root server = / usr/bin/rsync server_args =-- daemon log_on_failure + = USERID} [root@localhost html] # rsync-- daemon # launch rsync [root @ localhost html] # netstat-pantu | grep 873tcp 0 0 0 .142.132server/rsync 873 0.0.0.0 LISTEN 6779/rsync [root@localhost html] # mkdir-p / server/rsync [root@localhost html] # cd! $cd / server/rsync [root@localhost rsync] # touch rsync.txt [root@localhost rsync] # lsrsync.txt [root@localhost rsync] # chmod 600 / etc/rsyncd_users.db # be sure to grant 600 permissions to password files Otherwise, there will be an error in synchronizing data!

two。 Perform a synchronization operation:

Client

[root@localhost rsync] # rsync-avz wang@192.168.142.132::share / client/rsyncPassword:receiving incremental file list./rsync.txtsent 77 bytes received 151 bytes 50.67 bytes/sectotal size is 0 speedup is 0.00 [root@localhost rsync] # lsrsync.txt# data synchronization succeeded [root@localhost rsync] # pwd/client/rsync

The downlink synchronization is complete, and next I will demonstrate the uplink synchronization:

Server side

# be sure to modify module permissions and ACL permissions before performing uplink synchronization [root@localhost rsync] # vim / etc/rsyncd.conf [root@localhost rsync] # cat / etc/rsyncd.confaddress = 192.168.142.132port 873pid file = / var/run/rsyncd.pidlog file = / var/log/rsyncd.log [share] comment = soft path = / server/rsync read only = no # it must be changed to no dont compress = * .gz * .bz2 * .zip Auth users = wang secrets file = / etc/rsyncd_ users.db[ root @ localhost rsync] # setfacl-m u:nobody:rwx / srver/rsync # set ACL permissions [root@localhost rsync] # pkill rsync # close rsync [root@localhost rsync] # rsync-- daemon # start rsync

Client

[root@localhost rsync] # touch client.txt [root@localhost rsync] # rsync-avz / client/rsync/* wang@192.168.142.132::sharePassword:sending incremental file listclient.txtsent 85 bytes received 27 bytes 32.00 bytes/sectotal size is 0 speedup is 0.0 Uplink synchronization succeeded

View on the server:

[root@localhost rsync] # lsclient.txt rsync.txt [root@localhost rsync] # pwd/server/rsync

3. Another format for uplink synchronization:

Client

[root@localhost rsync] # lsclient.txt rsync.txt [root@localhost rsync] # touch test.txt [root@localhost rsync] # rsync-avz / client/rsync/* rsync://wang@192.168.142.132/sharePassword:sending incremental file listtest.txtsent 102 bytes received 27 bytes 28.67 bytes/sectotal size is 0 speedup is 0.00

Server side

[root@localhost rsync] # lsclient.txt rsync.txt test.txt 5. Configure password-free authentication

1. Synchronization source based on SSH

Implement the client through the secret key pair

[root@localhost ssh] # pwd/client/ssh [root@localhost ssh] # lsa.txt b.txt index.html [root@localhost ssh] # rm-rf * [root@localhost ssh] # ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/ root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again:Your identification has been saved in / root/.ssh/id_rsa.Your public key has been saved in / root/.ssh/id _ rsa.pub.The key fingerprint is:3d:fe:c8:0e:2c:b7:90:b0:f4:0d:31:af:b4:d3:9e:87 root@localhost.localdomainThe key's randomart image is:+-- [RSA 2048]-+ | o | | +. | | o o S o | |. = O. . | |. O *. | | * Edou.o | | + o +. | | | +-+ [root@localhost ssh] # [root@localhost ssh] # ssh-copy-id server@192.168.142.132server@192.168.142.132's password:Now try logging into the machine, with "ssh 'server@192.168.142.132'" And check in: .ssh / authorized_keysto make sure we haven't added extra keys that you weren't expecting. [root@localhost ssh] # id server # server user successfully logged in to server eth0 Link encap:Ethernet HWaddr 00:0C:29:BE:68:3F inet addr:192.168.142.132 Bcast at server id: server: No such user [root@localhost ssh] # ssh server@192.168.142.132 [server@localhost ~] $ifconfig#: 192.168.142.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:935 errors:0 dropped:0 overruns:0 frame:0 TX packets:660 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:112043 (109.4 KiB) TX bytes: 89842 (87.7 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: 1Universe 128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b) [server@localhost ~] $exitlogoutConnection to 192.168.142.132 closed. [root@localhost ssh] # ls [root@localhost ssh] # pwd/client/ssh [root@localhost ssh] # rsync-avz server@192.168.142.132:/var/www/html/* / client/ssh/receiving incremental file lista.txtb.txtindex.html# now no input is required to perform synchronization Password sent 68 bytes received 219 bytes 191.33 bytes/sectotal size is 27 speedup is 0.09 [root@localhost ssh] # lsa.txt b.txt index.html# deleted files are synchronized from the server again

2. Synchronization source based on rsync

Implementation of RSYNC_PASSWORD client through system variable

[root@localhost client] # cd rsync/ [root@localhost rsync] # lsclient.txt rsync.txttest.txt [root@localhost rsync] # rm-rf * [root@localhost rsync] # export RSYNC_PASSWORD=123456 # 123456 is the password for the virtual user wang [root@localhost rsync] # rsync-avz wang@192.168.142.132::share / client/rsyncreceiving incremental file list./client.txtrsync.txttest.txt# now you do not need to enter the password sent 115 bytes received 265 bytes 760 to perform synchronization operation .00 bytes/sectotal size is 0 speedup is 0.00 [root@localhost rsync] # lsclient.txt rsync.txt test.txt# deleted files are synchronized from the server again. Thank you for reading. The above is the content of "what is the basic use of rsync". After the study of this article, I believe you have a deeper understanding of what the basic use of rsync has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report