mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-25 19:43:57 +01:00
Added backups
This commit is contained in:
parent
b174166f2a
commit
0d0453291f
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -181,3 +181,5 @@ cython_debug/
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
backups/
|
|
@ -3,6 +3,9 @@
|
||||||
# Commit to the production branch
|
# Commit to the production branch
|
||||||
# git commit -am "Auto-trigger - Production server started" && git push origin Production
|
# git commit -am "Auto-trigger - Production server started" && git push origin Production
|
||||||
|
|
||||||
|
# backup database
|
||||||
|
/usr/local/bin/python /home/nova-api/backup_manager/main.py pre_prodpush
|
||||||
|
|
||||||
# Kill production server
|
# Kill production server
|
||||||
fuser -k 2333/tcp
|
fuser -k 2333/tcp
|
||||||
|
|
51
api/backup_manager/main.py
Normal file
51
api/backup_manager/main.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
import os
|
||||||
|
from motor.motor_asyncio import AsyncIOMotorClient
|
||||||
|
from bson import json_util
|
||||||
|
import json
|
||||||
|
from sys import argv
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
MONGO_URI = os.getenv("MONGO_URI")
|
||||||
|
FILE_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
|
async def main(output_dir):
|
||||||
|
await make_backup(output_dir)
|
||||||
|
|
||||||
|
async def make_backup(output_dir):
|
||||||
|
output_dir = os.path.join(FILE_DIR, "..", "backups", output_dir)
|
||||||
|
|
||||||
|
if not os.path.exists(output_dir):
|
||||||
|
os.makedirs(output_dir)
|
||||||
|
|
||||||
|
client = AsyncIOMotorClient(MONGO_URI)
|
||||||
|
databases = await client.list_database_names()
|
||||||
|
databases = {db: await client[db].list_collection_names() for db in databases}
|
||||||
|
|
||||||
|
for database in databases:
|
||||||
|
if not os.path.exists(f"{output_dir}/{database}"):
|
||||||
|
os.mkdir(f"{output_dir}/{database}")
|
||||||
|
|
||||||
|
for collection in databases[database]:
|
||||||
|
print(f"Making backup for {database}/{collection}")
|
||||||
|
await make_backup_for_collection(database, collection, output_dir)
|
||||||
|
|
||||||
|
async def make_backup_for_collection(database, collection, output_dir):
|
||||||
|
path = f"{output_dir}/{database}/{collection}.json"
|
||||||
|
|
||||||
|
client = AsyncIOMotorClient(MONGO_URI)
|
||||||
|
collection = client[database][collection]
|
||||||
|
documents = await collection.find({}).to_list(length=None)
|
||||||
|
|
||||||
|
with open(path, "w") as f:
|
||||||
|
json.dump(documents, f, default=json_util.default)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(argv) < 2 or len(argv) > 2:
|
||||||
|
print("Usage: python3 main.py <output_dir>")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
output_dir = argv[1]
|
||||||
|
asyncio.run(main(output_dir))
|
|
@ -208,20 +208,20 @@ async def demo():
|
||||||
else:
|
else:
|
||||||
raise ConnectionError('API Server is not running.')
|
raise ConnectionError('API Server is not running.')
|
||||||
|
|
||||||
# print('[lightblue]Checking if function calling works...')
|
print('[lightblue]Checking if function calling works...')
|
||||||
# print(await test_function_calling())
|
print(await test_function_calling())
|
||||||
|
|
||||||
# print('Checking non-streamed chat completions...')
|
print('Checking non-streamed chat completions...')
|
||||||
# print(await test_chat_non_stream_gpt4())
|
print(await test_chat_non_stream_gpt4())
|
||||||
|
|
||||||
# print('Checking streamed chat completions...')
|
print('Checking streamed chat completions...')
|
||||||
# print(await test_chat_stream_gpt3())
|
print(await test_chat_stream_gpt3())
|
||||||
|
|
||||||
# print('[lightblue]Checking if image generation works...')
|
print('[lightblue]Checking if image generation works...')
|
||||||
# print(await test_image_generation())
|
print(await test_image_generation())
|
||||||
|
|
||||||
# print('Checking the models endpoint...')
|
print('Checking the models endpoint...')
|
||||||
# print(await test_models())
|
print(await test_models())
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print('[red]Error: ' + str(exc))
|
print('[red]Error: ' + str(exc))
|
||||||
|
|
Loading…
Reference in a new issue