备份mongo数据库,移动文件,备份压缩目录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了备份mongo数据库,移动文件,备份压缩目录相关的知识,希望对你有一定的参考价值。

备份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:%S‘)

try:
    for dir_name in os.listdir(cx_path):
        all_backup_path = os.path.join(backup_path,dir_name)
        if not os.path.exists(all_backup_path):
            os.mkdir(all_backup_path)
        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,True)
except:
    print "数据库备份发生错误"

#移动文件

#!/usr/bin/env python
#coding:utf-8
import os,datetime,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,datetime,re,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)

以上是关于备份mongo数据库,移动文件,备份压缩目录的主要内容,如果未能解决你的问题,请参考以下文章

mongo的备份数据库导入现有数据库

Linux+Shell获取及拷贝最新备份数据

如何将Linux系统的多个目录及文件备份并压缩到一个文件,以方面保持和传递?

81. lvm+mysql你们是如何备份的

Linux 压缩解压及备份命令

Linux文件目录及文件系统的备份与恢复相关命令