def __proc_hive_node_param(self): did, content, err = did_post_json_param_pre_proc( self.response, "backup_credential", access_vault=VAULT_ACCESS_R) if err: return None, None, err host, backup_token, err = view.h_auth.backup_auth_request(content) if err: return None, None, self.response.response_err(UNAUTHORIZED, err) info = get_vault_backup_info(did) if info and info[VAULT_BACKUP_INFO_STATE] != VAULT_BACKUP_STATE_STOP: if info[VAULT_BACKUP_INFO_TIME] < (datetime.utcnow().timestamp() - 60 * 60 * 24): data = dict() data["vault_backup_state"] = info[VAULT_BACKUP_INFO_STATE] return None, None, self.response.response_ok(data) upsert_vault_backup_info(did, VAULT_BACKUP_INFO_TYPE_HIVE_NODE, host, backup_token) data, err = self.get_backup_service(host + INTER_BACKUP_SERVICE_URL, backup_token) if err: return None, None, err backup_service = data["backup_service"] return did, backup_service, None
def __proc_google_drive_param(self): did, content, err = did_post_json_param_pre_proc( self.response, 'token', 'refresh_token', 'expiry', 'client_id', 'client_secret', access_vault=VAULT_ACCESS_R) if err: return None, None, err info = get_vault_backup_info(did) if info and info[VAULT_BACKUP_INFO_STATE] != VAULT_BACKUP_STATE_STOP: # If sync process more than one day, we think it is failed if info[VAULT_BACKUP_INFO_TIME] < (datetime.utcnow().timestamp() - 60 * 60 * 24): data = dict() data["vault_backup_state"] = info[VAULT_BACKUP_INFO_STATE] return None, None, self.response.response_ok(data) config_data = RcloneTool.get_config_data(content, did) drive_name = HiveBackup.gene_did_google_drive_name(did) RcloneTool.create_rclone_config_file(drive_name, config_data) upsert_vault_backup_info(did, VAULT_BACKUP_INFO_TYPE_GOOGLE_DRIVE, drive_name) return did, drive_name, None
def backup_to_vault(self): did, content, err = did_post_json_param_pre_proc(self.response) if err: return self.response.response_err( UNAUTHORIZED, "Backup backup_to_vault auth failed") vault_service = get_vault_service(did) if not vault_service: return self.response.response_err( BAD_REQUEST, f"There is not vault service of {did} to active") backup_service = get_vault_backup_service(did) if not backup_service: return self.response.response_err( BAD_REQUEST, f"There is not vault backup service of {did}") freeze_vault(did) delete_user_vault(did) app_id_list = backup_service[VAULT_BACKUP_SERVICE_DATA] for app_id in app_id_list: import_files_from_backup(did, app_id) import_mongo_db_from_backup(did, app_id) unfreeze_vault(did) return self.response.response_ok()
def inter_backup_save(self): did, content, err = did_post_json_param_pre_proc( self.response, "app_id_list") if err: return self.response.response_err( UNAUTHORIZED, "Backup internal backup_communication_start auth failed") update_vault_backup_service_item(did, VAULT_BACKUP_SERVICE_DATA, content["app_id_list"]) return self.response.response_ok()
def backup_restore_finish(self): did, content, err = did_post_json_param_pre_proc(self.response) if err: return err backup_path = get_vault_backup_path(did) if not backup_path.exists(): return self.response.response_err(NOT_FOUND, f"{did} backup vault not found") backup_checksum_list = get_file_checksum_list(backup_path) data = {"checksum_list": backup_checksum_list} return self.response.response_ok(data)
def backup_save_finish(self): did, content, err = did_post_json_param_pre_proc(self.response, "checksum_list") if err: return err checksum_list = content["checksum_list"] backup_path = get_vault_backup_path(did) if not backup_path.exists(): return self.response.response_err(NOT_FOUND, f"{did} backup vault not found") backup_checksum_list = get_file_checksum_list(backup_path) for checksum in checksum_list: if checksum not in backup_checksum_list: return self.response.response_err(CHECKSUM_FAILED, f"{did} backup file checksum failed") total_size = 0.0 total_size = get_dir_size(backup_path.as_posix(), total_size) update_vault_backup_service_item(did, VAULT_BACKUP_SERVICE_USE_STORAGE, total_size) return self.response.response_ok()
def activate_to_vault(self): did, content, err = did_post_json_param_pre_proc(self.response) if err: return self.response.response_err( UNAUTHORIZED, "Backup backup_to_vault auth failed") vault_service = get_vault_service(did) if not vault_service: return self.response.response_err( BAD_REQUEST, f"There is not vault service of {did} to active") backup_service = get_vault_backup_service(did) if not backup_service: return self.response.response_err( BAD_REQUEST, f"There is not vault backup service of {did}") freeze_vault(did) delete_user_vault_data(did) copy_local_backup_to_vault(did) import_mongo_db(did) delete_mongo_db_export(did) unfreeze_vault(did) return self.response.response_ok()
def move_file(self, is_copy): did, content, response = did_post_json_param_pre_proc(self.response, "src_file", "dst_file", access_backup=BACKUP_ACCESS) if response is not None: return response src_name = content.get('src_file') src_name = filter_path_root(src_name) dst_name = content.get('dst_file') dst_name = filter_path_root(dst_name) backup_path = get_vault_backup_path(did) src_full_path_name = (backup_path / src_name).resolve() dst_full_path_name = (backup_path / dst_name).resolve() if not src_full_path_name.exists(): return self.response.response_err(NOT_FOUND, "src_name not exists") if dst_full_path_name.exists(): dst_full_path_name.unlink() dst_parent_folder = dst_full_path_name.parent if not dst_parent_folder.exists(): if not create_full_path_dir(dst_parent_folder): return self.response.response_err(SERVER_MKDIR_ERROR, "move_file make dst parent path dir error") try: if is_copy: shutil.copy2(src_full_path_name.as_posix(), dst_full_path_name.as_posix()) else: shutil.move(src_full_path_name.as_posix(), dst_full_path_name.as_posix()) except Exception as e: logger.error(f"exception of move_file error is {str(e)}") return self.response.response_err(SERVER_MOVE_FILE_ERROR, "Exception:" + str(e)) return self.response.response_ok()
def inter_backup_ftp_start(self): did, content, err = did_post_json_param_pre_proc(self.response) if err: return self.response.response_err( UNAUTHORIZED, "Backup internal backup_communication_start auth failed") # check backup service exist info = get_vault_backup_service(did) if not info: return self.response.response_err( BAD_REQUEST, "There is no backup service of " + did) backup_path = get_vault_backup_path(did) if not backup_path.exists(): create_full_path_dir(backup_path) # add user to backup ftp server user, passwd = gene_vault_backup_ftp_record(did) if self.mode != HIVE_MODE_TEST: self.backup_ftp.add_user(user, passwd, backup_path, 'elradfmwMT') del info["_id"] if VAULT_BACKUP_SERVICE_FTP in info: del info[VAULT_BACKUP_SERVICE_FTP] if VAULT_BACKUP_SERVICE_DATA in info: del info[VAULT_BACKUP_SERVICE_DATA] data = { "token": HiveBackup.__data_to_node_backup_token( hive_setting.BACKUP_FTP_PORT, user, passwd), "backup_service": info } return self.response.response_ok(data)