#!/bin/env python
Import Paramiko,datetime
Hostname= ' x.x.x.x '
Username= ' Root '
password= ' xxxx '
Port=22
Date = Datetime.datetime.now (). Strftime ('%y-%m-%d ')
Srcpath = '/tmp/database_ ' + date + ' _sql.gz '
Despath = '/data/yupptv-mysqlbackup/database_ ' + date + ' _sql.gz '
Print Despath
def sshexec (cmd):
s = Paramiko. Sshclient ()
S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
S.connect (hostname = Hostname,port = Port,username = Username, password = password)
Stdin,stdout,stderr = S.exec_command (cmd)
S.close
def fileget (src,des):
t = Paramiko. Transport ((hostname,22))
T.connect (Username=username,password=password)
SFTP = Paramiko. Sftpclient.from_transport (t)
Sftp.get (Src,des)
T.close ()
if __name__ = = ' __main__ ':
Sshexec ('/usr/local/mysql/bin/mysqldump-uroot--opt--all-databases | gzip >%s '% srcpath)
FileGet (Srcpath,despath)
Sshexec (' rm%s '% Srcpath)
A MySQL-ready python script