[V3] Optimize the backup command (#1666)

* [V3 Core] Enhance [p]backup to exclude some files

* Backup the repo list too

* Lol Sinbad's pre-commit hook

* Add option of sending the backup to the owner via DM

* Drop an unnecessary config object in RepoManager

* Move the backup functionality in redbot-setup to the new stuff

* More work on implementation, including backing up the instance data
This commit is contained in:
palmtree5
2018-05-27 20:56:28 -08:00
committed by Kowlin
parent f01d48f9ae
commit 7775b16199
5 changed files with 99 additions and 38 deletions

View File

@@ -2,6 +2,7 @@ import asyncio
import datetime
import importlib
import itertools
import json
import logging
import os
import sys
@@ -951,7 +952,7 @@ class Core:
@commands.command()
@checks.is_owner()
async def backup(self, ctx):
async def backup(self, ctx, backup_path: str = None):
"""Creates a backup of all data for the instance."""
from redbot.core.data_manager import basic_config, instance_name
from redbot.core.drivers.red_json import JSON
@@ -979,14 +980,59 @@ class Core:
instance_name, ctx.message.created_at.strftime("%Y-%m-%d %H-%M-%S")
)
if data_dir.exists():
home = data_dir.home()
backup_file = home / backup_filename
os.chdir(str(data_dir.parent))
if not backup_path:
backup_pth = data_dir.home()
else:
backup_pth = Path(backup_path)
backup_file = backup_pth / backup_filename
to_backup = []
exclusions = [
"__pycache__",
"Lavalink.jar",
os.path.join("Downloader", "lib"),
os.path.join("CogManager", "cogs"),
os.path.join("RepoManager", "repos"),
]
downloader_cog = ctx.bot.get_cog("Downloader")
if downloader_cog and hasattr(downloader_cog, "_repo_manager"):
repo_output = []
repo_mgr = downloader_cog._repo_manager
for n, repo in repo_mgr._repos:
repo_output.append(
{{"url": repo.url, "name": repo.name, "branch": repo.branch}}
)
repo_filename = data_dir / "cogs" / "RepoManager" / "repos.json"
with open(str(repo_filename), "w") as f:
f.write(json.dumps(repo_output, indent=4))
instance_data = {instance_name: basic_config}
instance_file = data_dir / "instance.json"
with open(str(instance_file), "w") as instance_out:
instance_out.write(json.dumps(instance_data, indent=4))
for f in data_dir.glob("**/*"):
if not any(ex in str(f) for ex in exclusions):
to_backup.append(f)
with tarfile.open(str(backup_file), "w:gz") as tar:
tar.add(data_dir.stem)
for f in to_backup:
tar.add(str(f), recursive=False)
print(str(backup_file))
await ctx.send(
_("A backup has been made of this instance. It is at {}.").format(backup_file)
_("A backup has been made of this instance. It is at {}.").format((backup_file))
)
await ctx.send(_("Would you like to receive a copy via DM? (y/n)"))
def same_author_check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
msg = await ctx.bot.wait_for("message", check=same_author_check, timeout=60)
except asyncio.TimeoutError:
await ctx.send(_("Ok then."))
else:
if msg.content.lower().strip() == "y":
await ctx.author.send(
_("Here's a copy of the backup"), file=discord.File(str(backup_file))
)
else:
await ctx.send(_("That directory doesn't seem to exist..."))