- 论坛徽章:
- 2
|
简单介绍:自动生成rsync配置文件和同步所需要的密码文件
操作说明:交互过程有提示,只需要输入“模块名”和“备份路径”
脚本内容如下:
希望大家给些改进意见
#!/usr/bin/python
'''The rsync_server config'''
import os
import stat
import sys
import time
now=time.strftime('%Y%m%d%H%M')
print 'First,Please Install Xinetd And Rsync Server \n'
#A function definition of xinetd-rsync
def cfgxrs():
cfgxrsync='''service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon --config=/etc/rsyncd.conf
log_on_failure += USERID
}'''
if os.path.exists('/etc/xinetd.d/rsync'):
cmd_mv='mv /etc/xinetd.d/rsync /etc/xinetd.d/rsync.%s' % (now)
os.system(cmd_mv)
content=file('/etc/xinetd.d/rsync','w')
content.write(cfgxrsync)
content.close()
#A function definition of password-file
def cfgpwd():
cfgpasswd='''ning:123456'''
content=file('/etc/.rsyncd.passwd','w')
content.write(cfgpasswd)
content.close()
os.chmod('/etc/.rsyncd.passwd', stat.S_IREAD)
#A function definition of rsyncd
def cfgsrv():
cfgrsyncd='''#rsyncd#
uid = root
gid = root
use chroot = no
max connections = 5
lock file = /var/log/rsync/rsyncd.lock
log file = /var/log/rsync/rsyncd.log
pid file = /var/log/rsync/rsyncd.pid
hosts allow = 127.0.0.1 192.168.60.134
hosts deny = *
ignore errors
read only = yes
list = no
auth users = ning
secrets file = /etc/.rsyncd.passwd
transfer logging = ture \n'''
if os.path.exists('/etc/rsyncd.conf'):
cmd_mv='mv /etc/rsyncd.conf /etc/rsyncd.conf.%s' % (now)
os.system(cmd_mv)
if os.path.exists('/var/log/rsync'):
print '/var/log/rsync is already established.'
else:
os.mkdir('/var/log/rsync')
content=file('/etc/rsyncd.conf','w')
content.write(cfgrsyncd)
content.close()
#A function definition of '/etc/rsyncd.conf' is module append
def cfgapd():
try:
try:
content=file('/etc/rsyncd.conf','a')
mod=raw_input('please enter the module: \n>')+'\n'
content.write(mod)
pth='path='+raw_input('please enter the path:> \n>')+'\n'
content.write(pth)
content.close()
except:
print 'Please enter a correct Module and Path \n'
sys.exit()
print 'Scripting termination !! \n'
finally:
content.close()
#Display file content
def cfgdpy():
print '\nPrint ''/etc/rsyncd.conf'' content \n'
time.sleep(1)
content=file('/etc/rsyncd.conf','r')
while True:
line = content.readline()
if len(line) == 0:
break
print line,
content.close()
def srvrst():
print '\nexec /etc/init.d/xinetd reload'
cmd='/etc/init.d/xinetd reload'
time.sleep(2)
os.system(cmd)
#Function call
cfgxrs()
cfgsrv()
cfgpwd()
cfgapd()
cfgdpy()
srvrst() |
|