环境:vbox+centos6.6
实现在客户端(192.168.1.234)以student身份,每天定时以rsync配合ssh取得主机(192.168.1.211)的/etc,/root/,home三个目录的镜像到客户端的/backups/下面.
制作不用密码登录的SSH用户
1)客户端建立两把钥匙:
[student@clientlinux ~]$ ssh-keygen
(下面可以分别按三次enter)
在~/.ssh中得到/home/student/.ssh/id_rsa(私钥文件)、/home/student/.ssh/id_rsa.pub
切记,~/.ssh和id_rsa的权限必须如下:
drwx------. 2 student student 4096 2011 -07-25 12:58 /home/student/.ssh
-rw-------. 1 student student 1675 2011 -07-25 12:58 id_rsa
-rw-r--r--. 1 student student 416 2011 -07-25 12:58 id_rsa.pub
[student@clientlinux ~]$ scp ~/.ssh/id_rsa.pub root@192.168.1.211:~
3)将公钥放置到服务器端的正确目录与文件里
公钥数据要附加到authorized_keys这个文件里
建立~/.ssh,权限需要为700
[root@www ~]# mkdir .ssh; chmod 700 .ssh
[root@www ~]# cat id_rsa.pub >> .ssh/authorized_keys
[root@www ~]# chmod 644 .ssh/authorized_keys
以上,就完成了student(192.168.1.234)免密码ssh登录root(192.168.1.211)……
提前建好/backups,使student可读写
[student@clientlinux ~]$ mkdir ~/bin ; vim ~/bin/backup_www.sh
#!/bin/bash
localdir=/backups
remotedir="/etc /root /home"
remoteip="192.168.1.211"
[ -d ${localdir} ] || mkdir ${localdir}
for dir in ${remotedir}
do
rsync -av -e ssh root@${remoteip}:${dir} ${localdir}
done
[student@clientlinux ~]$ chmod 755 ~/bin/backup_www.sh
建立日常任务
[student@clientlinux ~]$ crontab -e
0 2 * * * /home/student/bin/backup_www.sh