'Django how to create, update and keep clone objects to another database for backup purpose?

postgresql is my primary data base. If any object create in my original database then I want it will also keep an duplicate object in my secondary database. I read Django documentation for create an clone database but didn't work. here is my code:

#replicarouter.py

 class PrimaryReplicaRouter:
    def db_for_read(self, model, **hints):
        """
        Reads go to a randomly-chosen replica.
        """
        return 'primary'

    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        return 'primary'

    def allow_relation(self, obj1,**hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        db_set = {'primary', 'replica_database'}
        if obj1._state.db in db_set:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All non-auth models end up in this pool.
        """
        return True
 

settings.py

     DATABASES = {
        'default':{},
        'primary': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'my_db_name',
            'USER': 'postgres',
            'PASSWORD': 'my_db_pass',
            'HOST': 'localhost',
            'PORT':5432,
        },

        'replica_database': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'my_db_name',
            'USER': 'root',
            'PASSWORD': '',
            'HOST': 'localhost',
            'PORT': 3306,
        }

        
    }

DATABASE_ROUTERS = ['my_root_folder_name.dbrouters.AuthRouter','my_root_folder_name.replicarouter.PrimaryReplicaRouter']

right now all new objects creating in my primary database. I want to keep clone of every new object in my replica_database. If any object added in my primary database then it will also add an clone object in my replica_database.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source