So I have this website that I am deploying onto render. I pushed my code to a GitHub repository and render takes the code from there. My plan was to save user data in json files that were inside a data folder, but the problem I realized is that every so often render replaces its code with the old code from my GItHub repository, including the old data folder that doesn't have any user data in it yet. My idea, was to make a function in my app.py that every time someone makes an account, edits their account, deletes their account or basically anytime that those json files inside my data folder are changed this function will take that data folder from render, and push it to my GitHub repository replacing the old data folder. This time whenever my render site replaces it's code with the GitHub, it will still have all user data. I first tried making this backup work on my local computer, by replacing the GitHub data folder with one on my computer and it worked. Here is that code:
import os
import shutil
import subprocess
def backup():
GITHUB_USERNAME = "My github username"
REPO_NAME = "Name of my repository"
BRANCH_NAME = "main"
GITHUB_TOKEN = 'github token name' # for when I put this on render I saved the token as an environment variable: os.getenv("GITHUB_TOKEN")
repo_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github/{GITHUB_USERNAME}/{REPO_NAME}.git"
repo_path = "/tmp/myrepo"
folder_to_add = "data/"
local_folder_path = f"pathtofolder/{folder_to_add}"
if os.path.exists(repo_path):
shutil.rmtree(repo_path)
subprocess.run(["git", "clone", repo_url, repo_path], check=True)
os.chdir(repo_path)
repo_folder_path = os.path.join(repo_path, folder_to_add)
if os.path.exists(repo_folder_path):
shutil.rmtree(repo_folder_path)
shutil.copytree(local_folder_path, repo_folder_path)
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", f"Replaced {folder_to_add} with a new version"], check=True)
subprocess.run(["git", "push", "origin", BRANCH_NAME], check=True)
backup()
I then tried putting it in my app.py on my Github repository and then redeploying my website and testing it, and when I made an account it worked. I went to the GitHub repository and sure enough the data folder had been changed to include the users information inside a json file. But then whenever I tried to change user data again, wether that was to make another account or or delete the current one, or change user information, it gave me a 500 internal server error. And in my render logs I got this message:
Cloning into '/tmp/myrepo'... fatal: Unable to read current working directory: No such file or directory [2025-03-16 14:05:32,558] ERROR in app: Exception on /deleteaccount [POST]
Here is the changed backup function that was inside of my app.py:
def backup():
GITHUB_USERNAME = "Github username"
REPO_NAME = "Name of repository"
BRANCH_NAME = "main"
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
if not GITHUB_TOKEN:
raise ValueError("There is no github token in an environment variable.")
repo_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github/{GITHUB_USERNAME}/{REPO_NAME}.git"
repo_path = "/tmp/myrepo"
folder_to_add = "data/"
local_folder_path = f"/opt/render/project/src/{folder_to_add}"
if os.path.exists(repo_path):
shutil.rmtree(repo_path)
subprocess.run(["git", "clone", repo_url, repo_path], check=True)
os.chdir(repo_path)
repo_folder_path = os.path.join(repo_path, folder_to_add)
if os.path.exists(repo_folder_path):
shutil.rmtree(repo_folder_path)
shutil.copytree(local_folder_path, repo_folder_path)
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "config", "--global", "user.name", "Your Name"], check=True)
subprocess.run(["git", "config", "--global", "user.email", "[email protected]"], check=True)
subprocess.run(["git", "commit", "-m", f"Replaced {folder_to_add} with a new version"], check=True)
subprocess.run(["git", "push", "origin", BRANCH_NAME], check=True)
If anyone knows how to fix this problem I would really appreciate it.