Exemple #1
0
def dump_db(dump_filename='db_dump.sql',
            for_rsync=False,
            tables=[],
            options=[],
            mode='w'):
    """Dump the database in the current working directory"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings(
    )
    if not db_engine.endswith('mysql'):
        print 'dump_db only knows how to dump mysql so far'
        sys.exit(1)
    dump_cmd = ['/usr/bin/mysqldump'] + options + [
        '--user='******'--password='******'--host=' + db_host
    ]
    try:
        port = int(db_port)
        dump_cmd.append('--port=' + port)
    except (TypeError, ValueError):
        pass

    # this option will mean that there will be one line per insert
    # thus making the dump file better for rsync, but slightly bigger
    if for_rsync:
        dump_cmd.append('--skip-extended-insert')
    dump_cmd.append(db_name)
    dump_cmd += tables

    dump_file = open(dump_filename, mode)
    if env['verbose']:
        print 'Executing dump command: %s\nSending stdout to %s' % (
            ' '.join(dump_cmd), dump_filename)

    _call_command(dump_cmd, stdout=dump_file)
    dump_file.close()
Exemple #2
0
def dump_db(dump_filename='db_dump.sql', for_rsync=False, tables=[], options=[], mode='w'):
    """Dump the database in the current working directory"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings()
    if not db_engine.endswith('mysql'):
        print 'dump_db only knows how to dump mysql so far'
        sys.exit(1)
    dump_cmd = ['/usr/bin/mysqldump'] + options + ['--user='******'--password='******'--host='+db_host]
    try:
        port = int(db_port)
        dump_cmd.append('--port=' + port)
    except (TypeError, ValueError):
        pass
         
    # this option will mean that there will be one line per insert
    # thus making the dump file better for rsync, but slightly bigger
    if for_rsync:
        dump_cmd.append('--skip-extended-insert')
    dump_cmd.append(db_name)
    dump_cmd += tables

    dump_file = open(dump_filename, mode)
    if env['verbose']:
        print 'Executing dump command: %s\nSending stdout to %s' % (' '.join(dump_cmd), dump_filename)

    _call_command(dump_cmd, stdout=dump_file)
    dump_file.close()
Exemple #3
0
def restore_db(dump_filename):
    """Restore a database dump file by name"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings()
    if not db_engine.endswith("mysql"):
        print "restore_db only knows how to restore mysql so far"
        sys.exit(1)
    restore_cmd = ["/usr/bin/mysql", "--user="******"--password="******"--host=" + db_host]
    if db_port != None:
        restore_cmd.append("--port=" + db_port)
    restore_cmd.append(db_name)

    dump_file = open(dump_filename, "r")
    if env["verbose"]:
        print "Executing dump command: %s\nSending stdin to %s" % (" ".join(restore_cmd), dump_filename)
    _call_command(restore_cmd, stdin=dump_file)
    dump_file.close()
Exemple #4
0
def restore_db(dump_filename):
    """Restore a database dump file by name"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings()
    if not db_engine.endswith('mysql'):
        sys.exit('restore_db only knows how to restore mysql so far')
    restore_cmd = ['/usr/bin/mysql', '--user='******'--password='******'--host='+db_host]
    if db_port != None:
        restore_cmd.append('--port='+db_port)
    restore_cmd.append(db_name)

    dump_file = open(dump_filename, 'r')
    if env['verbose']:
        print 'Executing dump command: %s\nSending stdin to %s' % (' '.join(restore_cmd), dump_filename)
    _call_command(restore_cmd, stdin=dump_file)
    dump_file.close()
Exemple #5
0
def dump_db(dump_filename="db_dump.sql"):
    """Dump the database in the current working directory"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings()
    if not db_engine.endswith("mysql"):
        print "dump_db only knows how to dump mysql so far"
        sys.exit(1)
    dump_cmd = ["/usr/bin/mysqldump", "--user="******"--password="******"--host=" + db_host]
    if db_port != None:
        dump_cmd.append("--port=" + db_port)
    dump_cmd.append(db_name)

    dump_file = open(dump_filename, "w")
    if env["verbose"]:
        print "Executing dump command: %s\nSending stdout to %s" % (" ".join(dump_cmd), dump_filename)
    _call_command(dump_cmd, stdout=dump_file)
    dump_file.close()
Exemple #6
0
def _call_wrapper(argv, **kwargs):
    if env["verbose"]:
        if hasattr(argv, "__iter__"):
            command = " ".join(argv)
        else:
            command = argv
        print "Executing command: %s" % command
    return _call_command(argv, **kwargs)
Exemple #7
0
def _call_wrapper(argv, **kwargs):
    if env['verbose']:
        if hasattr(argv, '__iter__'):
            command = ' '.join(argv)
        else:
            command = argv
        print "Executing command: %s" % command
    return _call_command(argv, **kwargs)
Exemple #8
0
def dump_db(dump_filename='db_dump.sql'):
    """Dump the database in the current working directory"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings()
    if not db_engine.endswith('mysql'):
        print 'dump_db only knows how to dump mysql so far'
        sys.exit(1)
    dump_cmd = ['/usr/bin/mysqldump', '--user='******'--password='******'--host='+db_host]
    if db_port != None:
        dump_cmd.append('--port='+db_port)
    dump_cmd.append(db_name)
    
    dump_file = open(dump_filename, 'w')
    if env['verbose']:
        print 'Executing dump command: %s\nSending stdout to %s' % (' '.join(dump_cmd), dump_filename)
    _call_command(dump_cmd, stdout=dump_file)
    dump_file.close()
Exemple #9
0
def _call_wrapper(argv, **kwargs):
    if env['verbose']:
        if hasattr(argv, '__iter__'):
            command = ' '.join(argv)
        else:
            command = argv
        print "Executing command: %s" % command
    return _call_command(argv, **kwargs)
Exemple #10
0
def db_exists(db_user, db_pw, db_name, db_port, db_host):
    db_exist_call = ['mysql', '-u', db_user, '-p'+db_pw]
    db_exist_call += ['--host=%s' % db_host]

    if db_port != None:
        db_exist_call += ['--port=%s' % db_port]

    db_exist_call += [db_name, '-e', 'quit']
    return _call_command(db_exist_call) == 0
Exemple #11
0
def db_exists(db_user, db_pw, db_name, db_port, db_host):
    db_exist_call = ["mysql", "-u", db_user, "-p" + db_pw]
    db_exist_call += ["--host=%s" % db_host]

    if db_port != None:
        db_exist_call += ["--port=%s" % db_port]

    db_exist_call += [db_name, "-e", "quit"]

    return _call_command(db_exist_call) == 0
Exemple #12
0
def _mysql_exec_as_root(mysql_cmd):
    """ execute a SQL statement using MySQL as the root MySQL user"""
    mysql_call = ['mysql', '-u', 'root', '-p'+_get_mysql_root_password()]
    mysql_call += ['--host=%s' % env['db_host']]
    
    if env['db_port'] != None:
        mysql_call += ['--port=%s' % env['db_port']]
    mysql_call += ['-e']
    if env['verbose']:
        print 'Executing MySQL command: %s' % ' '.join(mysql_call + [mysql_cmd])
    return _call_command(mysql_call + [mysql_cmd])
Exemple #13
0
def dump_db(dump_filename="db_dump.sql", for_rsync=False):
    """Dump the database in the current working directory"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings()
    if not db_engine.endswith("mysql"):
        print "dump_db only knows how to dump mysql so far"
        sys.exit(1)
    dump_cmd = ["/usr/bin/mysqldump", "--user="******"--password="******"--host=" + db_host]
    if db_port != None:
        dump_cmd.append("--port=" + db_port)
    # this option will mean that there will be one line per insert
    # thus making the dump file better for rsync, but slightly bigger
    if for_rsync:
        dump_cmd.append("--skip-extended-insert")
    dump_cmd.append(db_name)

    dump_file = open(dump_filename, "w")
    if env["verbose"]:
        print "Executing dump command: %s\nSending stdout to %s" % (" ".join(dump_cmd), dump_filename)
    _call_command(dump_cmd, stdout=dump_file)
    dump_file.close()
Exemple #14
0
def _mysql_exec_as_root(mysql_cmd):
    """ execute a SQL statement using MySQL as the root MySQL user"""
    mysql_call = ["mysql", "-u", "root", "-p" + _get_mysql_root_password()]
    mysql_call += ["--host=%s" % env["db_host"]]

    if env["db_port"] != None:
        mysql_call += ["--port=%s" % env["db_port"]]
    mysql_call += ["-e"]
    if env["verbose"]:
        print "Executing MySQL command: %s" % " ".join(mysql_call + [mysql_cmd])
    return _call_command(mysql_call + [mysql_cmd])
Exemple #15
0
def restore_db(dump_filename):
    """Restore a database dump file by name"""
    db_engine, db_name, db_user, db_pw, db_port, db_host = _get_django_db_settings(
    )
    if not db_engine.endswith('mysql'):
        print 'restore_db only knows how to restore mysql so far'
        sys.exit(1)
    restore_cmd = [
        '/usr/bin/mysql', '--user='******'--password='******'--host=' + db_host
    ]
    if db_port != None:
        restore_cmd.append('--port=' + db_port)
    restore_cmd.append(db_name)

    dump_file = open(dump_filename, 'r')
    if env['verbose']:
        print 'Executing dump command: %s\nSending stdin to %s' % (
            ' '.join(restore_cmd), dump_filename)
    _call_command(restore_cmd, stdin=dump_file)
    dump_file.close()
Exemple #16
0
def _get_mysql_root_password():
    # first try to read the root password from a file
    # otherwise ask the user
    if not env.has_key('root_pw'):
        file_exists = _call_command(['sudo', 'test', '-f', '/root/mysql_root_password'])
        if file_exists == 0:
            # note this requires sudoers to work with this - jenkins particularly ...
            root_pw = _capture_command(["sudo", "cat", "/root/mysql_root_password"])
            env['root_pw'] = root_pw.rstrip()
        else:
            env['root_pw'] = getpass.getpass('Enter MySQL root password:'******'root_pw']
Exemple #17
0
def _call_wrapper(argv, **kwargs):
    if env["verbose"]:
        print "Executing command: %s" % " ".join(argv)
    _call_command(argv, **kwargs)
Exemple #18
0
def _call_wrapper(argv, **kwargs):
    if env['verbose']:
        print "Executing command: %s" % ' '.join(argv)
    _call_command(argv, **kwargs)