UltraDebug

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: A C D R G Y M Z X S P
公益项目,接受捐赠
查看: 2404|回复: 0
收起左侧

[Python] 开源一个简单的"每天定时加密压缩指定文件夹"的小程序

[复制链接]
uqwndggg

主题

0

回帖

UD

注册会员

UID
57
积分
51
注册时间
2022-6-10
最后登录
1970-1-1
2023-9-15 23:03:41 | 显示全部楼层 |阅读模式
等保要求加密传输  奈何数据库比较老 不支持加密  所以暂时想到了这个办法  有需要的可以自行打包

[Python] 纯文本查看 复制代码
import shutil
import subprocess
import os
import schedule
import time
import logging
from pathlib import Path
 
def encrypt_folder_with_winrar(folder_path, password):
    # WinRAR命令
    winrar_command = r'C:\Program Files \WinRAR\WinRAR.exe'
 
    # 获取当前日期
    current_date = time.strftime("%Y-%m-%d", time.localtime())
 
    # 获取桌面BD文件夹路径
    desktop_path = str(Path.home() / "Desktop")
    bd_folder_path = os.path.join(desktop_path, "DB")
 
    # 创建DB文件夹(如果不存在)
    if not os.path.exists(bd_folder_path):
        os.mkdir(bd_folder_path)
 
    # 压缩包文件名
    compressed_filename = f'压缩包-{current_date}.rar'
 
    # 压缩包路径
    compressed_filepath = os.path.join(bd_folder_path, compressed_filename)
 
    # 加密命令
    encryption_command = [winrar_command, 'a', '-r', '-hp' + password, compressed_filepath, folder_path]
 
    # 执行加密命令
    subprocess.run(encryption_command)
 
    return compressed_filepath
 
def encrypt_and_delete_folder(folder_path, password):
    try:
        # 压缩并加密文件夹
        compressed_file = encrypt_folder_with_winrar(folder_path, password)
 
        # 等待一段时间,确保压缩文件完成
        time.sleep(10)
 
        # 删除源文件夹中的文件
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                os.remove(os.path.join(root, file))
 
        logger.info("加密打包操作成功")
    except Exception as e:
        logger.error(f"加密打包操作失败:{e}")
 
        # 出现错误时,删除已创建的压缩文件
        if os.path.exists(compressed_file):
            os.remove(compressed_file)
            logger.info("压缩文件已删除.")
 
# 创建日志记录器
logger = logging.getLogger("encrypt_and_delete")
logger.setLevel(logging.INFO)
 
# 创建文件处理器
log_file = "encrypt_and_delete.log"
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.INFO)
 
# 创建控制台处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
 
# 创建日志格式
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
 
# 添加处理器到记录器
logger.addHandler(file_handler)
logger.addHandler(console_handler)
 
# 文件夹路径
folder_path = r'D:\BACK'
 
# 密码
password = '123456'
 
# 每天定时执行加密打包操作
schedule.every().day.at("12:20").do(encrypt_and_delete_folder, folder_path=folder_path, password=password)
 
# 循环执行任务,并写入日志
while True:
    schedule.run_pending()
    time.sleep(60)
SyntaxHighlighter Copyright 2004-2013 Alex Gorbatchev.
UltraDebug免责声明
✅以上内容均来自网友转发或原创,如存在侵权请发送到站方邮件9003554@qq.com处理。
✅The above content is forwarded or original by netizens. If there is infringement, please send the email to the destination 9003554@qq.com handle.
回复 打印

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|站点地图|UltraDebug ( 滇ICP备2022002049号-2 滇公网安备 53032102000034号)

GMT+8, 2025-6-18 06:46 , Processed in 0.029037 second(s), 11 queries , Redis On.

Powered by Discuz X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表