def restore(context, sql_file_path, mariadb_root_username=None, mariadb_root_password=None, db_name=None, verbose=None, install_app=None, admin_password=None, force=None, with_public_files=None, with_private_files=None): "Restore site database from an sql file" from frappe.installer import extract_sql_gzip, extract_tar_files, is_downgrade force = context.force or force # Extract the gzip file if user has passed *.sql.gz file instead of *.sql file if not os.path.exists(sql_file_path): base_path = '..' sql_file_path = os.path.join(base_path, sql_file_path) if not os.path.exists(sql_file_path): print('Invalid path {0}'.format(sql_file_path[3:])) sys.exit(1) elif sql_file_path.startswith(os.sep): base_path = os.sep else: base_path = '.' if sql_file_path.endswith('sql.gz'): decompressed_file_name = extract_sql_gzip(os.path.abspath(sql_file_path)) else: decompressed_file_name = sql_file_path site = get_site(context) frappe.init(site=site) # dont allow downgrading to older versions of frappe without force if not force and is_downgrade(decompressed_file_name, verbose=True): warn_message = "This is not recommended and may lead to unexpected behaviour. Do you want to continue anyway?" click.confirm(warn_message, abort=True) _new_site(frappe.conf.db_name, site, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=admin_password, verbose=context.verbose, install_apps=install_app, source_sql=decompressed_file_name, force=True, db_type=frappe.conf.db_type) # Extract public and/or private files to the restored site, if user has given the path if with_public_files: with_public_files = os.path.join(base_path, with_public_files) public = extract_tar_files(site, with_public_files, 'public') os.remove(public) if with_private_files: with_private_files = os.path.join(base_path, with_private_files) private = extract_tar_files(site, with_private_files, 'private') os.remove(private) # Removing temporarily created file if decompressed_file_name != sql_file_path: os.remove(decompressed_file_name) success_message = "Site {0} has been restored{1}".format(site, " with files" if (with_public_files or with_private_files) else "") click.secho(success_message, fg="green")
def restore( context, sql_file_path, db_root_username=None, db_root_password=None, db_name=None, verbose=None, install_app=None, admin_password=None, force=None, with_public_files=None, with_private_files=None, ): "Restore site database from an sql file" from frappe.installer import ( _new_site, extract_files, extract_sql_from_archive, is_downgrade, is_partial, validate_database_sql, ) force = context.force or force decompressed_file_name = extract_sql_from_archive(sql_file_path) # check if partial backup if is_partial(decompressed_file_name): click.secho( "Partial Backup file detected. You cannot use a partial file to restore a Frappe Site.", fg="red", ) click.secho( "Use `bench partial-restore` to restore a partial backup to an existing site.", fg="yellow") sys.exit(1) # check if valid SQL file validate_database_sql(decompressed_file_name, _raise=not force) site = get_site(context) frappe.init(site=site) # dont allow downgrading to older versions of frappe without force if not force and is_downgrade(decompressed_file_name, verbose=True): warn_message = ( "This is not recommended and may lead to unexpected behaviour. " "Do you want to continue anyway?") click.confirm(warn_message, abort=True) _new_site( frappe.conf.db_name, site, db_root_username=db_root_username, db_root_password=db_root_password, admin_password=admin_password, verbose=context.verbose, install_apps=install_app, source_sql=decompressed_file_name, force=True, db_type=frappe.conf.db_type, ) # Extract public and/or private files to the restored site, if user has given the path if with_public_files: public = extract_files(site, with_public_files) os.remove(public) if with_private_files: private = extract_files(site, with_private_files) os.remove(private) # Removing temporarily created file if decompressed_file_name != sql_file_path: os.remove(decompressed_file_name) success_message = "Site {0} has been restored{1}".format( site, " with files" if (with_public_files or with_private_files) else "") click.secho(success_message, fg="green")