1 Star 1 Fork 0

lavenliu/MyPy3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bak_and_rsync.py 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
lavenliu 提交于 2024-06-03 16:39 . 新增代码
__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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lavenliu/my-py3.git
git@gitee.com:lavenliu/my-py3.git
lavenliu
my-py3
MyPy3
main

搜索帮助