def restore_database(files_base, site_config_path, site): # restore database database_file = files_base + '-database.sql.gz' decompress_db(files_base, site) config = get_config() # Set db_type if it exists in backup site_config.json set_key_in_site_config('db_type', site, site_config_path) # Set db_host if it exists in backup site_config.json set_key_in_site_config('db_host', site, site_config_path) # Set db_port if it exists in backup site_config.json set_key_in_site_config('db_port', site, site_config_path) # get updated site_config site_config = get_site_config(site) # if no db_type exists, default to mariadb db_type = site_config.get('db_type', 'mariadb') is_database_restored = False if db_type == 'mariadb': restore_mariadb(config=config, site_config=site_config, database_file=database_file) is_database_restored = True elif db_type == 'postgres': restore_postgres(config=config, site_config=site_config, database_file=database_file) is_database_restored = True if is_database_restored: # Set encryption_key if it exists in backup site_config.json set_key_in_site_config('encryption_key', site, site_config_path)
def restore_database(files_base, site): db_root_password = os.environ.get('MYSQL_ROOT_PASSWORD') if not db_root_password: print('Variable MYSQL_ROOT_PASSWORD not set') exit(1) db_root_user = os.environ.get("DB_ROOT_USER", 'root') # restore database database_file = files_base + '-database.sql.gz' decompress_db(files_base, site) config = get_config() site_config = get_site_config(site) # mysql command prefix mysql_command = 'mysql -u{db_root_user} -h{db_host} -p{db_password} -e '.format( db_root_user=db_root_user, db_host=config.get('db_host'), db_password=db_root_password) # drop db if exists for clean restore drop_database = mysql_command + "\"DROP DATABASE IF EXISTS \`{db_name}\`;\"".format( db_name=site_config.get('db_name')) os.system(drop_database) # create db create_database = mysql_command + "\"CREATE DATABASE IF NOT EXISTS \`{db_name}\`;\"".format( db_name=site_config.get('db_name')) os.system(create_database) # create user create_user = mysql_command + "\"CREATE USER IF NOT EXISTS \'{db_name}\'@\'%\' IDENTIFIED BY \'{db_password}\'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password')) os.system(create_user) # create user password set_user_password = mysql_command + "\"UPDATE mysql.user SET authentication_string = PASSWORD('{db_password}') WHERE User = \'{db_name}\' AND Host = \'%\';\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password')) os.system(set_user_password) # grant db privileges to user grant_privileges = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name')) os.system(grant_privileges) command = "mysql -u{db_root_user} -h{db_host} -p{db_password} '{db_name}' < {database_file}".format( db_root_user=db_root_user, db_host=config.get('db_host'), db_password=db_root_password, db_name=site_config.get('db_name'), database_file=database_file.replace('.gz', ''), ) print('Restoring database for site: {}'.format(site)) os.system(command)
def decompress_db(files_base, site): database_file = files_base + '-database.sql.gz' config = get_config() site_config = get_site_config(site) db_root_user = os.environ.get('DB_ROOT_USER', 'root') command = 'gunzip -c {database_file} > {database_extract}'.format( database_file=database_file, database_extract=database_file.replace('.gz', '')) print('Extract Database GZip for site {}'.format(site)) os.system(command)
def main(): site_name = os.environ.get("SITE_NAME", 'site1.localhost') mariadb_root_username = os.environ.get("DB_ROOT_USER", 'root') mariadb_root_password = os.environ.get("MYSQL_ROOT_PASSWORD", 'admin') force = True if os.environ.get("FORCE", None) else False install_apps = os.environ.get("INSTALL_APPS", None) install_apps = install_apps.split(',') if install_apps else [] frappe.init(site_name, new_site=True) _new_site( None, site_name, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=os.environ.get("ADMIN_PASSWORD", 'admin'), verbose=True, install_apps=install_apps, source_sql=None, force=force, reinstall=False, ) config = get_config() site_config = get_site_config(site_name) # update User's host to '%' required to connect from any container command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( db_host=config.get('db_host'), mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password) command += "\"UPDATE mysql.user SET Host = '%' where User = '******'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name')) os.system(command) # Set db password command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( db_host=config.get('db_host'), mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password) command += "\"SET PASSWORD FOR '{db_name}'@'%' = PASSWORD('{db_password}'); FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password')) os.system(command) # Grant permission to database command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( db_host=config.get('db_host'), mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password) command += "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name')) os.system(command) exit(0)
def main(): config = get_config() db_type = 'mariadb' db_port = config.get('db_port', 3306) db_host = config.get('db_host') site_name = os.environ.get("SITE_NAME", 'site1.localhost') mariadb_root_username = os.environ.get("DB_ROOT_USER", 'root') mariadb_root_password = get_password("MYSQL_ROOT_PASSWORD", 'admin') postgres_root_password = get_password("POSTGRES_PASSWORD") if postgres_root_password: db_type = 'postgres' db_host = os.environ.get("POSTGRES_HOST") db_port = 5432 if not db_host: db_host = config.get('db_host') print('Environment variable POSTGRES_HOST not found.') print('Using db_host from common_site_config.json') sites_path = os.getcwd() common_site_config_path = os.path.join(sites_path, COMMON_SITE_CONFIG_FILE) update_site_config("root_login", mariadb_root_username, validate=False, site_config_path=common_site_config_path) update_site_config("root_password", postgres_root_password, validate=False, site_config_path=common_site_config_path) force = True if os.environ.get("FORCE", None) else False install_apps = os.environ.get("INSTALL_APPS", None) install_apps = install_apps.split(',') if install_apps else [] frappe.init(site_name, new_site=True) if semantic_version.Version(frappe.__version__).major > 11: _new_site( None, site_name, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=get_password("ADMIN_PASSWORD", 'admin'), verbose=True, install_apps=install_apps, source_sql=None, force=force, db_type=db_type, reinstall=False, db_host=db_host, db_port=db_port, ) else: _new_site( None, site_name, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=get_password("ADMIN_PASSWORD", 'admin'), verbose=True, install_apps=install_apps, source_sql=None, force=force, reinstall=False, ) if db_type == "mariadb": site_config = get_site_config(site_name) mysql_command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( db_host=config.get('db_host'), mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password) # update User's host to '%' required to connect from any container command = mysql_command + "\"UPDATE mysql.user SET Host = '%' where User = '******'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name')) os.system(command) # Set db password command = mysql_command + "\"ALTER USER '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password')) os.system(command) # Grant permission to database command = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name')) os.system(command) if frappe.redis_server: frappe.redis_server.connection_pool.disconnect() exit(0)
def restore_database(files_base, site_config_path, site): db_root_password = get_password('MYSQL_ROOT_PASSWORD') if not db_root_password: print('Variable MYSQL_ROOT_PASSWORD not set') exit(1) db_root_user = os.environ.get("DB_ROOT_USER", 'root') # restore database database_file = files_base + '-database.sql.gz' decompress_db(files_base, site) config = get_config() site_config = get_site_config(site) # mysql command prefix mysql_command = 'mysql -u{db_root_user} -h{db_host} -p{db_password} -e '.format( db_root_user=db_root_user, db_host=config.get('db_host'), db_password=db_root_password) # drop db if exists for clean restore drop_database = mysql_command + "\"DROP DATABASE IF EXISTS \`{db_name}\`;\"".format( db_name=site_config.get('db_name')) os.system(drop_database) # create db create_database = mysql_command + "\"CREATE DATABASE IF NOT EXISTS \`{db_name}\`;\"".format( db_name=site_config.get('db_name')) os.system(create_database) # create user create_user = mysql_command + "\"CREATE USER IF NOT EXISTS \'{db_name}\'@\'%\' IDENTIFIED BY \'{db_password}\'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password')) os.system(create_user) # grant db privileges to user grant_privileges = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password')) os.system(grant_privileges) command = "mysql -u{db_root_user} -h{db_host} -p{db_password} '{db_name}' < {database_file}".format( db_root_user=db_root_user, db_host=config.get('db_host'), db_password=db_root_password, db_name=site_config.get('db_name'), database_file=database_file.replace('.gz', ''), ) print('Restoring database for site: {}'.format(site)) os.system(command) if os.path.exists(site_config_path): with open(site_config_path, 'r') as sc: site_config = json.load(sc) encryption_key = site_config.get("encryption_key") if encryption_key: print('Restoring site config for site: {}'.format(site)) update_site_config('encryption_key', encryption_key, site_config_path=os.path.join( os.getcwd(), site, "site_config.json"))