Backing up SQL Server databases to remote host scripts
mssqlbackup.py:
# name: mssqlbackup.py# purpose: ## author: yanglixue## created: 2015/ 04/01#-------------------------------------------------------------------------------Import pymssqlimport datetimeimport loggingimport tracebackimport os log_format = '% (filename) s [% (asctime) s] [% (levelname) s] % (message) s ' log_file= ' c:\\ ' + ' Backup_ ' + Datetime.datetime.now (). Strftime ("%y-%m-%d") + ". Log" Logging.basicconfig (filename=log_file,format= Log_format,level=logging.info) backupdir= "e:\\backup\\" delcmd= "d:\eclipsework\mssqlbackup\del.bat >> c:\\ " + " Del_ " +datetime.datetime.now (). Strftime ("%y-%m-%d ") + ". Log "dbs=[' DB2 ' , ' Test ']try: logging.info ("connect database ...") conn=pymssql.connect (host= ' 10.1.1.2 ', database= ' master ', user= ' Sa ', password= ' [EMAIL&NBsp;protected] ', autocommit=true) cur=conn.cursor () for dbname in dbs: now = datetime.datetime.now () sdate = now.strftime ("%y-%m-%d_%h-%m-%s") bakname=backupdir + dbname + "_" + sdate + ". Bak" sql = "". Join (["backup database [", dbname, "] to dIsk=n ' "+ bakname, "' with noinit,noformat,checksum,stop_on_error " ] ) cur.execute ( SQL) Logging.info (SQL) logging.info ("DB:" + dbname + " OK. ") conn.close () logging.info (Delcmd) out = os.system (delcmd) logging.iNFO (delcmd + " " + str (out)) except exception as ex: logging.error ("Exception:" + traceback.format_exc ())
Del.bat:
echo "Start ..." @echo onxcopy e:\backup\* \\192.168.1.11\E$\dbbackupdel e:\backup\*/q
This article is from the "Days together with it" blog, so be sure to keep this source http://raugher.blog.51cto.com/3472678/1628075
Backing up SQL Server databases to remote host scripts