def restore(self, user_did):
        clog().info('[restore_main] enter restore().')

        doc = cli.find_one_origin(DID_INFO_DB_NAME, VAULT_BACKUP_INFO_COL,
                                  {USER_DID: user_did})
        if self.is_ipfs:
            self.restore_ipfs_download_dbfiles(user_did,
                                               doc[VAULT_BACKUP_INFO_DRIVE],
                                               doc[VAULT_BACKUP_INFO_TOKEN])
            clog().info(
                '[restore_main: ipfs] success to download database files.')
            cli.import_mongodb(user_did)
            clog().info(
                '[restore_main: ipfs] success to import mongodb database.')
            self.restore_ipfs_pin_cids(user_did)
            clog().info('[restore_main: ipfs] success to pin ipfs cids.')
        else:
            vault_root = get_vault_path(user_did)
            if not vault_root.exists():
                create_full_path_dir(vault_root)
            clog().info(f'[restore_main] success to get vault root path.')
            self.restore_really(vault_root, doc[VAULT_BACKUP_INFO_DRIVE],
                                doc[VAULT_BACKUP_INFO_TOKEN])
            clog().info(f'[restore_main] success to execute restore.')
            self.restore_finish(user_did, doc[VAULT_BACKUP_INFO_DRIVE],
                                doc[VAULT_BACKUP_INFO_TOKEN])
            clog().info(f'[restore_main] success to restore finish.')
            cli.import_mongodb(user_did)

        self.delete_mongodb_data(user_did)
        self.update_backup_state(user_did, VAULT_BACKUP_STATE_STOP,
                                 VAULT_BACKUP_MSG_SUCCESS)
        clog().info('[restore_main] success to restore really.')
    def restore_finish(self, user_did, host_url, access_token):
        body = self.http.get(host_url + URL_RESTORE_FINISH, access_token)
        checksum_list = body["checksum_list"]
        vault_root = get_vault_path(user_did)
        if not vault_root.exists():
            create_full_path_dir(vault_root)

        local_checksum_list = get_file_checksum_list(vault_root)
        for checksum in checksum_list:
            if checksum not in local_checksum_list:
                raise BadRequestException(msg='Failed to finish restore.')
    def backup_finish(self, checksum_list):
        user_did, _, doc = self._check_auth_backup()

        backup_root = get_vault_backup_path(user_did)
        # TODO: remove this check.
        if not backup_root.exists():
            create_full_path_dir(backup_root)

        local_checksum_list = get_file_checksum_list(backup_root)
        for checksum in checksum_list:
            if checksum not in local_checksum_list:
                raise BadRequestException(
                    msg='Failed to finish backup process.')

        cli.update_one_origin(DID_INFO_DB_NAME, VAULT_BACKUP_SERVICE_COL,
                              {VAULT_BACKUP_SERVICE_DID: user_did}, {
                                  "$set": {
                                      VAULT_BACKUP_SERVICE_USE_STORAGE:
                                      get_dir_size(backup_root.as_posix(), 0)
                                  }
                              })
def export_mongo_db(did, app_did):
    """ Export every database as tar file to folder HIVE_DATA/vaults/<did>/mongo_db """
    save_path = get_save_mongo_db_path(did)
    if not save_path.exists():
        if not create_full_path_dir(save_path):
            return False

    # dump the data of the database 'db_name' to file 'dump_file'
    db_name = gene_mongo_db_name(did, app_did)
    from src.utils.db_client import cli
    if not cli.is_database_exists(db_name):
        return False

    return export_mongo_db_to_full_path(
        db_name, (save_path / db_name).with_suffix(BACKUP_FILE_SUFFIX))
Example #5
0
def query_upload_get_filepath(did, app_did, file_name):
    """
    Create the parent folder of the file and return the full path of the file.
    Return: full file path, error message
    """
    err = {}

    path = get_save_files_path(did, app_did)
    full_path_name = (path / file_name).resolve()

    if not create_full_path_dir(full_path_name.parent):
        err["status_code"], err[
            "description"] = INTERNAL_SERVER_ERROR, "make path dir error"
        return full_path_name, err

    if not full_path_name.exists():
        full_path_name.touch(exist_ok=True)

    if full_path_name.is_dir():
        err["status_code"], err[
            "description"] = NOT_FOUND, "file name is a directory"
        return full_path_name, err

    return full_path_name, err
Example #6
0
 def create_dir(self, path: Path):
     if not path.exists() and not create_full_path_dir(path):
         return False
     return True