同步目录下文件脚本

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python

#运行:rsync_img.py today | yesterday

import os
import time
import sys

s_dirs=['/zz_data/www/Krptos-cms-php/public/uploads','/opt/tomcat/webapps/uploads']
t_dir='/zz_data/www/img/uploads'

today=time.strftime('/%Y/%m/%d',time.localtime(time.time()))
yesterday=time.strftime('/%Y/%m/%d',time.localtime(time.time() - 24*60*60) )
d={'today':today,'yesterday':yesterday}
def rsync(s_dir,t_dir,_day):
sd='%s%s'%(s_dir,_day)
td='%s%s'%(t_dir,_day)
now=time.strftime("%Y-%m-%d %X",time.localtime())
if not os.path.exists(sd) or not os.listdir(sd):
sys.exit()
if not os.path.exists(td):
os.makedirs(td)
_cmd='/usr/bin/rsync -avz %s/* %s'%(sd,td)
try:
os.system(_cmd)
except:
out=open('/zz_data/logs/rsync.err','a')
out.write('rsync failed at %s\n'%now)
out.close()
def main():
if len(sys.argv)<2:
print 'need args today or yesterday\n'
sys.exit()
if sys.argv[1] not in ['today','yesterday']:
print 'args must be today or yesterday\n'
sys.exit()
for s_dir in s_dirs:
rsync(s_dir,t_dir,d.get(sys.argv[1]))
if __name__=='__main__':
main()