From 1290a1b8f7358a6ba8dea4f76eb40dc0b42ebfd4 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 30 Jan 2022 16:38:03 +0800 Subject: [PATCH] =?UTF-8?q?autobackup=20=E5=87=8C=E6=99=A8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=A4=87=E4=BB=BD=E6=95=B0=E6=8D=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autobackup.py | 33 +++++++++++++++++++++++++++++++++ list.json | 10 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 autobackup.py diff --git a/autobackup.py b/autobackup.py new file mode 100644 index 0000000..238dc25 --- /dev/null +++ b/autobackup.py @@ -0,0 +1,33 @@ +""" Pagermaid auto-backup plugin. """ +import json +import os +from datetime import datetime +from distutils.util import strtobool +from pagermaid import config, redis_status, redis, scheduler, bot +from pagermaid.utils import upload_attachment +from pagermaid.modules.backup import make_tar_gz + + +@scheduler.scheduled_job("cron", day="*", hour="0", minute="0", second="20", id="backup_job") +async def run_every_1_day(): + # remove mp3 , they are so big ! + for i in os.listdir("data"): + if i.find(".mp3") != -1 or i.find(".jpg") != -1 or i.find(".flac") != -1 or i.find(".ogg") != -1: + os.remove(f"data{os.sep}{i}") + # backup redis + redis_data = {} + if redis_status(): + for k in redis.keys(): + data_type = redis.type(k) + if data_type == b'string': + v = redis.get(k) + redis_data[k.decode()] = v.decode() + with open(f"data{os.sep}redis.json", "w", encoding='utf-8') as f: + json.dump(redis_data, f, indent=4) + # run backup function + if strtobool(config['log']): + make_tar_gz(f"pagermaid_{datetime.now().date()}.tar.gz", ["data", "plugins", "config.yml"]) + await upload_attachment(f"pagermaid_{datetime.now().date()}.tar.gz", int(config['log_chatid']), None) + os.remove(f"pagermaid_{datetime.now().date()}.tar.gz") + else: + make_tar_gz(f"pagermaid_backup.tar.gz", ["data", "plugins", "config.yml"]) diff --git a/list.json b/list.json index cf5b128..aef1fba 100644 --- a/list.json +++ b/list.json @@ -869,6 +869,16 @@ "supported": true, "des-short": "傻妞机器人对接。", "des": "把自己变成行走的傻妞。" + }, + { + "name": "autobackup", + "version": "1.00", + "section": "daily", + "maintainer": "xtaodada", + "size": "1.4 kb", + "supported": true, + "des-short": "凌晨自动备份。", + "des": "凌晨自动备份数据文件。" } ] }