From ad93203d2786395cf76d5955277a09c05a9705d8 Mon Sep 17 00:00:00 2001 From: MrRazamataz Date: Sun, 28 Mar 2021 20:25:42 +0100 Subject: [PATCH] Fixed and added new things. Discord status update Automatic every 6 hours after `backup!start` To stop auto backups do `backup!start` To start them do `backup!restart` then `backup!start`. --- backup_func.py | 10 ++++----- backup_func2.py | 8 +++---- main.py | 59 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/backup_func.py b/backup_func.py index 0b6f752..963505d 100644 --- a/backup_func.py +++ b/backup_func.py @@ -8,12 +8,12 @@ def backup_func(): # and directories hostname = 'ip' - port = 22 - username = 'root' + port = 22 #default port for ssh/sftp is 22, change if needed + username = 'user' password = 'password' - start_directory = 'folder to backup.' - backup_dir = 'backup to' - + start_directory = 'dir to backup' + backup_dir = 'dir to save to (on local machine, same that the bot is running on)' + import paramiko import os import datetime diff --git a/backup_func2.py b/backup_func2.py index 55b4b46..7b85d11 100644 --- a/backup_func2.py +++ b/backup_func2.py @@ -8,11 +8,11 @@ def backup_func2(): # and directories hostname = 'ip' - port = 22 - username = 'root' + port = 22 #default port for ssh/sftp is 22, change if needed + username = 'user' password = 'password' - start_directory = 'folder to backup.' - backup_dir = 'backup to' + start_directory = 'dir to backup' + backup_dir = 'dir to save to (on local machine, same that the bot is running on)' import paramiko import os diff --git a/main.py b/main.py index 1c03c35..b12f346 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,9 @@ from discord.ext import commands from backup_func import * from backup_func2 import * +import asyncio +global online +online = True print ("Starting.") client = commands.Bot(command_prefix="backup!", help_command=None) @@ -15,24 +18,50 @@ async def on_ready(): @client.command(name="start") @commands.has_permissions(administrator=True) async def start(ctx: commands.Context): - try: - print ("Backup starting.") - await ctx.channel.send("Backup starting.") - #from backup_func import backup_func - backup_func() - except OSError as err: - await ctx.send("First backup complete.") - print ("Backup Complete") + while online == True: try: - await ctx.send("Starting second backup...") - print ("Second backup starting.") - backup_func2() - await ctx.send("Backup complete.") - print("Backup Complete") + print ("Backup starting.") + await ctx.channel.send("Backup starting.") + await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='backing up...')) + #from backup_func import backup_func + backup_func() except OSError as err: - print(err) - await ctx.send("Backup complete.") + await ctx.send("First backup complete.") print ("Backup Complete") + try: + await ctx.send("Starting second backup...") + print ("Second backup starting.") + backup_func2() + await ctx.send("Backup complete.") + print("Backup Complete") + await ctx.send("Waiting for next backup to start in 6 hours.") + print ("Waiting for next backup to start in 6 hours.") + await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='waiting for next backup. play.kingdomscrusade.net')) + await asyncio.sleep(21600) + except OSError as err: + print(err) + await ctx.send("Backup complete.") + print ("Backup Complete") + await ctx.send("Waiting for next backup to start in 6 hours.") + print ("Waiting for next backup to start in 6 hours.") + await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='waiting for next backup. play.kingdomscrusade.net')) + await asyncio.sleep(21600) +@client.command(name="stop") +@commands.has_permissions(administrator=True) +async def stop(ctx: commands.Context): + await ctx.channel.send("Stopping the backup sequence.") + global online + online = False + print ("Backup stopped (paused)") + await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='KC Backup Bot 1.0, play.kingdomscrusade.net Backup status: paused')) +@client.command(name="restart") +@commands.has_permissions(administrator=True) +async def restart(ctx: commands.Context): + await ctx.channel.send("Restarting the backup sequence (you need to run backup!start to set it auto going again).") + global online + online = True + print("Backup started (unpaused)") + await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Restarting backups.')) @client.event async def on_error(ctx, event, *args, **kwargs): if isinstance(FileNotFoundError):