Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python3 backup MySQL/MariaDB (local + FTP)

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

[preparatory work]

Build FTP server: Windows Server 2008 R2 build FTP service

If the local access report 530 error: 1, check the user name, password; 2, check the user rights of the ftp directory.

If the remote host is not accessible, check the firewall.

About the port of ftp: the control port is 21; the data port is 20 in active mode (PORT) and uncertain in passive mode (PASV).

Turn on passive mode: configure the port in FTP Firewall support, and then restart the Microsoft FTP Service service. Note that the data box of the specific ftp site is grayed out and needs to be configured in the IIS master site.

[code]

The following code is feasible for testing MySQL 5.7.23 under MariaDB 10.1.18 under Windows.

# encoding: utf-8# author: walker# date: 2018-08-02 # summary: Python3 backup MySQL/MariaDB (local + FTP) A library, a file, import osimport timeimport pymysqlimport pprintimport psutilfrom ftplib import FTPDBHost = r'127.0.0.1'DBPort = 3306DBUser = rroomroot' DBPwd = r'password'# LocalBakRoot = homewalkerapapsqlbackup bak' # local backup directory LocalBakRoot = ritual D:\ sql_bak' # local backup directory LocalIP =''FtpHost = rroom192.168.xx.xx' # FTP host FtpBakRoot = rroomsqlroombak' # FTP directory FtpUser = rroomftpadmin' FtpPwd = rroompassword' # ignore the system library IgnoreDB = {'information_schema' 'mysql', 'performance_schema' 'sys'} # get IPdef GetLocalIPByPrefix (prefix) according to the prefix: localIP =' 'dic = psutil.net_if_addrs () for adapter in dic: snicList = dic [adapter] for snic in snicList: if not snic.family.name.startswith (' AF_INET'): continue ip = snic.address if ip.startswith (prefix) : localIP = ip return localIP # handles a database def ProcOneDB (dbName): print ('ProcOneDB% s...'% dbName) filename ='% slots% slots% s.sql'% (time.strftime ('% Y% m% d') Time.localtime (), LocalIP, dbName) localFile = os.path.join (LocalBakRoot, filename) # backup database The database creation statement cmd = 'mysqldump-u% s-p% s-B% s >% s'% (DBUser, DBPwd, dbName, localFile) # print ('cmd:% s'% cmd) print ('mysqldump...') is not exported without the-B parameter. Rtn = os.system (cmd) if 0! = rtn: print ('Error: 0! = rtn') return False ftp = FTP () ftp.encoding =' gb18030' ftp.set_pasv (False) ftp.connect (FtpHost, port=21, timeout=10) ftp.login (user=FtpUser Passwd=FtpPwd) print (ftp.getwelcome ()) ftp.cwd (FtpBakRoot) # use sql server ip as the subdirectory name if LocalIP not in ftp.nlst (): ftp.mkd (LocalIP) # create a subdirectory ftp.cwd (LocalIP) with open (localFile, mode='rb') as f: ftp.storbinary ('STOR' + filename F) return True # handles all databases def ProcAllDB (): connDB = pymysql.connect (host=DBHost, port=DBPort, user=DBUser, passwd=DBPwd Charset='utf8mb4') cur = connDB.cursor () sql = "show databases "print ('input sql:' + sql) cur.execute (sql) rowList = cur.fetchall () cur.close () connDB.close () dbList = list () for row in rowList: dbList.append (row [0]) print (' dbList (% d):\ n% s\ n'% (len (dbList), pprint.pformat (dbList) Indent=4)) for dbName in dbList: if dbName in IgnoreDB: continue print ('current time:% s'% time.strftime ('% Y-%m-%d% HRV% MRV% S') Time.localtime ()) ProcOneDB (dbName) print ('current time:% s\ n'% time.strftime ('% Y-%m-%d% HV% MVA% S') Time.localtime ()) if _ _ name__ = ='_ main__': LocalIP = GetLocalIPByPrefix ('192.168.xx.') ProcAllDB ()

[related reading]

Python3 statistics on the number and size of ftp files

Ftplib-FTP protocol client

Psutil

MySQL/MariaDB Tips

Ubuntu Server 18.04vs5.7MySQL

FTP directory synchronization: pyftpsync

* walker * *

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report