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

来源: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分钟之内,有时候可能无法执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值