Exemplo n.º 1
0
def backup_db(release=None, limit=5):
    """
    Backup database and associate it with current release
    """

    assert "psql_user" in env, "Missing psql_user in env"
    assert "psql_db" in env, "Missing psql_db in env"
    assert "psql_password" in env, "Missing psql_password in env"

    if not release:
        release = paths.get_current_release_name()

    max_versions = limit+1

    if not release:
        logger.info("No releases present, skipping task")
        return

    remote_file = "postgresql/%s.sql.tar.gz" % release
    remote_path = paths.get_backup_path(remote_file)

    env.run("mkdir -p %s" % paths.get_backup_path("postgresql"))

    with context_managers.shell_env(PGPASSWORD=env.psql_password):
        env.run("pg_dump -h localhost -Fc -f %s -U %s %s -x -O" % (
            remote_path, env.psql_user, env.psql_db
        ))

    # Remove older releases
    env.run("ls -dt %s/* | tail -n +%s | xargs rm -rf" % (
        paths.get_backup_path("postgresql"),
        max_versions)
    )
Exemplo n.º 2
0
def backup_db(release=None, limit=5):
    """
    Backup database and associate it with current release
    """

    assert "mysql_user" in env, "Missing mysqL_user in env"
    assert "mysql_password" in env, "Missing mysql_password in env"
    assert "mysql_host" in env, "Missing mysql_host in env"
    assert "mysql_db" in env, "Missing mysql_db in env"

    if not release:
        release = paths.get_current_release_name()

    max_versions = limit+1

    if not release:
        return

    env.run("mkdir -p %s" % paths.get_backup_path("mysql"))

    backup_file = "mysql/%s.sql.gz" % release
    backup_path = paths.get_backup_path(backup_file)

    env.run("mysqldump -u %s -p%s -h %s %s | gzip -c > %s" %
            (env.mysql_user, env.mysql_password, env.mysql_host, env.mysql_db,
             backup_path))

    # Remove older releases
    env.run("ls -dt %s/* | tail -n +%s | xargs rm -rf" % (
        paths.get_backup_path("mysql"),
        max_versions)
    )
Exemplo n.º 3
0
def backup_db(release=None, limit=5):
    """
    Backup database and associate it with current release
    """

    assert "psql_user" in env, "Missing psql_user in env"
    assert "psql_db" in env, "Missing psql_db in env"
    assert "psql_password" in env, "Missing psql_password in env"

    if not release:
        release = paths.get_current_release_name()

    max_versions = limit + 1

    if not release:
        logger.info("No releases present, skipping task")
        return

    remote_file = "postgresql/%s.sql.tar.gz" % release
    remote_path = paths.get_backup_path(remote_file)

    env.run("mkdir -p %s" % paths.get_backup_path("postgresql"))

    with context_managers.shell_env(PGPASSWORD=env.psql_password):
        env.run("pg_dump -h localhost -Fc -f %s -U %s %s -x -O" %
                (remote_path, env.psql_user, env.psql_db))

    # Remove older releases
    env.run("ls -dt %s/* | tail -n +%s | xargs rm -rf" %
            (paths.get_backup_path("postgresql"), max_versions))
Exemplo n.º 4
0
def sync_remote_to_local(force="no"):
    """
    Sync your remote postgres database with local

    Example:
        fabrik prod sync_remote_to_local
    """

    _check_requirements()

    if force != "yes":
        message = "This will replace your local database '%s' with the "\
            "remote '%s', are you sure [y/n]" % (env.local_psql_db, env.psql_db)
        answer = prompt(message, "y")

        if answer != "y":
            logger.info("Sync stopped")
            return

    init_tasks()  # Bootstrap fabrik

    # Create database dump
    remote_file = "postgresql/sync_%s.sql.tar.gz" % int(time.time()*1000)
    remote_path = paths.get_backup_path(remote_file)

    env.run("mkdir -p %s" % paths.get_backup_path("postgresql"))

    with context_managers.shell_env(PGPASSWORD=env.psql_password):
        env.run("pg_dump -h localhost -Fc -f %s -U %s %s -x -O" % (
            remote_path, env.psql_user, env.psql_db
        ))

    local_path = "/tmp/%s" % remote_file

    # Download sync file
    get(remote_path, local_path)

    # Import sync file by performing the following task (drop, create, import)
    with context_managers.shell_env(PGPASSWORD=env.local_psql_password):
        elocal("pg_restore --clean -h localhost -d %s -U %s '%s'" % (
            env.local_psql_db,
            env.local_psql_user,
            local_path)
        )

    # Cleanup
    env.run("rm %s" % remote_path)
    elocal("rm %s" % local_path)

    # Trigger hook
    run_hook("postgres.after_sync_remote_to_local")

    logger.info("Sync complete")
Exemplo n.º 5
0
def sync_remote_to_local(force="no"):
    """
    Sync your remote postgres database with local

    Example:
        fabrik prod sync_remote_to_local
    """

    _check_requirements()

    if force != "yes":
        message = "This will replace your local database '%s' with the "\
            "remote '%s', are you sure [y/n]" % (env.local_psql_db, env.psql_db)
        answer = prompt(message, "y")

        if answer != "y":
            logger.info("Sync stopped")
            return

    init_tasks()  # Bootstrap fabrik

    # Create database dump
    remote_file = "postgresql/sync_%s.sql.tar.gz" % int(time.time() * 1000)
    remote_path = paths.get_backup_path(remote_file)

    env.run("mkdir -p %s" % paths.get_backup_path("postgresql"))

    with context_managers.shell_env(PGPASSWORD=env.psql_password):
        env.run("pg_dump -h localhost -Fc -f %s -U %s %s -x -O" %
                (remote_path, env.psql_user, env.psql_db))

    local_path = "/tmp/%s" % remote_file

    # Download sync file
    get(remote_path, local_path)

    # Import sync file by performing the following task (drop, create, import)
    with context_managers.shell_env(PGPASSWORD=env.local_psql_password):
        elocal("pg_restore --clean -h localhost -d %s -U %s '%s'" %
               (env.local_psql_db, env.local_psql_user, local_path))

    # Cleanup
    env.run("rm %s" % remote_path)
    elocal("rm %s" % local_path)

    # Trigger hook
    run_hook("postgres.after_sync_remote_to_local")

    logger.info("Sync complete")
Exemplo n.º 6
0
def restore_db(release=None):
    """
    Restores backup back to version, uses current version by default.
    """

    assert "mysql_user" in env, "Missing mysqL_user in env"
    assert "mysql_password" in env, "Missing mysql_password in env"
    assert "mysql_host" in env, "Missing mysql_host in env"
    assert "mysql_db" in env, "Missing mysql_db in env"

    if not release:
        release = paths.get_current_release_name()

    if not release:
        raise Exception("Release %s was not found" % release)

    backup_file = "mysql/%s.sql.gz" % release
    backup_path = paths.get_backup_path(backup_file)

    if not env.exists(backup_path):
        raise Exception("Backup file %s not found" % backup_path)

    env.run("gunzip < %s | mysql -u %s -p%s -h %s %s" %
            (backup_path, env.mysql_user, env.mysql_password, env.mysql_host,
             env.mysql_db))
Exemplo n.º 7
0
def restore_db(release=None):
    """
    Restores backup back to version, uses current version by default.
    """

    if not release:
        release = paths.get_current_release_name()

    if not release:
        raise Exception("Release %s was not found" % release)

    backup_file = "postgresql/%s.sql.gz" % release
    backup_path = paths.get_backup_path(backup_file)

    if not env.exists(backup_path):
        raise Exception("Backup file %s not found" % backup_path)

    with context_managers.shell_env(PGPASSWORD=env.psql_password):
        env.run("pg_restore --clean -h localhost -d %s -U %s '%s'" %
                (env.psql_db, env.psql_user, backup_path))
Exemplo n.º 8
0
def restore_db(release=None):
    """
    Restores backup back to version, uses current version by default.
    """

    if not release:
        release = paths.get_current_release_name()

    if not release:
        raise Exception("Release %s was not found" % release)

    backup_file = "postgresql/%s.sql.gz" % release
    backup_path = paths.get_backup_path(backup_file)

    if not env.exists(backup_path):
        raise Exception("Backup file %s not found" % backup_path)

    with context_managers.shell_env(PGPASSWORD=env.psql_password):
        env.run("pg_restore --clean -h localhost -d %s -U %s '%s'" % (
            env.psql_db,
            env.psql_user,
            backup_path)
        )