远程rsync实时备份方案脚本

准备环境:

首先,想要使用远程备份,必须要让远程主机信任你,让你能够使用ssh无验证登陆。

ssh无密码登陆原理:

  • 首先在客户端上创建一对公私钥 (公钥文件:~/.ssh/id_rsa.pub; 私钥文件:~/.ssh/id_rsa)

  • 然后把公钥放到服务器上(~/.ssh/authorized_keys), 自己保留好私钥

  • 当ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配.如果匹配成功就可以登录了

创建两个密钥哦

[root@node4 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/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:
d4:37:37:3b:7c:13:e8:15:77:f7:5c:88:b0:6b:f7:9e [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|          .. . o=|
|         . .. o.B|
|        . o o.ooo|
|       .   o.+.o.|
|        S o ..+..|
|         . . . o.|
|              .  |
|             . . |
|              E  |
+-----------------+

1.先生成秘钥吧!

[root@node4 ~]# ssh-copy-id localhost

[root@node4 ~]# ssh-copy-id 192.168.0.25

[root@node4 ~]# scp ~/.ssh/* 192.168.0.25:~/.ssh/        其实让25主机能够无验证登陆node4主机就行

测试登陆:

[root@node4 ~]# ssh 192.168.0.25                                node4无验证登陆node1(25主机)
Last login: Sun Jul 20 03:20:14 2014 from 192.168.0.1
[root@node1 ~]# ssh 192.168.0.28                                node1(25)主机无验证登陆node4(28主机)
Last login: Sun Jul 20 06:00:26 2014 from 192.168.0.1
[root@node4 ~]# logout

2.本地备份主机编写备份脚本:

# vim rsync.sh
#!/bin/bash
remotedir=/home/backup                备份到远程主机的目录(必须存在哦)
basedir=/backup/                            本地需要备份的目录
host=192.168.0.28                            备份到远程主机
id=root                                                使用root身份(当然也可以使用普通用户身份,记得无验证哦)
rsync -av -e ssh $basedir ${id}@${host}:${remotedir}

#chmod 700 rsync.sh

#./rsync.sh
back.sh
mk.sh
rsync.sh
sent 2288 bytes  received 73 bytes  277.76 bytes/sec
total size is 2056  speedup is 0.87

看到这个你就可以确定备份成功了

远程主机查看一下:

[root@node4 backup]# ls
back.sh  mk.sh  rsync.sh  weekly

成功备份咯!!!!


现在,你可以放心的将此脚本应用在你的环境了,为了保障系统的实时备份,此时,可以将脚本加入crontab中

[root@node1 backup]# crontab -e

*/5 * * * * /backup/rsync.sh > /dev/null

ok,现在就会每5分钟进行一次远程备份了!




你可能感兴趣的:(rsync,远程备份,ssh无密码登陆)