1.先实现scp,拷贝不需要密码。然后在考虑增量备份
假设A,B两服务器,现在需要在A机上用root登陆B机,而不需要输入密码,那我们可按照下面的步骤来做:
1)在A机上生成钥匙对,执行以下命令:
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):直接回车
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:
f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 root@host1
这样,在/root/.ssh/路径下会生成id_rsa,和id_rsa.pub,其中id_rsa是密钥,id_rsa.pub是公钥。
2)把在A机生成的id_rsa.pub拷贝到B机上,假设拷贝到B机的临时目录下,如:
scp /root/.ssh/id_rsa.pub root@192.168.1.22:/tmp
3)用root帐号登陆B机,进入其主目录,创建authorized_keys文件,并设置好权限。
cd ~/.ssh
cat /tmp/id_rsa.pub >>authorized_keys
chmod400 authorized_keys
rm -f /tmp/id_rsa.pub
4)重新打开A的链接窗口,命令行下输入
ssh 192.168.1.22
就可以看到直接进入了。
2.mysql备份数据库的脚本
only_mysql.sh脚本内容如下
MY_USER="root"
MY_PASS="123456"
MY_HOST="localhost"
MY_CONN="-u $MY_USER -p$MY_PASS -h$MY_HOST"
MY_DB1="discuz"
BF_DIR="/root/sqlback/"
BF_CMD="/usr/bin/mysqldump"
#BF_TIME=`date +%Y%m%d-%H%M`
BF_TIME=`date +%Y%m%d`
NAME_1="$MY_DB1-$BF_TIME"
cd $BF_DIR/
$BF_CMD $MY_CONN --databases $MY_DB1 > $NAME_1.sql
/bin/tar zcf $NAME_1.tar.gz $NAME_1.sql --remove &> /dev/null
3.scp实现增量备份
scp.sh脚本如下内容“
MY_DB1="discuz"
BF_TIME=`date +%Y%m%d`
NAME_1="$MY_DB1-$BF_TIME"
/usr/bin/scp /root/sqlback/$NAME_1.tar.gz 192.168.1.22:/root/sqlback/
4.添加到任务计划,搞定!
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2016-11-19 12:54
聂文龙 阅读(968)
评论(0) 编辑 收藏 引用