Beispiel #1
0
 def register(self):
     """Register/create the current host on the remote server if not already registered."""
     if not dump_truck.get_var("client_id"):
         r_kwargs = self.request_kwargs.copy()
         r = requests.post(self.get_resource("clients"), **r_kwargs)
         if r.status_code == 200:
             client = r.json()
             if client:
                 dump_truck.save_var("client_id", client["_id"])
         else:
             log.error("An error occured during sync: {0}".format(r.text))
     else:
         log.debug("Already registered ({0})".format(dump_truck.get_var("client_id")))
Beispiel #2
0
def prepare_backup(backup):
    """Add some info before actually saving backup data.

    :type backup: dict
    :param backup: Backup data

    """
    backup["last_updated"] = int(datetime.utcnow().strftime("%s"))
    if backup.get("tags", []):
        tags_set = dump_truck.get_var("tags")
        tags_set.update(backup.get("tags"))
        dump_truck.save_var("tags", tags_set)
    return backup
Beispiel #3
0
    def sync(self):
        """Draft for implementing bakthat clients (hosts) backups data synchronization.

        Synchronize Bakthat sqlite database via a HTTP POST request.

        Backups are never really deleted from sqlite database, we just update the is_deleted key.

        It sends the last server sync timestamp along with data updated since last sync.
        Then the server return backups that have been updated on the server since last sync.

        Both side (bakthat and the sync server) make upserts of the latest data avaible:
        - if it doesn't exist yet, it will be created.
        - if it has been modified (e.g deleted, since it's the only action we can take) we update it.
        """
        log.debug("Start syncing")

        self.register()

        last_sync_ts = dump_truck.get_var("sync_ts")
        to_insert_in_mongo = dump_truck.execute("SELECT * FROM backups WHERE last_updated > {0:d}".format(last_sync_ts))
        data = dict(sync_ts=last_sync_ts, to_insert_in_mongo=to_insert_in_mongo)
        r_kwargs = self.request_kwargs.copy()
        log.debug("Initial payload: {0}".format(data))
        r_kwargs.update({"data": json.dumps(data)})
        r = requests.post(self.get_resource("backups/sync/status"), **r_kwargs)
        if r.status_code != 200:
            log.error("An error occured during sync: {0}".format(r.text))
            return

        log.debug("Sync result: {0}".format(r.json()))
        to_insert_in_bakthat = r.json().get("to_insert_in_bakthat")
        sync_ts = r.json().get("sync_ts")
        for newbackup in to_insert_in_bakthat:
            log.debug("Upsert {0}".format(newbackup))
            dump_truck_upsert_backup(newbackup)

        dump_truck.save_var("sync_ts", sync_ts)

        log.debug("Sync succcesful")