added autoremover

main
Jan Beníček 2024-06-21 06:42:51 +02:00
parent 779e95883a
commit 552b438d00
4 changed files with 44 additions and 11 deletions

View File

@ -5,4 +5,5 @@ SAVE_DIR=/dir
DISCORD_WEBHOOK=https://webhookaddress
MAIN_DOMAIN=http://baseOfDomainForbackupsPropagationViaWebhook
DISCORD_BACKUPNAME=ServerName
DISCORD_ENABLED=1
DISCORD_ENABLED=1
BACKUPS_KEEP_DAYS=4

View File

@ -11,6 +11,7 @@ class Config(BaseSettings):
main_domain: str
discord_enabled: int
discord_backupname: str
backups_keep_days: int
cfg = Config()

View File

@ -1,21 +1,19 @@
from conf import get_config
import pterodactylReader, fileDownloader, datetime, discordPoster
import pterodactylReader, fileDownloader, datetime, discordPoster, time, oldRemover
conf = get_config()
api_client = pterodactylReader.init_client(config=conf)
today_date = datetime.datetime.now()
backups_dict = pterodactylReader.get_backups_list(api_client=api_client, server_id=conf.server_id)
backup_uuid = pterodactylReader.get_last_backup_id(backups_dict)
backup_url = pterodactylReader.get_backup_url(api_client=api_client, server_id=conf.server_id, backup_id=backup_uuid)
print(backups_dict)
print("------------------")
print(backup_uuid)
print("------------------")
print(backup_url)
print("------------------")
backup_local_path = fileDownloader.create_dirs_and_download(backup_url=backup_url, date=datetime.datetime.now(), base_path=conf.save_dir)
time.sleep(5)
backup_local_path = fileDownloader.create_dirs_and_download(backup_url=backup_url, date=today_date, base_path=conf.save_dir)
if conf.discord_enabled == 1:
discordPoster.send_url_to_discord(webhook_url=conf.discord_webhook, backup_url=f"{conf.main_domain}{backup_local_path}", datetim=datetime.datetime.now(), backup_name=conf.discord_backupname)
discordPoster.send_url_to_discord(webhook_url=conf.discord_webhook, backup_url=f"{conf.main_domain}{backup_local_path}", datetim=today_date, backup_name=conf.discord_backupname)
if conf.backups_keep_days != 0:
oldRemover.remove_old_files(today_date=today_date, keep_days=conf.backups_keep_days, root_path=conf.save_dir)

33
src/oldRemover.py Normal file
View File

@ -0,0 +1,33 @@
import os, datetime
from pathlib import Path
def folder_path_year_creator(date: datetime.datetime, default_path: str) -> str:
return f"{default_path}/{date.year}"
def folder_path_month_creator(date: datetime.datetime, default_path: str) -> str:
return f"{folder_path_year_creator(date=date, default_path=default_path)}/{date.month}"
def file_path_creator(date: datetime.datetime, default_path: str) -> str:
return f"{folder_path_month_creator(date=date, default_path=default_path)}/{date.day}.tar.gz"
def remove_old_files(today_date: datetime.datetime, keep_days: int, root_path: str):
older_date = today_date - datetime.timedelta(days=keep_days)
last_date = today_date - datetime.timedelta(days=keep_days)
while older_date > (older_date - datetime.timedelta(days=10)):
older_date = older_date - datetime.timedelta(days=1)
if last_date.year > older_date.year and Path(folder_path_year_creator(date=older_date, default_path=root_path)).exists():
Path(folder_path_year_creator(date=older_date, default_path=root_path)).rmdir()
return
elif last_date.month > older_date.month and Path(folder_path_month_creator(date=older_date, default_path=root_path())).exists():
Path(folder_path_month_creator(date=older_date, default_path=root_path)).rmdir()
return
elif os.path.exists(file_path_creator(date=older_date, default_path=root_path)):
os.remove(file_path_creator(date=older_date, default_path=root_path))
last_date = older_date