-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackup_wp_site.py
More file actions
129 lines (97 loc) · 3.49 KB
/
backup_wp_site.py
File metadata and controls
129 lines (97 loc) · 3.49 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
wordpress 备份脚本
备份网站,添加apache伪静态支持.htaccess,备份数据库
网站备份位置:/data/backup/wordpress_backup
#备份全部wordpress网站
python backup_wp_site.py
#备份指定wordpress网站
python backup_wp_site.py -f filename(每行一网站 www开头)
'''
import os
import shutil
import sys
from string import Template
import re
from optparse import OptionParser
wp_conf_path = '/usr/local/nginx/conf/vhosts/wordpress'
backup_site = '/data/backup/wordpress_backup'
wp_path = '/data/wwwroot/wordpress'
dbrootpass = '8pIi5crkPpUbLazs'
with open('htaccess','r') as htf:
ht_con = htf.read()
def writeHtaccess(path,d=dict()):
global ht_con
con = Template(ht_con)
con = con.safe_substitute(d)
with open(path,'w+') as ff:
ff.write(con)
def is_exist(path):
if os.path.exists(path):
return path
else:
print '%s has not found!' % path
sys.exit(1)
def getDbInfo(path):
'''from config get db info'''
if os.path.isfile(path):
with open(path) as f:
con = f.read()
user_pattern = r'\s+fastcgi_param USERNAME\s+(.*);'
user_pattern_obj = re.compile(user_pattern)
dbuser = user_pattern_obj.findall(con)[0]
db_pattern = r'\s+fastcgi_param DATABASE\s+(.*);'
db_pattern_obj = re.compile(db_pattern)
dbname = db_pattern_obj.findall(con)[0]
pass_pattern = r'\s+fastcgi_param PASSWORD\s+(.*);'
pass_pattern_obj = re.compile(pass_pattern)
dbpass = pass_pattern_obj.findall(con)[0]
return [dbname,dbuser,dbpass]
return None
def backup_one_site(site):
'''backup one site'''
if site.startswith('www'):
site_src = is_exist(os.path.join(wp_path,site + '/webroot'))
site_dest = os.path.join(backup_site,site)
if not os.path.isdir(site_dest):
shutil.copytree(site_src,site_dest)
ht_site_dest = os.path.join(site_dest,'.htaccess')
writeHtaccess(ht_site_dest,dict(host=site[4:]))
conf_src = is_exist(os.path.join(wp_conf_path,site + '.conf'))
info = getDbInfo(conf_src)
if not info:
print 'database infomation not found'
sys.exit(1)
print info
dump_command = 'mysqldump -uroot -p%s %s > %s' % (dbrootpass,info[0],os.path.join(site_dest,info[0]+'.sql'))
os.system(dump_command)
wp_list = '%s | %s\n' % (site,' | '.join(info))
with open('wp_list_info','w+') as wpinfo:
wpinfo.write(wp_list)
def backup_all():
'''backup all wordpress site'''
for site in os.listdir(wp_path):
backup_one_site(site)
sys.exit(1)
def walk_backup_site(infile):
'''backup site in file infile'''
if not os.path.isfile(infile):
print 'file %s not found!' % infile
sys.exit(1)
for site in file(infile):
site = site.strip()
backup_one_site(site)
if __name__ == '__main__':
print __doc__
usage = "usage: %prog [option]"
parse = OptionParser(usage=usage)
parse.add_option('-f','--file',dest='filename',default='all',help='read data from filename')
parse.add_option('-p','--password',dest='passwd',help='mysql root password')
(opt,args) = parse.parse_args()
if opt.passwd:
dbrootpass = opt.passwd
if not opt.filename == 'all':
walk_backup_site(opt.filename)
else:
backup_all()