因为每次在使用 mysqldump
进行备份是都要输入账号密码进行登录, 这时候需要人工输入密码来验证。
由于我们的脚本的无人值守的,所以要去掉输入密码这个步骤,这里使用 mysql_config_editor
来将登录凭证加密保存在本地,然后通过脚本传入参数让 mysqldump
通过凭证去登录,免去每次都输入密码的烦恼。
mysql_config_editor 配置步骤:
mysql_config_editor set --login-path=back --host=localhost --user=localuser --password
输入上述命令,会提示输入密码,将 localuser
对应密码输入进去,就保存了一个名字为 back
的登录凭证,以后就可以直接用 back
来进行登录或者备份啦。
还可以输入 mysql_config_editor print --all
来输入所有当前用户保存的凭证,不过密码还是以星号显示的,不会泄露。
1 2 3 4
| [local] user = back password = ***** host = localhost
|
创建完登录凭证以后就可以用脚本备份 mysql
了。
代码如下(目前只适用于 Linux
系统):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| import os import subprocess import time
import oss2
import requests
def execute(command): """ 执行命令
:param command: str 命令字符串 :return: str 命令执行后的返回值,标准输出和标准错误 """ result = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE, encoding='utf8') out, err = result.communicate() return result.returncode, out, err
def rm_file(file_name): """ 删除文件
:param file_name: 需要删除的文件路径 :return: None """ execute(f'rm {file_name}')
def back_mysql(): """ 执行备份逻辑
:return: 错误信息标题和错误信息具体内容 """ file_name = f'back-{time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())}.sql' return_code, out, err = execute(command=f'mysqldump --login-path=local --all-databases > "{file_name}"') if not return_code: if os.path.exists(file_name): return_code, out, err = execute(f'7z a -p123456 "{file_name}.7z" "{file_name}"') if not return_code: print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} 备份成功') rm_file(file_name=file_name) return upload_file(f'{file_name}.7z') else: return '压缩失败', f'压缩失败,错误信息为:\n\nerr:{err}\n\nout:{out}' else: return '备份成功', '备份成功,但文件不存在' else: return '数据库备份出错', f'mysqldump 备份出错,错误信息为\n\nerr: {err}\n\nout:{out}'
def upload_file(file_name): """ 上传文件
:param file_name: 需要上传的文件名 :return: 错误信息标题和错误信息内容 """ auth = oss2.Auth('LxxxxxxG', 'SxxxxxxY') bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'xxxxxx') result = bucket.put_object_from_file(f'xxxxx/{file_name}', file_name) if result.status == 200: print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} 上传成功!') rm_file(file_name) return else: print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} 上传失败!') return '上传失败!', f'错误状态码为:{result.status}'
def send_message(text, desp): """ 发送到微信通知
:param text: 消息标题 :param desp: 消息内容 :return: None """ url = 'https://sc.ftqq.com/SCUxxxxxxa.send' data = { 'text': text, 'desp': desp } requests.post(url=url, data=data) print(data)
if __name__ == '__main__': message = back_mysql() if message: send_message(*message)
|
更改脚本中的 AccessKey
等信息之后就可以将脚本添加到 Linux
的计划任务中了,设置一个定时运行的时间点就可以啦,不过添加到计划任务之后要注意路径问题哦。
参考链接:
https://docs.python.org/zh-cn/3.7/library/subprocess.html?highlight=subprocess#module-subprocess
https://sc.ftqq.com/
本文章首发于个人博客 LLLibra146’s blog
本文作者:LLLibra146
更多文章请关注公众号 (LLLibra146):![LLLibra146]()
版权声明:本博客所有文章除特别声明外,均采用 © BY-NC-ND 许可协议。非商用转载请注明出处!严禁商业转载!
本文链接:
https://blog.d77.xyz/archives/2fc7f40b.html