代码拉取完成,页面将自动刷新
__author__ = "lcc@lavenliu.cn"
import os
import smtplib
import subprocess
from datetime import datetime
from email.mime.text import MIMEText
MAIL_HOST = 'smtp.exmail.qq.com'
MAIL_PORT = 465
MAIL_USER = 'zabbix@lavenliu.cn'
MAIL_PASS = 'xxxxxxxxxx'
class BakAndRsync(object):
def __init__(this, target, target_dir):
this.target = target
this.target_dir = target_dir
# these info can be written in a config file,
# otherwise, it looks so ugly.
this._rsync_user = ' rsync_backup'
this._rsync_pass = ' --password-file=/etc/rsync.password'
this._mysql_user = ' -u root '
this._mysql_pass = ' -p admin12345 '
this._mysql_opts = ' --use-savepoints -G -E -R --regex '
this._mysql_excludes = '\'^(?!(mysql\.|test\.|information_schema\.|performance_schema\.))\''
this._mydumper = '/usr/local/bin/mydumper'
this._hostname = os.uname()[1]
this._local_tmp_dir = datetime.now().strftime("%Y%m%d-%H%M%S")
this._mail_content = os.uname()[1]
def _mydumper_cmd(this):
return this._mydumper + this._mysql_user + \
this._mysql_pass + this._mysql_opts + \
this._mysql_excludes + ' -o ' + this._local_tmp_dir
def _rsync_cmd(this):
return '/usr/bin/rsync -avz ' + this._local_tmp_dir + \
this._rsync_user + '@' + this.target + \
'::' + this.target_dir + this._rsync_pass
def _delete_tmp_cmd(this):
return 'rm -rf ' + this._local_tmp_dir
def backup(this):
ret = this.do(this._mydumper_cmd())
if ret == 0:
this._mail_content += '\n1. db backup successfully\n'
print('bak successfully')
else:
this._mail_content += '\n1. db backup failure\n'
print('bak failure')
def rsync(this):
ret = this.do(this._rsync_cmd())
if ret == 0:
this._mail_content += '\n2. db sync successfully\n'
print('rsync successesfully')
else:
this._mail_content += '\n2. db sync failure\n'
print('rsync failure')
def do(this, what):
ret = subprocess.call([what], shell=True)
return ret
def send_mail(this, mail_to, subject, content):
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = MAIL_USER
msg['To'] = mail_to
smtp = smtplib.SMTP_SSL(MAIL_HOST, port=MAIL_PORT)
smtp.login(MAIL_USER, MAIL_PASS)
smtp.sendmail(MAIL_USER, mail_to, msg.as_string())
smtp.close()
def tear_down(this):
ret = this.do(this._delete_tmp_cmd())
if ret == 0:
this._mail_content += '\n3. delete temp dir successfully\n'
print('delete successfully')
else:
this._mail_content += '\n4. delete temp dir failure\n'
print('delete failure')
this.send_mail('devops@lavenliu.cn',
this._hostname,
this._mail_content)
if __name__ == '__main__':
br = BakAndRsync('192.168.1.16', 'backup33')
br.backup()
br.rsync()
br.tear_down()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。