Ejemplo n.º 1
0
def restore_mariadb(config, site_config, database_file):
    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')

    db_host = site_config.get('db_host', config.get('db_host'))
    db_port = site_config.get('db_port', config.get('db_port'))

    # mysql command prefix
    mysql_command = 'mysql -u{db_root_user} -h{db_host} -p{db_password}'.format(
        db_root_user=db_root_user,
        db_host=db_host,
        db_port=db_port,
        db_password=db_root_password)

    # drop db if exists for clean restore
    drop_database = "{mysql_command} -e \"DROP DATABASE IF EXISTS \`{db_name}\`;\"".format(
        mysql_command=mysql_command,
        db_name=site_config.get('db_name'),
    )
    os.system(drop_database)

    # create db
    create_database = "{mysql_command} -e \"CREATE DATABASE IF NOT EXISTS \`{db_name}\`;\"".format(
        mysql_command=mysql_command,
        db_name=site_config.get('db_name'),
    )
    os.system(create_database)

    # create user
    create_user = "******"CREATE USER IF NOT EXISTS \'{db_name}\'@\'%\' IDENTIFIED BY \'{db_password}\'; FLUSH PRIVILEGES;\"".format(
        mysql_command=mysql_command,
        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} -e \"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;\"".format(
        mysql_command=mysql_command,
        db_name=site_config.get('db_name'),
        db_password=site_config.get('db_password'),
    )
    os.system(grant_privileges)

    command = "{mysql_command} '{db_name}' < {database_file}".format(
        mysql_command=mysql_command,
        db_name=site_config.get('db_name'),
        database_file=database_file.replace('.gz', ''),
    )

    print('Restoring MariaDB')
    os.system(command)
Ejemplo n.º 2
0
def restore_database(files_base, 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)
Ejemplo n.º 3
0
def main():
    backup_dir = get_backup_dir()

    if len(list_directories(backup_dir)) == 0:
        pull_backup_from_s3()

    for site in list_directories(backup_dir):
        site_slug = site.replace('.', '_')
        backups = [
            datetime.datetime.strptime(backup, DATE_FORMAT)
            for backup in list_directories(os.path.join(backup_dir, site))
        ]
        latest_backup = max(backups).strftime(DATE_FORMAT)
        files_base = os.path.join(backup_dir, site, latest_backup, '')
        files_base += latest_backup + '-' + site_slug
        site_config_path = files_base + '-site_config_backup.json'
        if not os.path.exists(site_config_path):
            site_config_path = os.path.join(backup_dir, site,
                                            'site_config.json')
        if site in get_sites():
            restore_database(files_base, site_config_path, site)
            restore_private_files(files_base)
            restore_files(files_base)
        else:
            mariadb_root_password = get_password('MYSQL_ROOT_PASSWORD')
            if not mariadb_root_password:
                print('Variable MYSQL_ROOT_PASSWORD not set')
                exit(1)

            site_config = get_conf_params(
                db_name='_' + hashlib.sha1(site.encode()).hexdigest()[:16],
                db_password=random_string(16))

            frappe.local.site = site
            frappe.local.sites_path = os.getcwd()
            frappe.local.site_path = os.getcwd() + '/' + site
            make_conf(
                db_name=site_config.get('db_name'),
                db_password=site_config.get('db_password'),
            )
            make_site_dirs()
            restore_database(files_base, site_config_path, site)
            restore_private_files(files_base)
            restore_files(files_base)

    exit(0)
Ejemplo n.º 4
0
def restore_postgres(config, site_config, database_file):
    # common config
    common_site_config_path = os.path.join(os.getcwd(),
                                           COMMON_SITE_CONFIG_FILE)

    db_root_user = config.get('root_login')
    if not db_root_user:
        postgres_user = os.environ.get('DB_ROOT_USER')
        if not postgres_user:
            print('Variable DB_ROOT_USER not set')
            exit(1)

        db_root_user = postgres_user
        update_site_config("root_login",
                           db_root_user,
                           validate=False,
                           site_config_path=common_site_config_path)

    db_root_password = config.get('root_password')
    if not db_root_password:
        root_password = get_password('POSTGRES_PASSWORD')
        if not root_password:
            print('Variable POSTGRES_PASSWORD not set')
            exit(1)

        db_root_password = root_password
        update_site_config("root_password",
                           db_root_password,
                           validate=False,
                           site_config_path=common_site_config_path)

    # site config
    db_host = site_config.get('db_host')
    db_port = site_config.get('db_port', 5432)
    db_name = site_config.get('db_name')
    db_password = site_config.get('db_password')

    psql_command = "psql postgres://{root_login}:{root_password}@{db_host}:{db_port}".format(
        root_login=db_root_user,
        root_password=db_root_password,
        db_host=db_host,
        db_port=db_port)

    print('Restoring PostgreSQL')
    os.system(psql_command +
              ' -c "DROP DATABASE IF EXISTS \"{db_name}\""'.format(
                  db_name=db_name))
    os.system(psql_command +
              ' -c "DROP USER IF EXISTS {db_name}"'.format(db_name=db_name))
    os.system(psql_command +
              ' -c "CREATE DATABASE \"{db_name}\""'.format(db_name=db_name))
    os.system(psql_command +
              ' -c "CREATE user {db_name} password \'{db_password}\'"'.format(
                  db_name=db_name, db_password=db_password))
    os.system(
        psql_command +
        ' -c "GRANT ALL PRIVILEGES ON DATABASE \"{db_name}\" TO {db_name}"'.
        format(db_name=db_name))

    os.system("{psql_command}/{db_name} < {database_file}".format(
        psql_command=psql_command,
        database_file=database_file.replace('.gz', ''),
        db_name=db_name,
    ))