rsync is a fast incremental file transfer tool that can be used for backup within the same host backup. We can also use it as a network backup tool for different hosts. This article mainly describes how to set up your own rsync server to achieve file transfer, backup and mirroring. Compared with tar and wget, rsync also has its own advantages, such as fast, safe and efficient;
Manual:
First, you need to establish the public key and key between the two servers, so that you don't need to enter the password every time, and it can be automated.
You can first use rpm -qa |grep rsync to check whether rsync has been installed. The homepage address of Rysnc for software download is: http://rsync.samba.org/
Test environment for this experiment (2 units, one server and one client)
rsync server ip: 192.168.48.60
rsync client ip: 192.168.48.148
--------------------------------------------------
Configure the server side first:
1. Install rsync package
# rpm -q rsync
rsync-2.6.3-1
2. rsync only uses one configuration file, which is /etc/rsyncd.conf. This file generally does not exist, so just create one yourself.
# vi /etc/rsyncd.conf
uid = nobody
gid = nobody
max connections = 200
timeout=600
use chroot=no
readonly=yes
pid file=/var/run/rsyncd.pid
host_allow =192.168.48.148 //The client's IP address is written here
#syslog facility = local7
#log file=/var/log/rsyncd.log
#rsyncconfig
#The 'standard' things
[rsync_gmmold] //Define the synchronization path (the client uses this keyword to link)
path = /home //The path that needs to be synchronized
comment = gmmold //I don’t know this yet
3. Start the service
#/usr/bin/rsync --daemon Finally, start rsync as a daemon on the server side. The port started by rsync is port 873.
4. Add boot startup
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
5. Check when rsync starts
# ps -ef | grep rsync
6. Stop service
#kill `cat /var/run/rsyncd.pid`
The following is the client configuration:
1. Install rsync package
[root@rhel403 old]# rpm -q rsync
rsync-2.6.3-1
2. Synchronize with the server:
#rsync -ave ssh [email protected]:/home/ /home/ganfic/laji/ //Synchronize and back up the files under /home of the 60 machine to /home/ganfic/laji/
Automatically sync backups:
1. Create a b.sh file under tmp
#vi /tmp/b.sh
Write the following content inside
#!bin/sh
rsync -ave ssh [email protected]:/home/ /home/ganfic/laji/
:wq
2 The second step involves crontab, enter on the command line
#crontab -e
Then put * * * * * sh /tmp/b.sh //* * * * * part to represent each minute, and then sh /tmp/b.sh means to run the b.sh script (I will add explanation below), -e Edit the crontab file using the editor specified by the environment variable EDITOR or RVISUAL. The newly created crontab file will be placed in the /var/spool/cron directory. The file name is the user name. After using cd /var/spool/cron, you will see the user file, and then use vi root (I am under the root user) ) and you will see that * * * * * sh /tmp/b.sh has been transferred here.
This is done. Then we need to see if it is running. Open the log file. The log file is in #vi /var/log/cron and you will see if it is running. You can also do this. Before using automatic running, we It was operated manually. At that time, the files in the /home directory of the 60 machine were also copied. In this way, when we enter the /home/ganfic/laji folder of the local machine, we will see the /home directory of the 60 machine. Content, delete all these contents, and then wait for 1 minute to see if the content will be added again. If so, it is successful.
The Linux firewall uses iptables, so we must allow the rsync server port you defined to pass at least on the server side, and also on the client side.
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
Start the rsync service:
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
$rsync_HOME/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
Replenish:
Related knowledge of rsync:
Usage of rsync command
After configuring the rsync server, you can issue rsync commands from the client to implement various synchronization operations. rsync has many functional options. The following is an introduction to the commonly used options:
The command format of rsync can be:
1. rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
2. rsync [OPTION]... [USER@]HOST:SRC DEST
3. rsync [OPTION]... SRC [SRC]... DEST
4. rsync [OPTION]... [USER@]HOST::SRC [DEST]
5. rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
6. rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]