Python 网站文件及数据库备份脚本

初学Python,试着写了一份python网站文件备份和数据库备份的脚本,功能是写出来了,但感觉还是不太适应,写得不太好,以后还要努力哈!backup.py读取backup.xml的信息,进行相关操作,至于为啥要把信息写在xml里,因为感觉用xml很酷,哈哈没啥的,学习麻,想用就用一下哈。

backup.xml:

<?xml version="1.0"?> 



<config> 




        <backupdir>/backup/</backupdir> 




        <dbbackup id="1"> 




                <host>192.168.1.100</host> 




                <user>root</user> 




                <passwd>123456</passwd> 




                <dbname>test1</dbname> 




        </dbbackup> 




        <dbbackup id="2"> 




                <host>192.168.1.101</host> 




                <user>root</user> 




                <passwd>123456</passwd> 




                <dbname>test2</dbname> 




        </dbbackup> 




        <webbackup id="1"> 




                <dir>/data/www</dir> 




        </webbackup> 




</config> 

backup.py:

#!/usr/bin/env python  


 



from xml.dom import minidom  




import time  




import os  




import re  



 



dom = minidom.parse('backup.xml')  



 



class readxml:  




        def getText(self, node):  




                return node.childNodes[0].data  



 



        def getId(self, node, id):  




                return node.getAttribute(id)  



 



        def getGroupText(self, model):  



                dbinfo = {}  


                xml_groups = dom.getElementsByTagName(model)  



                for group in xml_groups:  




                        groupid = self.getId(group, 'id')  



                        dbinfo_one = {}  



                        for node in group.childNodes:  




                                if node.nodeType == node.ELEMENT_NODE:  




                                        dbinfo_one[node.nodeName] = self.getText(node)  



                                        dbinfo[groupid] = dbinfo_one  



                return dbinfo  



 



class back:  




        def webbackup(self, webgroup):  




                for i in webgroup:  




                        filename = mydate + re.sub('/', '_', webgroup.get(i).get('dir')) + ".tar.gz" 




                        command = "tar zcvfp '%s' %s" % (today + os.sep + filename, webgroup.get(i).get('dir'))  



                        os.system(command)  


                          



        def dbbackup(self, dbgroup):  




                mysqlpath="/usr/local/mysql/bin/mysqldump" 




                for i in dbgroup:  




                        filename = today + os.sep + mydate + "_" + dbgroup.get(i).get('dbname') + ".sql.gz" 




                        command = "%s --opt -u%s -p%s -h%s %s | gzip > %s" % (mysqlpath, dbgroup.get(i).get('user'), dbgroup.get(i).get('passwd'), dbgroup.get(i).get('host'), dbgroup.get(i).get('dbname'), filename)  



                        os.system(command)  


                                      


xmlconfig = readxml()  



backupdir = xmlconfig.getText(dom.getElementsByTagName('backupdir')[0])  




dbbackup = xmlconfig.getGroupText('dbbackup')  




webbackup = xmlconfig.getGroupText('webbackup')  



 



mydate = time.strftime('%Y%m%d')  



today=backupdir + mydate  



if not os.path.exists(today):  



        os.makedirs(today)  


 


back().webbackup(webbackup)  


back().dbbackup(dbbackup) 

相关推荐