|
1. 需求
公司是做在线教育的,所以老师的录课视频是很重要的文件。需要把视频从录课的PC每天同步到文件服务器,并保证文件文件的完整性。
2. 文件同步软件选择
最开始使用的是allway sync。同步速度还不错,大概11m左右,但是有是会同步失败,使用计划任务不是每次都能成功。还有一个问题就是用的太频繁就提示授权,没有找到什么好的破解。
第二个软件是freefilesync。免费软件,使用sftp协议,缺点是速度太慢2m左右,而且因为是win的服务器所以需要安装ssh软件打开22端口。而且也有同步失败的问题。
在测试freefilesync的时候发现了cygwin这个神器,后开干脆考虑是不是使用cygwin的rsync配合计划任务同步视频文件。测试结果很满意速度16m左右,配合计划任务使用了两周左右没有发现同步失败的现象。
3. 环境要求
文件服务器安装cygwin,并安装openssh和rsync服务
录课PC安装cygwin并安装openssh服务,打开22端口
配置录课室ssh信任文件服务器
怎么使用cygwin并打开openssh百度资料有不少很容易找到
4. python脚本介绍
其实脚本比较简单大概分为主文件、日志函数、邮件函数以及配置文件几部分。使用计划任务定期执行,可以做到自动同步文件,判断同步执行与否,执行结果写入日志,执行不成功发邮件给运维人员排查故障。
5. 时序图
6. 脚本源码及说明
1)主文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #!/usr/bin/env python
#coding:utf-8
#auther:Bran Guo
#date:11/07/2015
#description:视频文件同步脚本,添加定时任务后自动运行
#version:V1.0
import os,ConfigParser,logger
conf = ConfigParser.ConfigParser()
conf.read("bakconf.ini")
dst_user = conf.get('destination','dst_user')
dst_ip = conf.get('destination','dst_ip')
dst_dir = conf.get('destination','dst_dir')
src_dir = conf.get('source','src_dir')
ret = os.system('rsync -Paz %s@%s:%s %s' %(dst_user,dst_ip,dst_dir,src_dir))
logger.logger(ret)
os.system('chmod -R 777 %s/* ' % src_dir)
exit()
|
特别简单的一个脚本就是使用configparser读取配置文件然后使用rsync同步目录,最后修改权限和写日志。
2)日志文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| #!/usr/bin/python
import datetime,os,ConfigParser,sendmail
conf = ConfigParser.ConfigParser()
conf.read("bakconf.ini")
dst_ip = conf.get('destination','dst_ip')
bak_log = conf.get("log_file","bak_log")
sender = conf.get('mail','sender')
receiver = conf.get('mail','receiver')
subject = conf.get('mail','subject')
smtpserver = conf.get('mail','smtpserver')
username = conf.get('mail','username')
password = conf.get('mail','password')
def logger(ret):
if ret == 0:
echo_line = "%s\tBackup video file succes\n" % datetime.datetime.now()
else:
echo_line = "%s\tBackup video file failed, plz check.\n" % datetime.datetime.now()
sendmail.sendmail(sender,receiver,subject,smtpserver,username,password,echo_line,dst_ip)
f = file(bak_log,'a')
f.write(echo_line)
f.flush()
f.close()
|
同样的读取配置文件,然后通过主文件传给的ret参数盘算命令执行结果,然后做相应的操作。从sender到password的参数是邮件的配置参数。
3)邮件文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| import smtplib,datetime
from email.mime.text import MIMEText
def sendmail(sender,receiver,subject,smtpserver,username,password,content,dsthost):
time = str(datetime.datetime.now)
msg = MIMEText(
'''
<body>
HOST: %s <br/>
DESCRIPON: %s <br/>
</body>
<pre>
''' %(dsthost,content),'html','ascii')
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = subject
smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
|
使用smtplib模块的函数,所有参数都是在logger中读取的配置文件赋值的,msg是邮件内容。
4)配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [source]
src_dir = /cygdrive/d/video
[destination]
dst_user = destination host username
dst_ip = destination ip address
dst_dir = /cygdrive/d/video/
[log_file]
bak_log = backuplog.txt
[mail]
sender = send@xxx.com
receiver = receive@xxx.com
subject = backup video failed
smtpserver = smtp.exmail.qq.com
username = send@xxxi.com
password = password
|
配置文件需要注意的地方就是源路径和目的路径要是使用/cygdrive/d/video而不要用windows风格的d:\video。
5)目录结构
7.遇到的坑
配置文件中的用户要和录课PC中启动sshd服务的用户一直否则会报错。
不要使用Administrator,使用名全是小写的用户,linux是严格区分大小写的所以会出问题,ssh信任验证过不去。
文件服务器要修改PATH路径把cgywin的目录放到默认目录的前边这样可以优先使用cygwin的命令,或者只用全路径。
配置计划任务的时候的 ‘起始于’ 一定要写脚本所在的目录,否则会报错。
|
|