Use Linux Crontab to call SCP in shell to automatically back up Mysql Data

Source: Internet
Author: User

1. In order to use SCP without entering a password, another SSH key pair is used for verification.

1. Use the ssh-keygen command to generate a key. In addition to the password, select the default value: Press enter.

[Root @ xxx root] # ssh-keygen-T RSA
Generating public/private RSA key pair.
Enter file in which to save the key (/root/. Ssh/id_rsa ):
Created directory '/root/. Ssh '.
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:
E0: F0: 3B: D3: 0a: 3D: da: 42: 01: 6a: 61: 2f: 6C: A0: C6: E7 root @ xxx
[Root @ xxx root] #

2. Change the public key access attribute in this key pair to 755, copy it to the backup machine, and change it to authorized_keys.

[Root@ Xxx Root] #Chmod 755/root/. Ssh/id_rsa.pub [Root @ Xxx Root ] # SCP/root/. Ssh/id_rsa.pub 192.168.1.92:/root/. Ssh/authorized_keys
Root @ xxx's password:
Id_rsa.pub 100 % 218 3 . 2 Mb/s 00 : 00
[Root @ Xxx Root ] #

Then, you do not need to enter a password when accessing the machine using ssh scp sftp. In this way, you can use shell for automatic file transfer.

Ii. Write the backup script VI/root/crontabshell/bakdb. Sh

My_current_date = 'Date + % Y-% m-% d: % H: % m: % s'
/Usr/local/MySQL/bin/mysqldump-phereismysqlpassword applicationdb>/bakup_data/applicationdb _ " $ My_current_date " _Dump.txt
/Usr/local/MySQL/bin/mysqldump-phereismysqlpassword webappdb>/bakup_data/webappdb _ " $ My_current_date " _Dump.txt
SCP/bakup_data/applicationdb _ " $ My_current_date " _Dump.txt root @ 192.168.1.92 :/Bakup_data
SCP/bakup_data/webappdb _ " $ My_current_date " _Dump.txt root @ 192.168.1.92 :/Bakup_data

3. automatically execute the schedule and execute crontab-e

0   0 * **/Root/crontabshell/bakdb. Sh

You can test it and change 0 to 10 minutes after the current time.
Be sure not to change it to 5 minutes after the current time, and sometimes it may not be executed.

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.