示例#1
0
 def create_backup_mysql(self):
     file = ''
     data = {}
     try:
         db_name = connection.settings_dict['NAME']
         data_now = '{0:%Y_%m_%d}'.format(datetime.now())
         name_backup = "{}_{}.sql".format('backup', data_now)
         script = 'mysqldump -u user_bd -p123456 {} >{}'.format(
             db_name, name_backup)
         subprocess.run(script, shell=True)
         file = os.path.join(BASE_DIR, name_backup)
         check = DatabaseBackups.objects.filter(fecha=datetime.now())
         if check:
             data['error'] = 'Ya existe un respaldo realizado el dia de hoy'
         else:
             db = DatabaseBackups()
             db.user = self.request.user
             db.archive.save(name_backup,
                             File(open(file, 'rb')),
                             save=False)
             db.save()
     except Exception as e:
         data['error'] = str(e)
     finally:
         if len(file):
             os.remove(file)
     return data
示例#2
0
 def create_backup_mysql(self):
     file = ''
     data = {}
     try:
         db = connection.settings_dict['NAME']
         db_name = '{}{}{}'.format("'", db, "'")
         db_pass = connection.settings_dict['PASSWORD']
         db_user = connection.settings_dict['USER']
         db_host = connection.settings_dict['HOST']
         data_now = '{0:%Y_%m_%d}'.format(datetime.now())
         name_backup = "{}_{}.sql".format('backup', data_now)
         # script = 'mysqldump -u {} -h {} -p{} --set-gtid-purged=OFF --no-tablespaces {} > {}'.format(db_user, db_host, db_pass, db_name, name_backup)
         script = 'mysqldump -u {} -h {} --set-gtid-purged=OFF --no-tablespaces {} > {}'.format(db_user, db_host, db_name, name_backup)
         print(BASE_DIR)
         subprocess.run(script, shell=True)
         file = os.path.join('/home/megacentroferretero/', name_backup)
         check = DatabaseBackups.objects.filter(fecha=datetime.now())
         if check:
             data['error'] = 'Ya existe un respaldo realizado el dia de hoy'
         else:
             db = DatabaseBackups()
             db.user = self.request.user
             db.archive.save(name_backup, File(open(file, 'rb')), save=False)
             db.save()
     except Exception as e:
         print('')
         print('')
         print('')
         print(12)
         print(e)
         data['error'] = str(e)
     finally:
         if len(file):
             os.remove(file)
     return data
示例#3
0
 def create_backup_postgresql(self):
     file = ''
     data = {}
     try:
         db_name = connection.settings_dict['NAME']
         data_now = '{0:%Y-%m-%d_%H:%M:%S}'.format(datetime.now())
         name_backup = "{}_{}.backup".format('backup', data_now)
         script = 'pg_dump -h localhost -p 5432 -U postgres -F c -b -v -f "{}" {}'.format(name_backup, db_name)
         subprocess.call(script, shell=True)
         file = os.path.join(BASE_DIR, name_backup)
         db = DatabaseBackups()
         db.user = self.request.user
         db.archive.save(name_backup, File(open(file, 'rb')), save=False)
         db.save()
     except Exception as e:
         data['error'] = str(e)
     finally:
         if len(file):
             os.remove(file)
     return data