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/8b25a06e6a1d614a78e1f93dc1a9c9a3 to your computer and use it in GitHub Desktop.
Save peterbe/8b25a06e6a1d614a78e1f93dc1a9c9a3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import datetime
import time
import os
out = subprocess.check_output(["docker", "ps"]).decode("utf-8")
docker_id, = [
x.split()[0] for x in out.splitlines() if x.split()[1].startswith("mysql:")
]
print(docker_id)
print("Docker ID", docker_id)
today = datetime.datetime.utcnow()
outfile = f'../backup.{today.strftime("%Y%m%d")}.sql'
with open(outfile, "w") as f:
command = [
"docker",
"exec",
docker_id,
"/usr/bin/mysqldump",
"-u",
"root",
"--password=kuma",
"developer_mozilla_org",
]
print(f"Starting... ({' '.join(command)})")
t0 = time.time()
subprocess.call(command, stdout=f)
t1 = time.time()
size = os.stat(outfile).st_size
print(f"Finished {outfile} ({size / 1024 /1024:.1f}MB) in {t1 - t0:.1f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment