rsyncd服务器配置实例
1、在服务器(hosta:192.168.40.20)上创建rsyncd的配置文件/etc/rsyncd.conf
hosta#vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup
ignore errors
read only = true
list = false
hosts allow = 192.168.40.21
hosts deny = 0.0.0 .0/32
auth users = netsword,root
secrets file = /etc/rsyncd.secrets
2、创建密码文件/etc/rsyncd.secrets(确保rsyncd的密码与/etc/passwd不同)
hosta#vi /etc/rsyncd.secrets
netsword:xiaobai
root:123456
hosta#chown root /etc/rsyncd.secrets
hosta#chmod 600 /etc/rsyncd.secrects --确保只有root可读写密码文件
3、启动rsyncd服务
4、在客户端(hostb:192.168.40.21)测试
hostb#rsync -avlP --progress [email protected]::backup /home/backup
Password:123456 --输入/etc/rsyncd.secrets中的密码
Rsync+SSH---让Server自动异地备份
例:在主机hosta(192.168.40.20)上的/bak目录自动对主机hostb(192.168.40.21)上的/backup目录做备份且不需要密码
解决办法:
1、在hosta上产生public/private dsa密钥对
hosta#cd /root/.ssh
hosta#ssh-keygen -d
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): <-- 此处不打passphrase..下次才不会询问password
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
11:22:33:44:55:66:77:88:99:00:11:22:33:44:55:66 [email protected]
2、将hosta上/root/.ssh/id_dsa.pub上传到hostb的/root/.ssh上且更名为authorized_keys2
hosta#scp id_dsa.pub 192.168.40.21:/root/.ssh/authorized_keys2
[email protected]'s password: ---输入密码
3、在hostb上启动rsyncd服务
hostb#chkconfig rsync on
4、测试rsnync能否成功
hosta#rsync -avlR -e ssh [email protected]:/backup/ /bak/ --backup
注:-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-R, --relative 使用相对路径信息
-l, --links 保留符号链接
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-r, --recursive 对子目录以递归模式处理
--backup, 在覆盖前备份文件
5、创建sync启动脚本
hosta#vi /etc/init.d/sync.sh
#/bin/bash
/usr/bin/rsync -avlR -e ssh [email protected]:/bakcup/ /bak/
6、使用crontab 来做自动排程(让hosta每天0时0分自动对hostb进行备份)
hosta#crontab -e
0 0 * * * /etc/init.d/sync.sh
7、自启动crond服务
hosta#chkconfig crond on