吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1743|回复: 25
收起左侧

[Python 原创] 写了一个简单的"每天定时加密压缩指定文件夹"的小工具

[复制链接]
jia222 发表于 2023-9-15 13:11
[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)


等保要求加密传输  奈何数据库比较老 不支持加密  所以暂时想到了这个办法  有需要的可以自行打包

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
aa20221101 + 1 + 1 热心回复!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

weipohai 发表于 2023-9-15 17:23
wyl0205 发表于 2023-9-15 17:00
不至于,估计要异机备份,不过这个东东,一般用批处理就行了

大概在11年,我所在公司异地代码库要求备份,没有预算,目的要达到。我的操作就是批处理,包括rar打包,vpn连接,ftp上传,邮件发送(实际无法实现或判断成功与否,邮件是最后发送)提醒。还别说,成功率挺高。
wan41986 发表于 2023-9-15 14:34
z906z5374 发表于 2023-9-15 14:48
有点脱裤子放屁的感觉,加密打包了还要传输出去
lq99665234 发表于 2023-9-15 14:50
学习了,感觉很不错
YZJCX330 发表于 2023-9-15 15:15
感觉还不错,感谢分享
wkdxz 发表于 2023-9-15 15:16
思路不错,借鉴一下
momo2436 发表于 2023-9-15 15:37

思路挺不错的,借鉴一下
kings0b 发表于 2023-9-15 15:54
思路挺不错的,学习学习
wyl0205 发表于 2023-9-15 17:00
z906z5374 发表于 2023-9-15 14:48
有点脱裤子放屁的感觉,加密打包了还要传输出去

不至于,估计要异机备份,不过这个东东,一般用批处理就行了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-5 00:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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