How to achieve remote automatic backup of Linux through scripts

  

In order to prevent data loss, what we often do is to back up these data on the computer, but although it is safe to back up the data, what should I do if the computer where the data is located? By backing up the data on this machine, once the hard disk of the machine fails, the data cannot be taken out. Remote manual backup of data is time consuming and time consuming. The best way is to implement remote automatic mutual preparation through scripts. However, whether you log in via SSH or copy files through scp, you need to enter a password. In order to overcome this problem, you first need to implement SSH login without password, so you can use rsync, scp, rexec and other commands to do remote backup.

1. Set up ssh login without password, the method is as follows:

Suppose A and B servers, now need to log in to the B machine on the A machine without entering the password, then we You can do the following steps:

1) To generate a key pair on the A machine, execute the following command:

ssh-keygen -t rsa

Generating public/private rsa Key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa

Enter passphrase (empty for no passphrase): direct Enter

Enter same passphrase again: Direct return

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:

f6:61:a8:27:35:cf:4c:6d:13:22:70:cf: 4c:c8:a0:23 root@host1

Thus, id_rsa, and id_rsa.pub are generated in the /root/.ssh/path, where id_rsa is the key and id_rsa.pub is the public key.

2) Copy the id_rsa.pub generated on the A machine to the B machine, assuming copying to the temporary directory of the B machine, such as:

scp /root/.ssh/id_rsa.pub [email protected]:/tmp

3) Log in to the B machine with the root account, enter its home directory, create the authorized_keys file, and set the permissions.

cd ~/.ssh

cat /tmp/id_rsa.pub >>authorized_keys

chmod 400 authorized_keys

rm -f /tmp/Id_rsa.pub

4) Test

Go to the root account on the A machine and try to log in to the B machine. See if you don't want a password.

Description:

The permissions of the authorized_keys file are important. If you set it to 777, you still need to provide a password when you log in.

Remember to delete the id_rsa.pub in the temporary directory to develop a good habit.

This method was tested on Red Hat 9.0.

Copyright © Windows knowledge All Rights Reserved