备份mongo数据库
#!/usr/bin/env python
#coding:utf-8
import datetime
import os
import shutil
user = 'root'
passwd = '123456'
port = 28018
backup_path = '/data_1/backup_logs/mongodb/'
after_dbname = 'bz-dw-and-'
cx_path ='/data/bz-dw-and/'
now_time = datetime.datetime.now()
time = now_time.strftime("%Y%m%d-%H:%M")
for dir_name in os.listdir(cx_path):
all_backup_path = os.path.join(backup_path,dir_name)
db_name = after_dbname + dir_name
os.system("mongodump -u %s -p %s --port %s --authenticationDatabase admin -d %s -o %s" % (user,passwd,port,db_name,all_backup_path))
os.chdir(all_backup_path)
os.system("tar -zcf %s-%s.tar.gz %s" % (db_name,time,db_name))
shutil.rmtree(db_name)
#移动文件
#!/usr/bin/env python
#coding:utf-8
import os
import datetime
import shutil
data=r'/data/testlog'
data_1=r'/data_1/backup_logs'
after_time=datetime.datetime.now()-datetime.timedelta(days=15)
old_time=after_time.strftime('%Y%m%d')
for dir_name in os.listdir(data):
dest_file_path=os.path.join(data_1,dir_name)
if not os.path.exists(dest_file_path):
os.mkdir(dest_file_path)
dir_path=os.path.join(data,dir_name)
for (root,dirs,filename) in os.walk(dir_path):
for files in filename:
src_file_path=os.path.join(root,files)
time_chuo=os.path.getmtime(src_file_path)
time=datetime.datetime.fromtimestamp(time_chuo)
file_time=time.strftime('%Y%m%d')
if file_time < old_time:
shutil.move(src_file_path,dest_file_path)
#备份压缩目录
#!/usr/bin/env python
import os
import datetime
import re
import tarfile
log_dir=r'/data/testlog'
old_time=datetime.datetime.now()-datetime.timedelta(days=13)
after_time=datetime.datetime.now()-datetime.timedelta(days=7)
old_date=old_time.strftime('%Y%m%d')
after_date=after_time.strftime('%m%d')
file_name='allgame'+'-'+old_date+'-'+after_date+'-'+'logs.tar.gz'
tar = tarfile.open('/data/backup_logs/all_game_log/temp.tar.gz', 'w:gz')
for (root,dirs,filename) in os.walk(log_dir):
for files in filename:
for num in range(7,14):
old_datetime=datetime.datetime.now()-datetime.timedelta(days=num)
old_datetime_gsh=old_datetime.strftime('%Y%m%d')
if re.match('\w+'+old_datetime_gsh+'\w+',files):
abs_path=os.path.join(root,files)
tar.add(abs_path)
tar.close()
os.rename('/data/backup_logs/all_game_log/temp.tar.gz','/data/backup_logs/all_game_log/'+file_name)