1. environment: two Centos computers are directly connected. the IP address of server A is 192.168.1.124, and that of server B is 192.168.1.121. 2. install the required software (the ssh system has been installed by default): server A: yuminstallrsyncinow.tools server B: yumins 1. environment: two Centos computers are directly connected, the IP address of server A is 192.168.1.124, and that of server B is 192.168.1.121.
2. install the required software (the ssh system has been installed by default ):
Server:
Yum install rsync inotify-tools
Server B:
Yum install rsync
3. create a user (backup execution user)
Create User: useradd rsync
Set password: Omitted
Note: Users must be created for both servers (the same user can be created for the two days, or different users can be created)
4. create an ssh-key for automatic ssh connection
Server
Create a public key and a private key
[Root @ A ~] # Su-rsync
[Rsync @ A ~] $ Ssh-keygen-t rsa (all use the default value. press enter all the time)
Then, a pair of "public key and private key" is generated under/home/rsync/. ssh, as shown below:
[Rsync @ A. ssh] # ll
Total 12
-Rw -------. 1 rsync 1675 Oct 20 id_rsa
-Rw-r --. 1 rsync 393 Oct 20 id_rsa.pub
Run the following command to route id_rsa.pub to server B.
Scp/home/rsync/. ssh/id. rsa. pub [email protected]:/home/rsync/
Server B:
Create a directory for storing the "public key" in ssh:
Mkdir/home/rsync/. ssh
Chmod 700/home/rsync/. ssh
Route the public key passed on server A to/home/rsync/. ssh. The following Command
Mv/home/rsync/id_rsa.pub/home/rsync/. ssh/authorized_keys
Chmod 600/home/rsync/. ssh/authorized_keys
Restart the SSH service
[Root @ A ~] #/Etc/init. d/sshd restart
[Root @ B ~] #/Etc/init. d/sshd restart
5. Use of inotify_tools
1. determine the inotifywait location
2. common use of inotifywait
/Usr/bin/inotifywait-mrq-e modify, delete, create, attrib
Command explanation:
-M: Keep Listening
-R is used to recursively View directories.
-Q indicates the output event.
-E create, move, delete, modify, and attrib refer to the "listening for creating a mobile delete write permission" event.
6. create A real-time monitoring backup script inotify. ssh on server
#! /Bin/sh
SRC =/usr/local/mysql/binlog/
DST = [email protected]:/home/rsync1/test
/Usr/bin/inotifywait-mrq-e modify, delete, create, attrib $ {SRC} | while read D E F
Do
/Usr/bin/rsync-ahqzt -- delete $ SRC $ DST
Done
Command explanation:
SRC: monitored Directory ()
DST: backup directory
While read d e f: The inotifywait command generates three return values: "date, time, and file", which are passed as parameters to read, therefore, the "while read d e f" statement in the script details the return value.
Rsync command explanation:
/Usr/bin/rsync-ahqzt -- delete $ SRC $ DST
-A Archive mode
-H save the hard connection
-Q: stop non-error messages
-Z: the compressed file data is being transmitted.
-T maintenance modification time
-Delete: delete unnecessary files.
7. run the script
Server:
Modify execution permission: chmod + x inotify. sh
Run the script in rsync user mode (the ssh-key is created by the rsync user ):
Su-rsync
[Rsync @ A sbin] $./inotify_rsync.sh & # run in the background
This time, files and directories are created under the/usr/local/mysql/binlog/# directory of server A, which can be backed up to the target location in real time.
Note:
1. does the backup user rsync have the permission to read/usr/local/mysql/binlog/directory? otherwise, the backup fails.
2. we recommend that you modify the storage directory of mysql-bin as follows:
Log-bin =/usr/local/mysql/binlog/mysql-bin
# Open the log (the host needs to be opened). This mysql-bin can also be customized. you can add a path here, for example,/usr/local/mysql/binlog/mysql-bin.