Skip to content

Instantly share code, notes, and snippets.

@The-Final-Apex
Created November 24, 2025 10:34
Show Gist options
  • Select an option

  • Save The-Final-Apex/6c06b73da1cb6b957f6a47545722ea45 to your computer and use it in GitHub Desktop.

Select an option

Save The-Final-Apex/6c06b73da1cb6b957f6a47545722ea45 to your computer and use it in GitHub Desktop.
A simple way to backup a sql file (its in instance) the routes are defines outside the code
@app.route('/backup')
def backup():
# Get current timestamp to avoid overwriting
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
backup_folder = os.path.join(BACKUP_DIR, f'backup_{timestamp}')
print(timestamp, backup_folder)
print(BACKUP_DIR)
# Create the backup directory
os.makedirs(backup_folder, exist_ok=True)
# Copy the uploads and instance folders to the backup folder
try:
shutil.copytree(UPLOADS_FOLDER, os.path.join(backup_folder, 'uploads'), dirs_exist_ok=True)
shutil.copytree(INSTANCE_FOLDER, os.path.join(backup_folder, 'instance'), dirs_exist_ok=True)
print("succefuly copied files")
# Now zip the backup folder
zip_filename = f'{backup_folder}.zip'
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
print("with staetment")
for root, _, files in os.walk(backup_folder):
print("first for")
for file in files:
print("second for")
file_path = os.path.join(root, file)
print("file path thingy")
zipf.write(file_path, os.path.relpath(file_path, backup_folder))
print("last thingy")
print(backup_folder)
# Clean up (remove the unzipped backup folder)
def handle_remove_readonly(func, path, exec_info):
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(backup_folder, onerror=handle_remove_readonly)
print("removed unzipped folder")
return f'Backup created successfully at {zip_filename} on Desktop.'
except Exception as e:
print(f"an error occured : {e}")
return f'An error occurred: {e}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment