#!/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)