Skip to content

Instantly share code, notes, and snippets.

Created May 6, 2023 22:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import base64
import hashlib
import json
import os
import requests
# Replace these variables with your information
GITHUB_ACCESS_TOKEN = "your_access_token"
REPO_OWNER = "your_github_username"
REPO_NAME = "your_repo_name"
DB_FILE_PATH = "path/to/your/sqlite.db"
# GitHub API base URL
# Set the required headers for API requests
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"token {GITHUB_ACCESS_TOKEN}"
def get_file_sha_and_content():
response = requests.get(f"{BASE_URL}/contents/{os.path.basename(DB_FILE_PATH)}", headers=headers)
if response.status_code == 200:
content = json.loads(response.text)
return content["sha"], content["content"]
print(f"Error: {response.status_code}")
return None, None
def commit_and_push_file(file_sha, new_content):
commit_msg = "Update SQLite DB backup"
data = {
"message": commit_msg,
"content": new_content,
"sha": file_sha
response = requests.put(f"{BASE_URL}/contents/{os.path.basename(DB_FILE_PATH)}", data=json.dumps(data), headers=headers)
if response.status_code == 200:
print(f"File committed and pushed with message: '{commit_msg}'")
print(f"Error: {response.status_code}")
def main():
sha, old_content = get_file_sha_and_content()
if sha is not None and old_content is not None:
with open(DB_FILE_PATH, "rb") as db_file:
new_content = base64.b64encode("utf-8")
# Check if file contents have changed
old_md5 = hashlib.md5(base64.b64decode(old_content)).hexdigest()
new_md5 = hashlib.md5(base64.b64decode(new_content)).hexdigest()
if old_md5 != new_md5:
commit_and_push_file(sha, new_content)
print("No changes in the SQLite DB file since the last commit.")
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment