Rsync+inotify-tools+ssh implementing Mysql-bin Log file real-time backup

Source: Internet
Author: User
Tags chmod inotify rsync

1, Environment: two CentOS computer direct connection, its server A's IP address is 192.168.1.124, Server B's IP address is 192.168.1.121.


2. Requirement Software Installation (SSH system is installed by default):


Server A:

Yum Install rsync Inotify-tools


Server B:

Yum Install rsync


3. Set up user (Backup execution user)

Build Users: Useradd rsync

Set Password: slightly

Note: Both servers need to establish a user (here two days the server establishes the same user, can also establish different users)


4, establish ssh-key to implement SSH automatic connection

Server A


Establish public and private keys

[Email protected] ~]# Su-rsync

[[email protected] ~]$ ssh-keygen-t RSA (use all default values, press ENTER all the time)

When finished, a pair of "public and private keys" are generated under/HOME/RSYNC/.SSH, as shown below

[email protected]. ssh]# LL

Total 12

-RW-------. 1 rsync rsync 1675 Oct 01:47 Id_rsa

-rw-r--r--. 1 rsync rsync 393 Oct 01:47 id_rsa.pub


Then, with the following command, the ID_RSA.PUB is responsible for Server B.

scp/home/rsync/.ssh/id.rsa.pub [Email protected]:/home/rsync/


Server B:

Establish the directory where SSH stores the "Public key":

Mkdir/home/rsync/.ssh

chmod 700/home/rsync/.ssh

Transfer the public key, MV to/HOME/RSYNC/.SSH, on Server A. As the command

Mv/home/rsync/id_rsa.pub/home/rsync/.ssh/authorized_keys

chmod 600/home/rsync/.ssh/authorized_keys


Restart the SSH service
[Email protected] ~]#/etc/init.d/sshd restart
[Email protected] ~]#/etc/init.d/sshd restart


5, the use of Inotify_tools

1, determine the location of the inotifywait

2. Common use method of inotifywait


/usr/bin/inotifywait-mrq-e Modify,delete,create,attrib

Command explanation:

-M is to keep listening
-R is a recursive view directory
-Q is the print out event
-e Create,move,delete,modify,attrib refers to the "Listen for create mobile Delete Write permission" event


6. Create a real-time monitoring backup script on Server a inotify.ssh


#!/bin/sh

src=/usr/local/mysql/binlog/

[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: The directory that is backed up

While the Read D e f:inotifywait command produces three return values, namely "date, time, file", the 3 return values are passed as parameters to read, so "while read D E F" in the script refines the return value.


The rsync command explains:

/usr/bin/rsync-ahqzt--delete $SRC $DST
-A archive mode
-H Save Hard Connection
-Q Suppress non-error messages
-Z Compressed file data in the transmission
-T Maintenance modification time
-delete deletion of redundant files


7, the operation of the script


Server A:


Modify execution permissions: chmod +x inotify.sh

Run the script in Rsync user mode (Ssh-key is created by rsync users):

Su-rsync

[Email protected] sbin]$/inotify_rsync.sh & #在后台运行


This time in Server A's/usr/local/mysql/binlog/#在该目录下, the establishment of files and directories, can be backed up to the target location


Attention:

1. Backup user rsync has no permissions to read the/usr/local/mysql/binlog/directory, otherwise the backup failed

2, it is recommended to modify the Mysql-bin storage directory, as shown below

Log-bin=/usr/local/mysql/binlog/mysql-bin

#打开日志 (the host needs to open), this mysql-bin can also be customized, here can also add a path, such as:/usr/local/mysql/binlog/mysql-bin











This article is from the "Linux" blog, so be sure to keep this source http://469952080.blog.51cto.com/8311493/1704715

Rsync+inotify-tools+ssh implementing Mysql-bin Log file real-time backup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.