Giter VIP home page Giter VIP logo

Comments (1)

cuu508 avatar cuu508 commented on June 2, 2024

I exported all the database and tried to import back but it's not being easy.

What issues did you run into?

Export/Import has been requested a few times, and I agree would be useful:

  • for moving account data when migrating servers (like in your case)
  • when moving from the hosted service to self-hosted (and vice-versa)
  • for self-service backups and recovery through the web UI ("oops, I deleted the wrong project, now what?")

For moving data between self-hosted instances, it could be as simple as a couple management commands – one command to produce a database dump, and another for importing it.

Export/import through web UI would be more tricky:

  • when exporting data, it should carefully select fields to include in the export. For example, exporting internal database IDs would be a security problem
  • when importing data, it would have to handle data conflicts. For example, what to do if we're importing a check, but a check with the same UUID already exists (and potentially belongs to a different user account)?
  • as Healthchecks evolves, and new database fields get added, the importing code would need to maintain backwards-compatibility for importing backups produced with older Healthchecks versions

It would have to be done very carefully, as there's lots of potential for unintended side-effects and security oopsies.


There was one time when a customer deleted a big project by accident and asked if it's possible to restore it. Obviously I could not restore the whole database, as it would roll back data for all other users. I restored a database backup to a separate system, and wrote export/import commands to export/import a single account. Here are the commands I used:

export.py:

from itertools import chain

from django.core.management.base import BaseCommand
from django.core.serializers import serialize

from django.contrib.auth.models import User
from hc.accounts.models import Credential, Profile, Project
from hc.api.models import Channel, Check, Flip, Notification, Ping


class Command(BaseCommand):
    help = """Export a single account's data in a JSON format."""

    def handle(self, *args, **options):
        email = "[email protected]"

        user_q = User.objects.filter(email=email)
        credential_q = Credential.objects.filter(user__email=email)
        profile_q = Profile.objects.filter(user__email=email)
        project_q = Project.objects.filter(owner__email=email)
        check_q = Check.objects.filter(project__owner__email=email)
        channel_q = Channel.objects.filter(project__owner__email=email)
        ping_q = Ping.objects.filter(owner__project__owner__email=email)
        flip_q = Flip.objects.filter(owner__project__owner__email=email)
        notification_q = Notification.objects.filter(owner__project__owner__email=email)

        chained = chain(
            user_q,
            credential_q,
            profile_q,
            project_q,
            check_q,
            channel_q,
            ping_q,
            notification_q,
            flip_q,
        )

        print(serialize("json", chained, indent=2))

restore.py:

from django.core.management.base import BaseCommand
from django.core.serializers import deserialize


class Command(BaseCommand):
    help = """Export a single account's data in a JSON format."""

    def handle(self, *args, **options):
        f = open("foo_at_example_org.json", "r")
        for i, obj in enumerate(deserialize("json", f)):
            obj.save(force_insert=True)

            if i % 1000 == 0:
                print(i)

Note: I have not checked what happens with Postgres sequences if you use the above scripts to import data in an empty database.

from healthchecks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.