Skip to content

Instantly share code, notes, and snippets.

@wendeehsu
Created February 11, 2021 06:04
Show Gist options
  • Save wendeehsu/5603620967f13bc0507a69280127ab31 to your computer and use it in GitHub Desktop.
Save wendeehsu/5603620967f13bc0507a69280127ab31 to your computer and use it in GitHub Desktop.
dump database and upload to GCS
import os
from google.cloud import storage
from datetime import datetime
# backup database
date = datetime.now().strftime("%Y-%m-%d")
base_dir = "/var/www/sophie/"
folder_name = "backup/"
filename = date + ".sql"
# The password of mysql root user is `12345`. `hallai` is the DB I want to backup.
command = "mysqldump -uroot -p12345 hallai > " + base_dir + folder_name + filename
os.system(command)
# upload to google cloud storage
client = storage.Client.from_service_account_json(base_dir + "credentials_gcs.json")
bucket = client.get_bucket('taipei_medical') # my bucket name
blob = bucket.blob(folder_name + filename) # I want to store the backup file in `backup` folder on GCS
blob.upload_from_filename(base_dir + folder_name + filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment