Python 数据库备份脚本

import datetime
import os
import subprocess
# 数据库备份目录
BACKUP_DIR = '/path/to/backup'
# 备份文件保留周期(天)
RETENTION_PERIOD = 7
# 备份数据库
def backup_database():
    current_time = datetime.datetime.now()
    backup_file = f"backup_{current_time.strftime('%Y%m%d%H%M%S')}.sql"
    backup_path = os.path.join(BACKUP_DIR, backup_file)

# 使用 subprocess 模块执行数据库备份命令 
   backup_command = [
        'mysqldump',
        '-u',
        'username',
        '-p',
        'password',
        '--all-databases'
    ]
    with open(backup_path, 'w') as backup_file:
        subprocess.run(backup_command, stdout=backup_file)
    print(f"数据库备份已完成,备份文件保存为: {backup_path}")
# 清理过期备份文件
def cleanup_backup():
    current_time = datetime.datetime.now()
    cutoff_time = current_time - datetime.timedelta(days=RETENTION_PERIOD)

    for file in os.listdir(BACKUP_DIR):
        file_path = os.path.join(BACKUP_DIR, file)
        if os.path.isfile(file_path):
            file_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
            if file_time < cutoff_time:
                os.remove(file_path)
                print(f"过期备份文件已删除: {file_path}")
# 恢复数据库
def restore_database(backup_file, restore_time):
    backup_path = os.path.join(BACKUP_DIR, backup_file)

    # 使用 subprocess 模块执行数据库恢复命令
    restore_command = [
        'mysql',
        '-u',
        'username',
        '-p',
        'password'
    ]
    with open(backup_path, 'r') as backup_file:
        subprocess.run(restore_command, stdin=backup_file)

    print(f"数据库已成功恢复到时间点: {restore_time}")
# 主函数
def main():
    # 执行数据库备份
    backup_database()

    # 清理过期备份文件
    cleanup_backup()
    # 恢复数据库到指定时间点
    restore_database('backup_20220101120000.sql', '2022-01-01 12:00:00')
if __name__ == '__main__':    main()

 

posted @ 2024-10-16 10:25  武装小灰灰  阅读(15)  评论(0编辑  收藏  举报