Skip to content

Instantly share code, notes, and snippets.

@peterbe
Last active October 30, 2019 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/d34541d95c4625239eac6fadb1bfd415 to your computer and use it in GitHub Desktop.
Save peterbe/d34541d95c4625239eac6fadb1bfd415 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import datetime
import time
import os
import shutil
from glob import glob
def run():
if input("Is docker-compose stopped? [Y/n] ").strip().lower() == "n":
return
if (
input("Have you run `docker-compose down --volumes --remove-orphans` [y/N]")
.strip()
.lower()
!= "y"
):
if input("Want me to run that for you? [Y/n] ").lower().strip() != "n":
out = subprocess.check_output(
["docker-compose", "down", "--volumes", "--remove-orphans"]
).decode("utf-8")
print(out)
print("--------------------")
print("About to run 'docker-compose pull'")
print(subprocess.check_output(["docker-compose", "pull"]).decode("utf-8"))
print("About to run 'docker-compose up -d'")
print(subprocess.check_output(["docker-compose", "up", "-d"]).decode("utf-8"))
print("Is up yet? Basically, you need to wait till docker-compose really is up")
files = glob(os.path.join("..", "backup.*.sql"))
files.sort(reverse=True)
def fmt(b):
return f"{b / 1024 / 1024:.1f}MB"
for i, f in enumerate(files):
print(
i + 1,
f,
datetime.datetime.fromtimestamp(os.stat(f).st_ctime),
fmt(os.stat(f).st_size),
)
i = int(input("Which one do you want to import now? [1] ") or "1")
# Copy it here...
file = files[i - 1]
tmp_name = "./backup-TEMP.sql"
print(f"Copying {file} to {tmp_name}")
shutil.copyfile(file, tmp_name)
try:
cat_command = f"cat {tmp_name} | ./manage.py dbshell"
print(
subprocess.check_output(
["docker-compose", "exec", "web", "bash", "-c", cat_command]
).decode("utf-8")
)
finally:
print(f"Deleting {tmp_name}...")
os.remove(tmp_name)
print("DONE!")
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment