Recently, you need to write a script to back up the programs on each server to a specified server. Originally, you thought that you could check the use of Rsync command 321. As a result, the Rsync command should support automatic login You still need to configure the service and parameters. You are not sure whether the configuration on the Internet is OK or not. Because it is the same, but it does not indicate the beginning and the end, so you choose a SSH automatic login which is less expensive to try instead.
SSH can be used by everyone. Usually, the basic function we use is to log in or execute shell commands remotely.
[plain] view plain copy
Log in
ssh [email protected]_or_ip
##Execute remote command
ssh [email protected]_or_ip commands
But usually if the command is executed, it is executed by shell script. I certainly don't want to input the password every time, so I can't automate it and feel less happy. So there seem to be many ways to make SSH log in automatically. Here we use the public key authentication method with the least cost. See the following for details:
A: Local machine
B: Is the remote server [logged in]
1. Generate the private key and public key of SSH on a [the generated files are all in ~ /. SSH directory]
[plain] view plain copy
SSH keygen - t RSA needs 3 carriage returns
SSH keygen - t RSA - P - enter only once
2. Transfer the public key content of a to the authorized keys file under the. SSH folder of the user directory of B
[plain] view plain copy
SCP ~ /. SSH / ID ﹣ RSA. Pub [email protected] ﹣ host ﹣ or ﹣ IP: ~ /. SSH / authorized ﹣ keys ᦇ the user who wants to log in automatically every time, and the B ﹣ user here will use whatever account, because the default will be tested under this account. SSH
Note: be sure to put the content in 3. When a is operating with SSH, you don't need to enter a password
[plain] view plain copy
SSH [email protected] ﹐ host ﹐ or ﹐ IP ﹐ direct login succeeded
SCP and SSH are a set, but this tool is used for file transfer between Linux, using the same secure transfer protocol as SSH. So after SSH can automatically log in, you can use SCP command to copy files between servers without entering password manually. In fact, at this time, you can realize the function of automatic backup of files by shell script, because SCP is a copy program between servers, and SCP is the meaning of security copy. Common commands of SCP are:
[plain] view plain copy
SCP / path / to / source [email protected]: / path / to / local
SCP [email protected]: / path / to / source / path / to / local
Rsync command is a remote synchronization program. Compared with SCP, it can back up files at the lowest cost, only backing up files with differences, so that each backup will be much less time. In addition to its own protocol, it also supports SSH transmission. As long as you add a SSH parameter, common command formats are:
[plain] view plain copy
Rsync - AVH [SSH] / path / to / source [email protected]: / path / to / local
Rsync - AVH [SSH] [email protected]: / path / to / source / path / to / local
Similarly, the command can automatically synchronize files without entering a password after SSH can automatically log in. Of course, the tool itself can also support the command without entering a password, -- password file = / path / to / PWD, but it also needs to configure the Rsync service, which is quite annoying. It is necessary to try again later.
The shell can automatically perform backup. The other thing is that it hopes to back up by itself on a regular basis, instead of being triggered by people. At this time, it uses the task planning command crontab of Linux. Its common command format is:
[plain] view plain copy
* * * * * /command_path
The first five * in turn represent
Minutes: 0-59
Hours: 1-23
Date: 1-31
Month: 1-12
Week: 0-6 (0 means Sunday)
You can also use some special symbols:
*: means any time
,: for split
-: indicates a segment, such as 1-5 in the second end, which means 1 to 5 points
/N: once for each n unit, for example, * / 1 in the second paragraph means once every 1 hour. It can also be written as 1-23 / 1
[plain] view plain copy
00 8,12,16 * * * /dataapp.sh
30 2 * * * /dataapp.sh
10 8,12,16 * * * /dataapp.sh
10 8,12,16 * * * /dataapp.sh
10 8,12,16 * * * /dataapp.sh
Automatic login of SSH Remote Login / SCP remote copy file / Rsync remote synchronization command under Linux