2022-05-23 12:40:30 +00:00
|
|
|
""" Pagermaid backup and recovery plugin. """
|
2024-08-13 10:38:13 +00:00
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
import os
|
2022-06-20 13:55:14 +00:00
|
|
|
import sys
|
2022-05-23 12:40:30 +00:00
|
|
|
import tarfile
|
|
|
|
from traceback import format_exc
|
|
|
|
|
|
|
|
from pagermaid.config import Config
|
2024-09-28 13:57:55 +00:00
|
|
|
from pagermaid.enums import Message
|
2022-05-23 12:40:30 +00:00
|
|
|
from pagermaid.listener import listener
|
2024-09-28 13:57:55 +00:00
|
|
|
from pagermaid.utils import lang
|
|
|
|
from pagermaid.utils.bot_utils import upload_attachment
|
2022-05-23 12:40:30 +00:00
|
|
|
|
|
|
|
pgm_backup_zip_name = "pagermaid_backup.tar.gz"
|
|
|
|
|
|
|
|
|
|
|
|
def make_tar_gz(output_filename, source_dirs: list):
|
|
|
|
"""
|
|
|
|
压缩 tar.gz 文件
|
|
|
|
:param output_filename: 压缩文件名
|
|
|
|
:param source_dirs: 需要压缩的文件列表
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
with tarfile.open(output_filename, "w:gz") as tar:
|
|
|
|
for i in source_dirs:
|
|
|
|
tar.add(i, arcname=os.path.basename(i))
|
|
|
|
|
|
|
|
|
|
|
|
def un_tar_gz(filename, dirs):
|
|
|
|
"""
|
|
|
|
解压 tar.gz 文件
|
|
|
|
:param filename: 压缩文件名
|
|
|
|
:param dirs: 解压后的存放路径
|
|
|
|
:return: bool
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
t = tarfile.open(filename, "r:gz")
|
|
|
|
t.extractall(path=dirs)
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
print(e, format_exc())
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
@listener(
|
|
|
|
is_plugin=False, outgoing=True, command="backup", description=lang("backup_des")
|
|
|
|
)
|
2022-06-20 13:55:14 +00:00
|
|
|
async def backup(message: Message):
|
2023-03-12 03:56:01 +00:00
|
|
|
await message.edit(lang("backup_process"))
|
2022-05-23 12:40:30 +00:00
|
|
|
|
|
|
|
# Remove old backup
|
|
|
|
if os.path.exists(pgm_backup_zip_name):
|
|
|
|
os.remove(pgm_backup_zip_name)
|
|
|
|
|
|
|
|
# remove mp3 , they are so big !
|
|
|
|
for i in os.listdir("data"):
|
2023-03-12 03:56:01 +00:00
|
|
|
if (
|
|
|
|
i.find(".mp3") != -1
|
|
|
|
or i.find(".jpg") != -1
|
|
|
|
or i.find(".flac") != -1
|
|
|
|
or i.find(".ogg") != -1
|
|
|
|
):
|
2022-05-23 12:40:30 +00:00
|
|
|
os.remove(f"data{os.sep}{i}")
|
|
|
|
|
|
|
|
# run backup function
|
2023-12-12 12:29:22 +00:00
|
|
|
make_tar_gz(pgm_backup_zip_name, ["data", "plugins"])
|
2022-05-23 12:40:30 +00:00
|
|
|
if Config.LOG:
|
2022-12-22 04:00:10 +00:00
|
|
|
try:
|
|
|
|
await upload_attachment(pgm_backup_zip_name, Config.LOG_ID, None)
|
|
|
|
await message.edit(lang("backup_success_channel"))
|
|
|
|
except Exception:
|
|
|
|
await message.edit(lang("backup_success"))
|
2022-05-23 12:40:30 +00:00
|
|
|
else:
|
|
|
|
await message.edit(lang("backup_success"))
|
|
|
|
|
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
@listener(
|
|
|
|
is_plugin=False,
|
|
|
|
outgoing=True,
|
|
|
|
command="recovery",
|
|
|
|
need_admin=True,
|
|
|
|
description=lang("recovery_des"),
|
|
|
|
)
|
2022-06-20 13:55:14 +00:00
|
|
|
async def recovery(message: Message):
|
2022-05-23 12:40:30 +00:00
|
|
|
reply = message.reply_to_message
|
|
|
|
|
2022-07-28 12:06:45 +00:00
|
|
|
if not reply:
|
2023-03-12 03:56:01 +00:00
|
|
|
return await message.edit(lang("recovery_file_error"))
|
2022-06-20 13:55:14 +00:00
|
|
|
if not reply.document:
|
2023-03-12 03:56:01 +00:00
|
|
|
return await message.edit(lang("recovery_file_error"))
|
2022-05-23 12:40:30 +00:00
|
|
|
|
2022-06-20 13:55:14 +00:00
|
|
|
try:
|
|
|
|
if ".tar.gz" not in reply.document.file_name:
|
2023-03-12 03:56:01 +00:00
|
|
|
return await message.edit(lang("recovery_file_error"))
|
|
|
|
await message.edit(lang("recovery_down"))
|
2022-06-20 13:55:14 +00:00
|
|
|
# Start download process
|
|
|
|
pgm_backup_zip_name = await reply.download() # noqa
|
|
|
|
except Exception as e: # noqa
|
|
|
|
print(e, format_exc())
|
2023-03-12 03:56:01 +00:00
|
|
|
return await message.edit(lang("recovery_file_error"))
|
2022-05-23 12:40:30 +00:00
|
|
|
# Extract backup files
|
2023-03-12 03:56:01 +00:00
|
|
|
await message.edit(lang("recovery_process"))
|
2022-05-23 12:40:30 +00:00
|
|
|
if not os.path.exists(pgm_backup_zip_name):
|
2023-03-12 03:56:01 +00:00
|
|
|
return await message.edit(lang("recovery_file_not_found"))
|
2022-05-23 12:40:30 +00:00
|
|
|
elif not un_tar_gz(pgm_backup_zip_name, ""):
|
|
|
|
os.remove(pgm_backup_zip_name)
|
2023-03-12 03:56:01 +00:00
|
|
|
return await message.edit(lang("recovery_file_error"))
|
2022-05-23 12:40:30 +00:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
if os.path.exists(pgm_backup_zip_name):
|
|
|
|
os.remove(pgm_backup_zip_name)
|
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
await message.edit(lang("recovery_success") + " " + lang("apt_reboot"))
|
2022-08-14 04:44:43 +00:00
|
|
|
sys.exit(0)
|