Skip to content

Commit

Permalink
add auto delete old backups
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRazamataz committed Aug 3, 2021
1 parent 098f6b7 commit d2d8e27
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,89 @@
import shutil
import discord
from discord.ext import commands
from backup_func import *
from backup_func2 import *
import asyncio
import datetime
import time
from datetime import *
global online
import os
online = True
print ("Starting.")
client = commands.Bot(command_prefix="backup!", help_command=None)
DELETE_OLD_BACKUPS_AFTER_DAYS = 3
async def backupclean1(ctx):
today = str(datetime.today())
#now = time.time()
backup_location = "/media/data/BackupBot/Essentials"
#print(f"{today} and {now}")
directory_contents = os.listdir(backup_location)
print(directory_contents)
print(f"The date today is `{today}`. Comparing the backup folders to today's date.")
list_of_datetimes = []
# delete old backups
for name in directory_contents:
year, month, day = name.split('-')
list_of_datetimes.append(datetime(int(year), int(month), int(day)))

today = datetime.now()
await ctx.channel.send(f"Checking for backups older than {DELETE_OLD_BACKUPS_AFTER_DAYS} day(s)...")
for date in list_of_datetimes:
if (today - date).days >= DELETE_OLD_BACKUPS_AFTER_DAYS:
date = date.strftime('%Y-%m-%d')
print(date, "is overdue and will be deleted.")
await ctx.channel.send(
f"The backup `{date}` is over {DELETE_OLD_BACKUPS_AFTER_DAYS} day(s) old and will be deleted.")
deletepath = f"{backup_location}/{date}"
shutil.rmtree(deletepath, ignore_errors=False, onerror=None)
print("Old backup destroyed!")
else:
print("No old backups found.")
await ctx.channel.send(
f"No backups were found that were older than {DELETE_OLD_BACKUPS_AFTER_DAYS} day(s), skipping to download.")
async def backupclean2(ctx):
today = str(datetime.today())
#now = time.time()
backup_location = "/media/data/BackupBot/mcMMO"
#print(f"{today} and {now}")
directory_contents = os.listdir(backup_location)
print(directory_contents)
print(f"The date today is `{today}`. Comparing the backup folders to today's date.")
list_of_datetimes = []
# delete old backups
for name in directory_contents:
year, month, day = name.split('-')
list_of_datetimes.append(datetime(int(year), int(month), int(day)))

today = datetime.now()
await ctx.channel.send(f"Checking for backups older than {DELETE_OLD_BACKUPS_AFTER_DAYS} day(s)...")
for date in list_of_datetimes:
if (today - date).days >= DELETE_OLD_BACKUPS_AFTER_DAYS:
date = date.strftime('%Y-%m-%d')
print(date, "is overdue and will be deleted.")
await ctx.channel.send(
f"The backup `{date}` is over {DELETE_OLD_BACKUPS_AFTER_DAYS} day(s) old and will be deleted.")
deletepath = f"{backup_location}/{date}"
shutil.rmtree(deletepath, ignore_errors=False, onerror=None)
print("Old backup destroyed!")
else:
print("No old backups found.")
await ctx.channel.send(
f"No backups were found that were older than {DELETE_OLD_BACKUPS_AFTER_DAYS} day(s), skipping to download.")

@client.event
async def on_ready():
print ("Backup Bot is ready.")
await client.change_presence(activity=discord.Game(name='KC Backup Bot 1.0'))
await client.change_presence(activity=discord.Game(name='KC Backup Bot 2.0'))


@client.command(name="start")
@commands.has_permissions(administrator=True)
async def start(ctx: commands.Context):
while online == True:
try:
await backupclean1(ctx)
await backupclean2(ctx)
print ("Backup starting.")
await ctx.channel.send("Backup starting.")
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='backing up...'))
Expand Down

0 comments on commit d2d8e27

Please sign in to comment.