Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2b6d7d

Browse files
committed
add codescripts
1 parent c994805 commit a2b6d7d

File tree

4 files changed

+190
-4
lines changed

4 files changed

+190
-4
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
wordpress 备份脚本
6+
备份网站,添加apache伪静态支持.htaccess,备份数据库
7+
网站备份位置:/data/backup/wordpress_backup
8+
#备份全部wordpress网站
9+
python backup_wp_site.py
10+
#备份指定wordpress网站
11+
python backup_wp_site.py -f filename(每行一网站 www开头)
12+
'''
13+
import os
14+
import shutil
15+
import sys
16+
from string import Template
17+
import re
18+
from optparse import OptionParser
19+
20+
21+
wp_conf_path = '/usr/local/nginx/conf/vhosts/wordpress'
22+
backup_site = '/data/backup/wordpress_backup'
23+
wp_path = '/data/wwwroot/wordpress'
24+
dbrootpass = '8pIi5crkPpUbLazs'
25+
26+
27+
28+
with open('htaccess','r') as htf:
29+
ht_con = htf.read()
30+
31+
32+
33+
34+
def writeHtaccess(path,d=dict()):
35+
global ht_con
36+
con = Template(ht_con)
37+
con = con.safe_substitute(d)
38+
with open(path,'w+') as ff:
39+
ff.write(con)
40+
41+
def is_exist(path):
42+
if os.path.exists(path):
43+
return path
44+
else:
45+
print '%s has not found!' % path
46+
sys.exit(1)
47+
48+
49+
def getDbInfo(path):
50+
51+
'''from config get db info'''
52+
53+
if os.path.isfile(path):
54+
with open(path) as f:
55+
con = f.read()
56+
user_pattern = r'\s+fastcgi_param USERNAME\s+(.*);'
57+
user_pattern_obj = re.compile(user_pattern)
58+
dbuser = user_pattern_obj.findall(con)[0]
59+
db_pattern = r'\s+fastcgi_param DATABASE\s+(.*);'
60+
db_pattern_obj = re.compile(db_pattern)
61+
dbname = db_pattern_obj.findall(con)[0]
62+
pass_pattern = r'\s+fastcgi_param PASSWORD\s+(.*);'
63+
pass_pattern_obj = re.compile(pass_pattern)
64+
dbpass = pass_pattern_obj.findall(con)[0]
65+
return [dbname,dbuser,dbpass]
66+
return None
67+
68+
69+
70+
def backup_one_site(site):
71+
72+
'''backup one site'''
73+
74+
if site.startswith('www'):
75+
site_src = is_exist(os.path.join(wp_path,site + '/webroot'))
76+
site_dest = os.path.join(backup_site,site)
77+
if not os.path.isdir(site_dest):
78+
shutil.copytree(site_src,site_dest)
79+
ht_site_dest = os.path.join(site_dest,'.htaccess')
80+
writeHtaccess(ht_site_dest,dict(host=site[4:]))
81+
conf_src = is_exist(os.path.join(wp_conf_path,site + '.conf'))
82+
info = getDbInfo(conf_src)
83+
if not info:
84+
print 'database infomation not found'
85+
sys.exit(1)
86+
print info
87+
dump_command = 'mysqldump -uroot -p%s %s > %s' % (dbrootpass,info[0],os.path.join(site_dest,info[0]+'.sql'))
88+
os.system(dump_command)
89+
wp_list = '%s | %s\n' % (site,' | '.join(info))
90+
with open('wp_list_info','w+') as wpinfo:
91+
wpinfo.write(wp_list)
92+
93+
94+
95+
def backup_all():
96+
97+
'''backup all wordpress site'''
98+
99+
for site in os.listdir(wp_path):
100+
backup_one_site(site)
101+
sys.exit(1)
102+
103+
104+
def walk_backup_site(infile):
105+
106+
'''backup site in file infile'''
107+
108+
if not os.path.isfile(infile):
109+
print 'file %s not found!' % infile
110+
sys.exit(1)
111+
112+
for site in file(infile):
113+
site = site.strip()
114+
backup_one_site(site)
115+
116+
117+
if __name__ == '__main__':
118+
print __doc__
119+
usage = "usage: %prog [option]"
120+
parse = OptionParser(usage=usage)
121+
parse.add_option('-f','--file',dest='filename',default='all',help='read data from filename')
122+
parse.add_option('-p','--password',dest='passwd',help='mysql root password')
123+
(opt,args) = parse.parse_args()
124+
if opt.passwd:
125+
dbrootpass = opt.passwd
126+
if not opt.filename == 'all':
127+
walk_backup_site(opt.filename)
128+
else:
129+
backup_all()

webapp/requirement.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mysql-connector-python
2+
jinja2
3+
MySQL-python

webapp/www/transwarp/db.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ def delete(table,**kw):
159159
sql = 'delete from `%s` where %s' % (table,where)
160160
return _update(sql,*args)
161161

162-
@with_connection
163-
def select(sql,first,*args):
162+
163+
164+
def _select(sql,first,*args):
164165

165166
global _db_ctx
166167
cursor = None
@@ -172,7 +173,6 @@ def select(sql,first,*args):
172173
names =[x[0] for x in cursor.description]
173174
if first:
174175
values = cursor.fetchone()
175-
print values
176176
if not values:
177177
return None
178178
return Dict(names,values)
@@ -182,7 +182,29 @@ def select(sql,first,*args):
182182
cursor.close()
183183

184184

185+
@with_connection
186+
def select_one(sql,*args):
187+
'''
188+
select_one('select * from user where id = ?','1')
189+
'''
190+
return _select(sql,True,*args)
191+
185192

193+
@with_connection
194+
def select_int(sql,*args):
195+
196+
d = _select(sql,True,*args)
197+
if len(d) != 1:
198+
raise MuiltiColumnsError('Expect only one column.')
199+
return d.values()[0]
200+
201+
202+
@with_connection
203+
def select(sql,*args):
204+
'''
205+
select('select * from user where id = ?','1')
206+
'''
207+
return _select(sql,False,*args)
186208

187209

188210
if __name__ == '__main__':
@@ -194,4 +216,5 @@ def select(sql,first,*args):
194216
# print insert('user',id='2',name='lo')
195217
# print update('update user set name = ? where id = ?','good','1')
196218
# print delete('user',id='1')
197-
print select('select * from user',False)
219+
print select_one('select * from user where id = ?','1')
220+
print select('select * from user where id = ?','1')

webapp/www/transwarp/orm.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = 'coolcoding'
5+
6+
import time,logging
7+
8+
class Model(dict):
9+
10+
11+
def __init__(self,**kw):
12+
13+
super(Model,self).__init__(**kw)
14+
15+
16+
def __getattr__(self,key):
17+
try:
18+
return self[key]
19+
except KeyError:
20+
raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
21+
22+
23+
def __setattr__(self,key,value):
24+
25+
self[key] = value
26+
27+
28+
29+
if __name__ == '__main__':
30+
m = Model()
31+
print m.user

0 commit comments

Comments
 (0)