rsync Backup tool
I. SCP
SCP Backup tool is not available for incremental backups
1. Installing SCP
[email protected] ~]# Yum install-y openssh-clients
2, Scp-r 192.168.21.98:/tmp/a/./a/
Copy the A directory below/tmp/from the remote host to the current directory
-R: Recursively copy
3, [[email protected] cheng]# SCP 1.txt 192.168.21.98:/root/cheng/
Copy the files in the current directory to the/root/cheng/directory of the remote host
4, [[email protected] cheng]# SCP 1.txt [email protected]:/tmp/cheng/
Copy the 1.txt file to the remote host Cheng this user
5, if the user name is omitted, then it will be copied to the default user, that is, this side is used to copy the user,
Then the compound will be the same account. If the other host does not have that user, so long copy unsuccessful
6, Scp-r a/192.168.21.98:/tmp/b/
Copy the files under the A directory to the remote Host B directory, instead of copying the a target to the B directory
Second, rsync Backup tool, you can copy files to a remote host, can support incremental backup
1, the default is not installed, need to install:
[email protected] ~]# Yum install-y rsync
Also install: Openssh-clients not be able to execute
2. rsync command format
rsync [OPTION] ... SRC DEST
Rsync-av 123.txt/tmp/
Synchronizing the 123.txt to the/tmp directory
rsync [OPTION] ... SRC [[Email protected]]host:dest
Rsync-av 123.txt 192.168.0.101:/data/
Synchronize 123.txt files to the/data/directory of the 192.168.0.101 machine
rsync [OPTION] ... [[email protected]] HOST:SRC DEST
Rsync User name @ip address:/tmp/1.txt/tmp/1.txt
Synchronize remote host/TMP/1.TX files to local
rsync [OPTION] ... [[email protected]] Host::src dest
rsync [OPTION] ... Src [[email protected]]host::D est
These two use two colons, which differs from the previous approach in that they are validated differently and later written to
Rsync-av usr//tmp/123/
Copy the user directory under TMP, do not change the name to 123
SRC:SRC is the abbreviation of source, that is, the meaning of the code. The source file address is represented here
-A: Make sure that the original properties of the data do not change when synchronizing data
Third, rsync common options
-a archive mode, which means that the file is transferred recursively, and all properties are equal to-rlptgod, and the-a option can be followed by a--no-option, which indicates that one of the-rlptgod is closed, for example-a--no-l is equivalent to-rptgod
-R subdirectory in recursive mode, mainly for the directory, if a separate file does not need to add-r, but the transfer is the directory must be added to the-r option
-V Print some information, such as rate, number of files, etc.
-L reserved Soft link
-L process Soft links as normal files, if there is a soft link file in src, then add this option will be the soft link to the target file to the DST, equal to the real files are also copied past
-P Maintain file permissions
-O Keep File owner information
-G hold File group information
-D Keep Device file information
-T hold file time information
-Z Compress at copy time
--delete Delete the files that are in the dest but SRC does not
--exclude=pattern specifies to exclude files that do not need to be transferred, the equals sign followed by the file name, can be a universal character pattern (such as *.txt)
Rsync-av--exclude= "*.txt" usr//tmp/123/#同步的时候排除掉以. txt end file.
When filtering multiple files, add--exclude= "file type or name" directly after-av.
--progress in the process of synchronization can see the process state of synchronization, such as statistics to synchronize the number of files, synchronized file transfer speed, etc.
Can be written like this: RSYNC-AVP usr//tmp/123/
--BWLIMIT=100: Control the synchronization bandwidth, 100 refers to the speed, can be written according to the actual situation
-U plus this option will exclude new files in dest than SRC and will not overwrite
The most commonly used-a-v--delete--exclude
-A--no-l-V: This option is for copying files without copying the soft connection file
Iv. rsync Options Explained
1, Rsync-av dir1/dir2/# where dir2/directory can not exist, remember to synchronize the directory must be at the end of the Add/
-A: A copy of the soft connection will be copied in the past
2, Rsync-avl test1/test2/# plus-l will copy the corresponding source file of the soft connection file into the target directory
3. The-u option is to ignore the file if the target file is newer than the source file
Touch Test2/1.txt; Rsync-avu test1/test2/
4, Rsync-av--delete test1/test2/#这样会把test2/directory than the test1/directory more files removed
5, Rsync-a--exclude= "2.txt" test1/test2/#在同步的过程中, will ignore 2.txt this file
6, Rsync-a--progress--exclude= "*.txt" test1/test2/#--progress Show details of the synchronization process, you can also use wildcard characters after--exclude *
V. Application example of rsync-ssh mode
Rsync-avl test1/[Email protected]:/tmp/test2/
Rsync-avl 192.168.0.101:/tmp/test2/./test3/
It is not appropriate to write to the script because of the need to enter a password, but you can create a trust relationship between two machines by creating a key pair so that you do not have to enter a password
1, in Linux2 to create the following information, it is important to note that in the experiment, the LINUX2 host is the server
[[email protected] ~]# mkdir .ssh[[email protected] ~]# ssh-keygen Generating 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:19:da:90:61:63:d7:94:65:7a:4d:ad:05:06:50:49:5a [email protected]the key ' s  RANDOMART IMAGE IS:+--[ RSA 2048]----+| = . oo*eo+o | | o = o=.+ o| | o . o . .o | | + o . . | | . s | | | | | | | | |+------ -----------+[[email protected] ~]# ls .sshid_rsa id_rsa.pub known_ hosts[[email protected] ~]# cat .ssh/id_rsa.pub ssh-rsa aaaab3nzac1yc2eaaaabiwaaaqeaxcpm8bviqohy/jwi9kbahtkb+w4tnnl3xg12cfj+h52yu0ixvanj+ Qrsq8xcxd47gnalgt4jzmmgizq8txfrjmybdntrwswqr1oxefabmuehwbwmsnmd2g9dzy8r8ekxiebksgk5uycpoozjbtfrd0xjefzfqlhey3pew9hooslj /koglwpxodidlh0ordze2qp5kovhw6nqdylijpveptss4qwr2dsv5recqw/l7yei1gxklsyefew0xsia58hy3gnylp8pylcopwis0osmhvcljr/xw7ixfhnwfzah/iho5jt+ 8vobdao2ajmg5yrni4oopldn8vijvgoj/gd78w== [email protected]
Copy the secret key to the/root/.ssh/authorized_keys file on the Chenglinux machine, if the Chenglinux machine does not have an. SSH directory and the Authorized_keys file needs to be created.
2, because Chenglinx this machine does not have the. SSH directory and the Authorized_keys file, so you'll need to create
[Email protected] ~]# mkdir ssh[[email protected] ~]# chmod. Ssh/[[email protected] ~]# touch. ssh/authorized_keys[[ Email protected] ~]# vim. Ssh/authorized_keys
3, test the login Chenglinx on the LINUX2 host, the result shows that no password has been entered.
[[email protected] ~]# ssh [email protected]last login:wed June 3 14:36:35 from 192.168.21.100[[email protected] ~]#
4, Test rsync synchronization, also did not prompt for password, so success.
[Email protected] ~]# rsync-av chengzelin.txt 192.168.21.99:/root/chengzelin.txt2sending Incremental file Listchengzelin.txtsent Bytes received Notoginseng bytes 238.00 bytes/sectotal size is (speedup is 0.66[[email protected] ~] #
5, if the SSH port is not 22, then you need to write this form
Rsync-av-e "Ssh-p Port"/dir1/192.168.0.101:/tmp/dir2/
6, sshd's configuration file location
/etc/ssh/sshd_condfig
7, Strace command is used to track the process, the default is not installed, need to install
Vi. application Example of rsync-back-end service mode
1. Global configuration file
Configuration file/etc/rsyncd.conf, with the following content:
#port the =873 #监听端口默认为873, or it could be another port.
Log File=/var/log/rsync.log #指定日志
PID File=/var/run/rsyncd.pid #指定pid
#address =192.168.0.10 #可以定义绑定的ip
2, the settings within the module
[Test] #为模块名, custom, the module can have multiple
Path=/root/rsync # Specifies which directory the module corresponds to
Use Chroot=true #是否限定在该目录下, the default is true, when there is a soft connection, you need to change to Fasle
Max Connections=4 # Specifies the maximum number of clients that can be connected
Read Only=no #是否为只读
List=true #是否可以列出模块名
Uid=root #以哪个用户的身份来传输
Gid=root #以哪个组的身份来传输
Auth Users=test #指定验证用户名, can not be set
Secrets file=/etc/rsyncd.passwd #指定密码文件, if you set the authentication user, this item must be set
Hosts allow=192.168.0.101 #设置可以允许访问的主机, can be a network segment
One thing to note is that you typically don't need to specify a user name and set a password, just specify the hosts option to
3, the password file/etc/rsyncd.passwd content format is: Username:password
4, the command to start the service is: rsync--daemon
5, Rsync-av Chengzelin.txt 192.168.21.99::test/4.txt
The format is to use the module you just set to synchronize the file, test is equal to the Rsync directory in the module settings, similar to an alias (here is the format mentioned above, this is the format:rsync [OPTION] ...) [[email protected]] HOST::SRC DEST)
Default to use/etc/rsyncd.conf This configuration file, you can also specify the configuration file rsync--daemon--config=/etc/rsyncd2.conf
The options available are: rsync--daemon--help
This article is from the It Exchange Learning blog, so be sure to keep this source http://sxct168.blog.51cto.com/824373/1660781
Rsync Backup tool for daily management of Linux systems