def process(self):
        self.ensure_target_folder_exists()
        target_file_path = os.path.join(self.target_folder, self.backup_object.target_file_name)

        try:
            self.reporter.info("Backing up MySql database '{0}'...".format(self.backup_object.database))

            kwargs = {
                'user': self.backup_object.user,
                'password': self.backup_object.password,
                '_out': target_file_path
            }
            if self.backup_object.host is not None:
                kwargs['host'] = self.backup_object.host
            if self.backup_object.port is not None:
                kwargs['port'] = self.backup_object.port

            # noinspection PyUnresolvedReferences
            sh.mysqldump(self.backup_object.database, '--lock-tables', '--opt', '--skip-extended-insert', **kwargs)

            self.reporter.info("Database backup complete.")

        except sh.ErrorReturnCode as ex:
            raise Exception("MySQL backup for database '{0}' failed: {1}"
                            .format(self.backup_object.database, ex.message))
        except Exception as ex:
            raise Exception("MySQL backup for database '{0}' failed: {1}"
                            .format(self.backup_object.database, repr(ex)))
    def process(self):
        self.ensure_target_folder_exists()
        target_file_path = os.path.join(self.target_folder,
                                        self.backup_object.target_file_name)

        try:
            self.reporter.info("Backing up MySql database '{0}'...".format(
                self.backup_object.database))

            kwargs = {
                'user': self.backup_object.user,
                'password': self.backup_object.password,
                '_out': target_file_path
            }
            if self.backup_object.host is not None:
                kwargs['host'] = self.backup_object.host
            if self.backup_object.port is not None:
                kwargs['port'] = self.backup_object.port

            # noinspection PyUnresolvedReferences
            sh.mysqldump(self.backup_object.database, '--lock-tables', '--opt',
                         '--skip-extended-insert', **kwargs)

            self.reporter.info("Database backup complete.")

        except sh.ErrorReturnCode as ex:
            raise Exception(
                "MySQL backup for database '{0}' failed: {1}".format(
                    self.backup_object.database, ex.message))
        except Exception as ex:
            raise Exception(
                "MySQL backup for database '{0}' failed: {1}".format(
                    self.backup_object.database, repr(ex)))
Esempio n. 3
0
    def save_api(api):
        print("Saving {} database...".format(api['name']))
        name = api['bbdd']
        dump_file = '/tmp/{}_dump.sql'.format(api['name'])
        mysqldump('-u', DBUSER, '-p{}'.format(DBPWD), '-h', DBHOST, '-P', DBPORT, name, _out=dump_file)

        print('File {} created'.format(dump_file))
def dump_database(release, db_user, db_password):
    database_name = _database_name(release)
    release_dir = _release_dir(release)
    filename = _dump_filename(release)

    src_database = database_name
    target = release_dir + '/' + filename
    mysqldump("-u", db_user, "-p" + db_password, src_database, '--result-file',
              target)
Esempio n. 5
0
 def dump(self):
     super().dump()
     dumpfile = join(self.directory, "dump.sql")
     
     sh = self.sh
     if self.config['options']['dump_command_prefix']:
         dump_command_prefix = self.config['options']['dump_command_prefix'].split()
         sh = sh.Command(dump_command_prefix[0]).bake(*dump_command_prefix[1:])
     
     sh.mysqldump(
         "--all-databases", "--complete-insert",
         _out=dumpfile,
         **self.options()
     )
Esempio n. 6
0
 def mysql_dump(self):
     if self.configuration['backup'].get('mysqldump', None):
         logger = logging.getLogger('backup.mysqldump')
         destination = os.path.join(
             self.configuration['destination']['database'], 'mysqldump.sql')
         if self.arguments.dry_run:
             destination = None
         try:
             sh.mysqldump('--defaults-extra-file=~/.my.cnf',
                          '--all-databases',
                          _out=destination)
         except sh.ErrorReturnCode as exc:
             logger.error("Unable to perform mysqldump: %s", exc.stderr)
         else:
             logger.debug("Performed mysql dump")
Esempio n. 7
0
def backup(**kwargs):
    import sh
    bakdir = "/var/toughradius/databak"
    if not os.path.exists(bakdir):
        os.mkdir(bakdir)
    now = datetime.now()
    dbname = kwargs.pop('dbname','toughradius')
    ftphost = kwargs.pop('ftphost','127.0.0.1')
    ftpport = kwargs.pop('ftpport',21)
    ftpuser = kwargs.pop('ftpuser','')
    ftppwd = kwargs.pop('ftppwd','')
    backfile = '%s/%s-backup-%s.gz'%(bakdir,dbname,now.strftime( "%Y%m%d"))
    
    sh.gzip(sh.mysqldump(u='root',B=dbname,S="/var/toughradius/mysql/mysql.sock"),'-cf',_out=backfile)

    if '127.0.0.1' not in ftphost:
        ftp=FTP() 
        ftp.set_debuglevel(2)
        ftp.connect(ftphost,ftpport)
        ftp.login(ftpuser,ftppwd)
        ftp.cwd('/')
        bufsize = 1024
        file_handler = open(backfile,'rb')
        ftp.storbinary('STOR %s' % os.path.basename(backfile),file_handler,bufsize)
        ftp.set_debuglevel(0) 
        file_handler.close() 
        ftp.quit()
Esempio n. 8
0
def backup(**kwargs):
    import sh
    bakdir = "/var/toughradius/databak"
    if not os.path.exists(bakdir):
        os.mkdir(bakdir)
    now = datetime.now()
    dbname = kwargs.pop('dbname', 'toughradius')
    ftphost = kwargs.pop('ftphost', '127.0.0.1')
    ftpport = kwargs.pop('ftpport', 21)
    ftpuser = kwargs.pop('ftpuser', '')
    ftppwd = kwargs.pop('ftppwd', '')
    backfile = '%s/%s-backup-%s.gz' % (bakdir, dbname, now.strftime("%Y%m%d"))

    sh.gzip(sh.mysqldump(u='root',
                         B=dbname,
                         S="/var/toughradius/mysql/mysql.sock"),
            '-cf',
            _out=backfile)

    if '127.0.0.1' not in ftphost:
        ftp = FTP()
        ftp.set_debuglevel(2)
        ftp.connect(ftphost, ftpport)
        ftp.login(ftpuser, ftppwd)
        ftp.cwd('/')
        bufsize = 1024
        file_handler = open(backfile, 'rb')
        ftp.storbinary('STOR %s' % os.path.basename(backfile), file_handler,
                       bufsize)
        ftp.set_debuglevel(0)
        file_handler.close()
        ftp.quit()
Esempio n. 9
0
def mysqldump(user='******', password='', **kwargs):
    with BakHelper(kwargs['backupname'],
                   destination=kwargs['destination'],
                   password=password,
                   tags=["mysql"]) as bh:

        mysql_args = []

        mysql_kwargs = dict(
            u=user,
            B=kwargs['database'],
            _out="dump_{0}.sql".format(kwargs['database']))

        if kwargs.get('host') is not None:
            mysql_kwargs['h'] = kwargs['host']

        if kwargs.get('single_transaction'):
            mysql_args.append('--single-transaction')

        if kwargs.get('force'):
            mysql_args.append('--force')

        sh.mysqldump("-p{0}".format(password), *mysql_args, **mysql_kwargs)
        bh.backup()
Esempio n. 10
0
    def disableServiceFromUser(self, request, user):
        username = user.username.replace(".", "_")
        homedir = '/home/%s' % (user)
        cur = connection.cursor()

        try:
            cd(homedir)
            password = settings.DATABASES["default"]["PASSWORD"]

            try:
                s = mysqldump("-u", "root", "-p%s" % (password), username)
                sqldumpfile = open('sqldump.sql', 'w')
                for line in s:
                    sqldumpfile.write(line)
                cur.execute("USE mysql")

                try:
                    cur.execute("DROP DATABASE %s" % (username))

                    try:
                        stmt = "DROP USER '{}'@'%%'".format(username)
                        cur.execute(stmt)
                        mysql = MySQL()
                        tmp = Service.objects.filter(user=user, servicetype=mysql.getServiceType())[0]
                        tmp.delete()
                        sendInfoMaterial(self, "Your MySQL-service has been disabled.", user)
                        messages.success(request, "MySQL service removed from user. SQLdump has been made")

                    except ErrorReturnCode:
                        messages.error(request, "Could'nt remove user")

                except ErrorReturnCode:
                    messages.error(request, "Could'nt remove user's database.")

            except ErrorReturnCode:
                messages.error(request, "MySQLdump failed.")

        except ErrorReturnCode:
            messages.error(request, "%s does'nt exist!" % (homedir))

        finally:
            cur.execute("USE %s" % (self.default_database))
            cur.close()
Esempio n. 11
0
# -*- coding: utf-8 -*-

from yunbk import YunBK
from yunbk.backend.local import LocalBackend

import sh

backend = LocalBackend('/data/backup')

with YunBK('mysql', [backend]) as ybk:
    sh.mysqldump(u='root',
                 all_databases=True,
                 _out="dump.sql")
    ybk.backup()
Esempio n. 12
0
logger.setLevel(logging.DEBUG)

backend = LocalBackend('/data/backup')
with YunBK('yb', [backend], keeps=KEEPS_NORMAL) as ybk:
    # 生成文件
    with open('t.txt', 'w') as f:
        f.write('ok')

    # 备份文件
    shutil.copy('/data/store/db.sqlite3', 'db.sqlite3')

    # 备份目录,如redis
    shutil.copytree('/data/store/redis', 'redis')

    # 备份mysql所有库
    sh.mysqldump(u='root', all_databases=True, _out="dump_all.sql")

    # 备份mysql某个库
    sh.mysqldump('db1',
                 "--ignore-table=db1.tb1",
                 u='root',
                 p='passwd',
                 _out="dump_db1.sql")

    # 备份mongodb
    sh.mongodump(
        u='root',
        p='passwd',
        h='127.0.0.1',
        port=27017,
        d='db1',  # 不传-d参数即备份所有库
Esempio n. 13
0
# Backup settings
DESTINATION = "glacier"
BACKUP_NAME = "full_backup"
BACKUP_PASS = "******"
MYSQL_DB    = "foo_db"

# Creates a temporary folder at /tmp to store the packed backup
TMP = "/tmp/{0}/{1}".format(random.getrandbits(32), BACKUP_NAME)
sh.mkdir("-p", TMP)

# Copy your backup files to the TMP
sh.cp("-R", "/var/www/website/uploads", TMP)

# Execute a MySQL dump and store at TMP
sh.mysqldump("--defaults-extra-file=/root/.database.cnf",
             MYSQL_DB,
             _out=TMP+"/dump.sql")

# The conf=bakthat_conf will not be specified. We'll use the settings on 
# ~/.bakthat.yml (which can be configured with $ bakthat configure).
with BakHelper(BACKUP_NAME,
               destination=DESTINATION,
               password=BACKUP_PASS,
               tags=["public-uploads", "public-ipad", "mysql"]) as bh:
  bh.backup(TMP)
  bh.rotate()

# Removes the temporary folder
sh.rm("-r", TMP)
Esempio n. 14
0
backend = LocalBackend('/data/backup')
with YunBK('yb', [backend], keeps=KEEPS_NORMAL) as ybk:
    # 生成文件
    with open('t.txt', 'w') as f:
        f.write('ok')

    # 备份文件
    shutil.copy('/data/store/db.sqlite3', 'db.sqlite3')

    # 备份目录,如redis
    shutil.copytree('/data/store/redis', 'redis')

    # 备份mysql所有库
    sh.mysqldump(
        u='root',
        all_databases=True,
        _out="dump_all.sql"
    )

    # 备份mysql某个库
    sh.mysqldump(
        'db1',
        "--ignore-table=db1.tb1",
        u='root',
        p='passwd',
        _out="dump_db1.sql"
    )

    # 备份mongodb
    sh.mongodump(
        u='root',
Esempio n. 15
0
backend = LocalBackend('/data/backup')
with YunBK('yb', [backend], keeps=KEEPS_NORMAL) as ybk:
    # 生成文件
    with open('t.txt', 'w') as f:
        f.write('ok')

    # 备份文件
    shutil.copy('/data/store/db.sqlite3', 'db.sqlite3')

    # 备份目录,如redis
    shutil.copytree('/data/store/redis', 'redis')

    # 备份mysql所有库
    sh.mysqldump(
        u='root',
        all_databases=True,
        _out="dump_all.sql"
    )

    # 备份mysql某个库
    sh.mysqldump(
        'db1',
        u='root',
        p='passwd',
        _out="dump_db1.sql"
    )

    # 备份mongodb
    sh.mongodump(
        u='root',
        p='passwd',
Esempio n. 16
0
logger.setLevel(logging.DEBUG)

backend = LocalBackend('/data/backup')
with YunBK('yb', [backend], keeps=KEEPS_NORMAL) as ybk:
    # 生成文件
    with open('t.txt', 'w') as f:
        f.write('ok')

    # 备份文件
    shutil.copy('/data/store/db.sqlite3', 'db.sqlite3')

    # 备份目录,如redis
    shutil.copytree('/data/store/redis', 'redis')

    # 备份mysql所有库
    sh.mysqldump(u='root', all_databases=True, _out="dump_all.sql")

    # 备份mysql某个库
    sh.mysqldump('db1', u='root', p='passwd', _out="dump_db1.sql")

    # 备份mongodb
    sh.mongodump(
        u='root',
        p='passwd',
        h='127.0.0.1',
        port=27017,
        d='db1',  # 不传-d参数即备份所有库
        o='mongo_dump',
    )

    ybk.backup()
Esempio n. 17
-1
def generate_backup(config):
    """ Generates the database dump. """

    print "Generating database dump."

    # dumps folder creation
    dumps_folder = "{}/cron_db_dumps".format(expanduser('~'))
    try:
        mkpath(dumps_folder)
    except DistutilsFileError:
        print "Error: can't mkdir {}".format(dumps_folder)
        return False

    # dump name generation
    db_name = config.get('db', 'name')
    dump_name = strftime("%Y-%m-%d-%H:%M:%S", gmtime())
    dump_name = "{}/{}-{}.dump".format(dumps_folder, db_name, dump_name)

    # db engine
    db_engine = config.get('db', 'engine')

    if db_engine == 'postgresql':
        dump_format = '-Fc'  # custom format
        sh.pg_dump(dump_format, db_name, '-f', dump_name)
    elif db_engine == 'mysql':
        db_user = config.get('db', 'user')
        db_password = config.get('db', 'password')
        sh.mysqldump('--compress', '-u', db_user, '--password={}'.format(
            db_password), db_name, '--result-file={}'.format(dump_name))
    else:
        print "Error: DB engine not supported."
        return False

    # gzip dump
    print "Compressing database dump."
    with open(dump_name, 'rb') as dump_file:
        dump_gzipped = gzip.open("{}.gz".format(dump_name), 'wb')
        dump_gzipped.writelines(dump_file)
        dump_gzipped.close()

    # remove raw file
    remove(dump_name)

    return "{}.gz".format(dump_name)