Esempio n. 1
0
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
	# Extract the gzip file if user has passed *.sql.gz file instead of *.sql file

	if not os.path.exists(sql_file_path):
		sql_file_path = '../' + sql_file_path
		if not os.path.exists(sql_file_path):
			print('Invalid path {0}' + sql_file_path[3:])
			sys.exit(1)

	if sql_file_path.endswith('sql.gz'):
		sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))

	site = get_site(context)
	frappe.init(site=site)
	_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=sql_file_path,
		force=context.force)

	# Extract public and/or private files to the restored site, if user has given the path
	if with_public_files:
		public = extract_tar_files(site, with_public_files, 'public')
		os.remove(public)

	if with_private_files:
		private = extract_tar_files(site, with_private_files, 'private')
		os.remove(private)
Esempio n. 2
0
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
	# Extract the gzip file if user has passed *.sql.gz file instead of *.sql file

	if not os.path.exists(sql_file_path):
		sql_file_path = '../' + sql_file_path
		if not os.path.exists(sql_file_path):
			print('Invalid path {0}' + sql_file_path[3:])
			sys.exit(1)

	if sql_file_path.endswith('sql.gz'):
		sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))

	site = get_site(context)
	frappe.init(site=site)
	_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=sql_file_path,
		force=context.force)

	# Extract public and/or private files to the restored site, if user has given the path
	if with_public_files:
		public = extract_tar_files(site, with_public_files, 'public')
		os.remove(public)

	if with_private_files:
		private = extract_tar_files(site, with_private_files, 'private')
		os.remove(private)
Esempio n. 3
0
def restore_from_file(sql_file_path):
    frappe.flags.in_install_db = True

    if sql_file_path.endswith('sql.gz'):
        sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))

    import_db_from_sql(sql_file_path, True)

    frappe.flags.in_install_db = False
    frappe.clear_cache()
Esempio n. 4
0
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")
Esempio n. 5
0
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
	# Extract the gzip file if user has passed *.sql.gz file instead of *.sql file
	if sql_file_path.endswith('sql.gz'):
		sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))

	site = get_site(context)
	frappe.init(site=site)
	db_name = db_name or frappe.conf.db_name or hashlib.sha1(site).hexdigest()[:10]
	_new_site(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=sql_file_path, force=context.force)

	# Extract public and/or private files to the restored site, if user has given the path
	if with_public_files:
		extract_tar_files(site, with_public_files, 'public')

	if with_private_files:
		extract_tar_files(site, with_private_files, 'private')
Esempio n. 6
0
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
    # Extract the gzip file if user has passed *.sql.gz file instead of *.sql file
    if sql_file_path.endswith('sql.gz'):
        sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))

    site = get_site(context)
    frappe.init(site=site)
    db_name = db_name or frappe.conf.db_name or hashlib.sha1(
        site).hexdigest()[:10]
    _new_site(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=sql_file_path,
              force=context.force)

    # Extract public and/or private files to the restored site, if user has given the path
    if with_public_files:
        extract_tar_files(site, with_public_files, 'public')

    if with_private_files:
        extract_tar_files(site, with_private_files, 'private')
Esempio n. 7
0
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
    # 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)
    _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)

    # 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")