How to do database backups?

Hello,

A project Iโ€™m working on is expanding and bringing on more devs, so I want to start regularly backing up our database in the event that anything goes wrong. How do you suggest I do this?

Thank you,
Spencer

you have direct access to mongo db instance now, you could do the backup periodically by connecting directly to Moralis server db

1 Like

Thank you for the response! Would you be able to provide some basic steps required to set up a system for being able to store backups, and the moralis DB from said backups?

You find the IP and port where to connect to mongo db in your Moralis server settings, you will also need to whitelist the IP that will connect to that mongo db instance (also in settings).
After that you can connect to that database and make queries directly there, for example this is a python script that makes a simple query:

import pprint
import pymongo

MONGO_HOST = "MONGO_HOST_IP_FROM_ADMIN_INTERFACE"
MONGO_PORT = MONGO_HOST_PORT_FROM_ADMMIN_INTERFACE

con = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
user_table = con['parse']['_User']
pprint.pprint(user_table.find_one())

And with a script then you can dump what tables you want or the entire database in order to store its data somewhere.

4 Likes

Awesome example, thanks!

I get raise ServerSelectionTimeoutError. Could that be something with security setting?

I know we canโ€™t go with python errors in the forum but anyone experienced could comment on this, would be appreciated. :handshake:

My script:

import pprint
import pymongo

MONGO_HOST = "104.248.***.***"
MONGO_PORT = 5****

con = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
user_table = con['parse']['Items']
pprint.pprint(user_table.find_one())
PS C:\project\export_db> python3 .\export_db.py > res.txt
Traceback (most recent call last):
  File ".\export_db.py", line 11, in <module>
    pprint.pprint(user_table.find_one())
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\collection.py", line 1419, in find_one
    for result in cursor.limit(-1):
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\cursor.py", line 1248, in next
    if len(self.__data) or self._refresh():
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\cursor.py", line 1139, in _refresh
    self.__session = self.__collection.database.client._ensure_session()
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\mongo_client.py", line 1663, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\mongo_client.py", line 1608, in __start_session
    self._topology._check_implicit_session_support()
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\topology.py", line 519, in _check_implicit_session_support
    self._check_session_support()
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\topology.py", line 535, in _check_session_support
    self._select_servers_loop(
  File "<PYTHON_PATH>\Python38\site-packages\pymongo\topology.py", line 227, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: 104.248.***.***:5****: connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 624f379df95db893d8507020, topology_type: Unknown, servers: [<ServerDescription ('104.248.***.***', 5****) server_type: Unknown, rtt: None, error=AutoReconnect('104.248.***.***:5****: connection closed')>]>

did you add your public IP in whitelist for that server?

if yes, try to add it again

1 Like

It works. I thought whitelist was open to anywhere as default until you enter an IP, so I havenโ€™t tried.