def _backup(backupname):
    """Backs up the database with pg_dump."""
    # We have some restrictions on the backupname
    if re.match('[a-z0-9_-]+$', backupname) is None:
        click.secho('Invalid backupname.', fg='red')
        sys.exit(1)

    # The file must not exist
    filename = os.path.join(BACKUP_DIR, backupname)
    if os.path.isfile(filename):
        click.secho('File %s exists.' % filename, fg='red')
        sys.exit(1)

    params = ['pg_dump', '-h', SOCKET_DIR, '-O', '-x', '-U', 'postgres',
              'django']
    with open(filename, 'w') as f, running_db():
        ret = subprocess.call(params, stdout=f, preexec_fn=setuser('postgres'))

    uid, gid, _ = id('postgres')
    os.chown(filename, uid, gid)

    if ret == 0:
        click.secho('Successful backup: %s' % filename, fg='green')
    else:
        try:
            os.remove(filename)
        except:
            pass
        click.secho('Backup (%s) failed' % filename, fg='red')
        sys.exit(1)
Exemple #2
0
def _backup(backupname):
    """Backs up the database with pg_dump."""
    # We have some restrictions on the backupname
    if re.match('[a-z0-9_-]+$', backupname) is None:
        click.secho('Invalid backupname.', fg='red')
        sys.exit(1)

    # The file must not exist
    filename = os.path.join(BACKUP_DIR, backupname)
    if os.path.isfile(filename):
        click.secho('File %s exists.' % filename, fg='red')
        sys.exit(1)

    params = [
        'pg_dump', '-h', SOCKET_DIR, '-O', '-x', '-U', 'postgres', 'django'
    ]
    with open(filename, 'w') as f, running_db():
        ret = subprocess.call(params, stdout=f, preexec_fn=setuser('postgres'))

    uid, gid, _ = id('postgres')
    os.chown(filename, uid, gid)

    if ret == 0:
        click.secho('Successful backup: %s' % filename, fg='green')
    else:
        try:
            os.remove(filename)
        except:
            pass
        click.secho('Backup (%s) failed' % filename, fg='red')
        sys.exit(1)
Exemple #3
0
def init(stopper):
    for f in os.listdir(SECRET_FILES_ROOT):
        fn = os.path.join(SECRET_FILES_ROOT, f)
        shutil.copy(fn, '/.ssh')
        fn = os.path.join('/.ssh', f)
        uid, gid, _ = id('ssh')
        os.chown(fn, uid, gid)