您的位置:首页 > 运维架构 > Shell

Linux下使用crontab调用shell中的scp自动备份mysql数据

2011-05-19 21:40 1291 查看
来源:Linux公社
  作者:王洪伟

一、为了在Linux下使用scp的时候不需要输入密码,采用ssh另一种用密钥对来验证的方式 。

1、使用ssh-keygen 命令生成密钥。生成过程中,除了输入密码外,其他都选择默认的值:回车即可。

[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、把这个密钥对中的公共密钥访问属性改为755,然后复制到备份的机器上去,并改名为 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.2MB/s   00:00

[root@xxx root]#

之后再用ssh scp sftp 访问那台机器时,就不用输入密码。这样可以利用shell进行自动文件传送了。

二、编写备份脚本 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

三、自动执行计划表,执行crontab -e

0 0 * * *  /root/crontabshell/bakdb.sh

可以测试一下,将0 0改为当前时间之后的10分钟。

注意不要修改为当前时间之后的5分钟之内,有时候可能无法执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux shell mysql date ssh 脚本