def backup(): isfile = os.path.exists(NAME_FILE + ".dt") if isfile: new_name_file = NAME_FILE + " " + str(datetime.datetime.today().strftime("%d_%m_%Y %H_%M_%S")) + ".dt" os.rename(NAME_FILE + ".dt", new_name_file) args = shlex.split(SCRIPT_FILE) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p.communicate() if os.path.exists(NAME_FILE + ".dt"): add_record_register() new_name_file = NAME_FILE + " " + str(datetime.datetime.today().strftime("%d_%m_%Y %H_%M_%S")) + ".dt" os.rename(NAME_FILE + ".dt", new_name_file) try: from YaDiskClient import YaDisk disk = YaDisk(YA_USER, YA_PASS) disk.upload(new_name_file, '/' + new_name_file) except: add_record_logs(u'Ошибка отправки файла на Яндекс.Диск') if os.path.exists(NAME_FILE_LOG + ".txt"): try: # TODO доделать f = open(NAME_FILE_LOG + ".txt") r = f.read().decode('windows-1251') f.close() add_record_logs(r) os.remove(NAME_FILE_LOG + ".txt") except: pass
def backup(): isfile = os.path.exists(NAME_FILE + ".dt") if isfile: new_name_file = NAME_FILE + " " + str( datetime.datetime.today().strftime("%d_%m_%Y %H_%M_%S")) + ".dt" os.rename(NAME_FILE + ".dt", new_name_file) args = shlex.split(SCRIPT_FILE) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p.communicate() if os.path.exists(NAME_FILE + ".dt"): add_record_register() new_name_file = NAME_FILE + " " + str( datetime.datetime.today().strftime("%d_%m_%Y %H_%M_%S")) + ".dt" os.rename(NAME_FILE + ".dt", new_name_file) try: from YaDiskClient import YaDisk disk = YaDisk(YA_USER, YA_PASS) disk.upload(new_name_file, '/' + new_name_file) except: add_record_logs(u'Ошибка отправки файла на Яндекс.Диск') if os.path.exists(NAME_FILE_LOG + ".txt"): try: # TODO доделать f = open(NAME_FILE_LOG + ".txt") r = f.read().decode('windows-1251') f.close() add_record_logs(r) os.remove(NAME_FILE_LOG + ".txt") except: pass
def upload_to_yadisk(uploads_files, config): if ast.literal_eval(config['yadisk']['on']): yd = YaDisk(config['yadisk']['user'],config['yadisk']['password']) try: yd.mkdir(config['yadisk']['directory']) except Exception as error: print '{0}'.format(error) for file in uploads_files: print 'Upload ' + file + ' to YaDisk' try: yd.upload(file, '%s/%s' % ( config['yadisk']['directory'], os.path.basename(os.path.abspath(file)))) print 'Upload ' + file + ' successful!' logging.info('Upload ' + '{0}' + ' successful finished'.format(file)) except Exception as error: print 'Upload error...' + '\n' + '{0}'.format(error) logging.error('upload to YaDisk error...' + '{0}'.format(error)) sys.exit()
def upload_to_yadisk(uploads_files, config): if ast.literal_eval(config['yadisk']['on']): yd = YaDisk(config['yadisk']['user'], config['yadisk']['password']) try: yd.mkdir(config['yadisk']['directory']) except Exception as error: print '{0}'.format(error) for file in uploads_files: print 'Upload ' + file + ' to YaDisk' try: yd.upload( file, '%s/%s' % (config['yadisk']['directory'], os.path.basename(os.path.abspath(file)))) print 'Upload ' + file + ' successful!' logging.info('Upload ' + '{0}' + ' successful finished'.format(file)) except Exception as error: print 'Upload error...' + '\n' + '{0}'.format(error) logging.error('upload to YaDisk error...' + '{0}'.format(error)) sys.exit()
print "running mongodump" print dump_cmd subprocess.check_call(dump_cmd, shell=True,stdout=subprocess.PIPE) dump_path=os.getcwd()+'/'+out_dump_dir+'/{0}/'.format(db_name) dump_files = [ f for f in os.listdir(dump_path) if os.path.isfile(os.path.join(dump_path,f)) ] if len(dump_files) == 0: print "No files to dump for {0}.{1}".format(db_name, coll_name) sys.exit() yadisk_client=YaDisk(LOGIN,PSWD) dest_path_temp="" for dest_path_dir in dest_path.split("/"): dest_path_temp += dest_path_dir+"/" try : yadisk_client.ls(dest_path_temp) except YaDiskException: yadisk_client.mkdir(dest_path_temp) exec_dir=os.getcwd() os.chdir(dump_path) gzipped_dump=time_prefix+".tar.gz" gzip_cmd='tar cfvz {1} {0} '.format(' '.join(dump_files), gzipped_dump) print "archive dumpped files to common tar.gz" print gzip_cmd subprocess.check_call(gzip_cmd, shell=True,stdout=subprocess.PIPE) print "upload to "+dest_path yadisk_client.upload(gzipped_dump, "%s/%s" % (dest_path, time_prefix+".tar.gz")) os.chdir(exec_dir) shutil.rmtree(out_dump_dir)
dbs = str( subprocess.Popen( "mysql -u"+ mysql_u +" -p"+ mysql_p +" -e'show databases;' | grep -v information_schema | grep -v Database", stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True ).communicate()[0] ) for db in dbs.split('\n'): if db and db != 'mysql' and db != 'performance_schema' and db != 'pma': db_file = date_today_str +"-www-"+ db +".sql.bz2" try_ = True try_i = 1 while try_ != False and try_i <= 5: subprocess.Popen( "mysqldump --skip-lock-tables -u"+ mysql_u +" -p"+ mysql_p +" "+ db +" | bzip2 -c > "+ db_file, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True ).communicate() time.sleep(sleep_time) if try_i % 2 == 0: disk.upload( os.path.abspath( db_file ), path_webdav_today + db_file ) # заливаем на ЯДиск else: subprocess.Popen( "curl --user "+ yd_u +":"+ yd_p +" -T "+ os.path.abspath(db_file) +" https://webdav.yandex.ru"+ path_webdav_today, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True ).communicate() os.remove( os.path.abspath(db_file) ) # удаляем файл с сервера try: for today_dump_file in disk.ls( path_webdav_today ): if not today_dump_file.get('isDir') and today_dump_file.get('displayname') == db_file: try_ = False except YaDiskException as e: if e.code == 404 or e.code == 500: continue try_i += 1 ######### <<
from utils import get_full_pash import config BACKUP_FOLDER = "backup" def make_sql_beckup(): p = subprocess.Popen([get_full_pash('db_tables_backup.sh')], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() files = [] for f in out.split(): gz_name = "%s.gz" % f files.append(gz_name) return files if __name__ == "__main__": disk = YaDisk(token=config.token) for file_path in make_sql_beckup(): db_name = file_path.split("/")[-2] name = file_path.split("/")[-1] server_name = socket.gethostname() folder_path = os.path.join(BACKUP_FOLDER, server_name, db_name) for folder in [BACKUP_FOLDER, os.path.join(BACKUP_FOLDER, server_name), folder_path]: try: disk.mkdir(folder) except YaDiskException as e: print folder, "already exist" disk.upload(file_path, os.path.join(folder_path, name))
try_ = True try_i = 1 while try_ != False and try_i <= 5: subprocess.Popen("mysqldump --skip-lock-tables -u" + mysql_u + " -p" + mysql_p + " " + db + " | bzip2 -c > " + db_file, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True).communicate() time.sleep(sleep_time) if try_i % 2 == 0: disk.upload(os.path.abspath(db_file), path_webdav_today + db_file) # заливаем на ЯДиск else: subprocess.Popen("curl --user " + yd_u + ":" + yd_p + " -T " + os.path.abspath(db_file) + " https://webdav.yandex.ru" + path_webdav_today, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True).communicate() os.remove(os.path.abspath(db_file)) # удаляем файл с сервера try: for today_dump_file in disk.ls(path_webdav_today): if not today_dump_file.get(