一.ssh 信任的建立
//用于双机直接相互传输问题免密码
1.更改两个节点上hosts文件(修改hosts权限需要进入root帐户)
[root@DELL ~]#vi /etc/hosts
///////////////////////////////////
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 node1 node1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.0.154 file_server
192.168.0.153 file_server_bak
///////////////////////////////////
:wq
2.//生成ssh本机的密钥
//三次回车后完成(先切换回magic帐户)
[root@DELL .ssh]# su magic
[magic@DELL .ssh]$ cd ~
[magic@DELL ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/magic/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/magic/.ssh/id_rsa.
Your public key has been saved in /home/magic/.ssh/id_rsa.pub.
The key fingerprint is:
f3:84:b3:14:b8:ab:ba:e7:3a:1e:98:72:0b:4e:fe:f6 magic@DELL
[magic@DELL ~]$
3.进入到用户的主目录下,默认为root用户的主目录
#cd /root
使用ls -al 察看是否是否存在.ssh这个文件夹
可以看到两个文件
[magic@DELL ~]$ cd /home/magic/.ssh/
[magic@DELL .ssh]$ ls -al
total 20
drwx—— 2 magic magic 4096 Apr 13 21:53 .
drwx—— 6 magic magic 4096 Apr 9 01:37 ..
-rw——- 1 magic magic 1675 Apr 13 21:53 id_rsa
-rw-r–r– 1 magic magic 402 Apr 13 21:53 id_rsa.pub
-rw-r–r– 1 magic magic 395 Feb 26 01:13 known_hosts
[magic@DELL .ssh]$
其中id_rsa为私钥,id_rsa.pub为公钥。
4.登陆file_server、file_server_bak分别执行以上三步
5.
//重新登陆到file_server上
//将公钥改名,并添加到另一台电脑的公钥内。
#cd /home/magic/.ssh/
# mv id_rsa.pub ha1
//修改公钥的姓名
[magic@DELL .ssh]$ scp ha1 magic@192.168.0.153:/home/magic/.ssh/
//把公钥传输到file_server_bak上
6.
//登陆到file_server_bak上
[magic@DELL ~]$ cd /home/magic/.ssh/
[magic@DELL .ssh]$ ls
ha1 id_rsa id_rsa.pub
[magic@DELL .ssh]$ cat ha1 >> id_rsa.pub
//将file_server的密钥添加到file_server_bak的公钥中去
[magic@DELL .ssh]$ mv id_rsa.pub authorized_keys
// AuthorizedKeysFile 指令指定了用于公钥认证的公钥文件(默认 ~/.ssh/authorized_keys)位置,每行一个公钥。此文件中空行和以’#'开头的行将被当作注释忽略
//修改文件名陈为ssh可以识别的key文件authorized_keys
7.
//将密钥传回到file_server上,
[magic@DELL .ssh]$ scp authorized_keys magic@192.168.0.154:/home/magic/.ssh/
二、配置rsync
1.在file_server上安装rsync程序(大部分linux默认已经安装,此步骤可跳过)
tar -zxvf rsync-3.0.4.tar #解开压缩包
cd rsync-3.0.4
./configure –prefix=/usr/local/rsync
编译与安装
make
make install
2.在/etc下创建rsync.conf文件
vi /etc/rsyncd.conf
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[file_server]
comment = replication
uid = magic
gid = magic
path = /opt/user_files
ignore errors
read .ly = no
write .ly = no
list= no
auth users = magic
secrets file = /etc/rsyncd.secrets
3.在/etc下创建/etc/rsyncd.secrets 文件
vi /etc/rsyncd.secrets
此文件格式为 用户名:密码
magic:12345678
4.在/etc/services 添加一行(默认已有,跳过)
rsync 873/tcp
5.添加环境变量,在/etc/profile添加一行(跳过)
#echo “export PATH=$PATH:/usr/local/rsync/bin” >> /etc/profile
#source /etc/profile
6.启动rsync服务
#rsync –daemon
7.让服务器开机的时候自启动
echo “rsync –daemon” >> /etc/rc.local
8.在file_server_bak上做相应的配置(以上7步)
9.在file_server上增加执行文件
vi /opt/file_auto_backup
内容如下:
#!/bin/sh
# This does file_server backup to a file_server_bak.
# directory to backup
DIR=/opt/user_files/
# the name of the primary
SERVER=file_server
##############################################
OPTS=”-rlptgoDvHS –progress –delete –force”
# now the actual transfer
rsync $OPTS magic@$SERVER:$DIR $DIR
10.添加到定时任务里
crontab -e
*/5 * * * * /opt/file_auto_backup