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)
while try_ != False and try_i <= 5: try: disk.ls( path_webdav_today ) try_ = False except YaDiskException as e: if e.code == 404: dir_webdav = '/' for dir in path_webdav_today.split('/'): if dir: dir_webdav += dir +"/" try: dir_webdav_ls = disk.ls( dir_webdav ) except YaDiskException as e: try: disk.mkdir( dir_webdav ) try_ = False except YaDiskException as e: if e.code == 500: try_ = True try_i += 1 ######### << ######### >> Сохраняем БД и заливаем на ЯДиск if backup_db: 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"
from YaDiskClient import YaDisk, YaDiskException 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))
while try_ != False and try_i <= 5: try: disk.ls(path_webdav_today) try_ = False except YaDiskException as e: if e.code == 404: dir_webdav = '/' for dir in path_webdav_today.split('/'): if dir: dir_webdav += dir + "/" try: dir_webdav_ls = disk.ls(dir_webdav) except YaDiskException as e: try: disk.mkdir(dir_webdav) try_ = False except YaDiskException as e: if e.code == 500: try_ = True try_i += 1 ######### << ######### >> Сохраняем БД и заливаем на ЯДиск if backup_db: 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,