原来是ruby写的, 考虑不太全, 现在重写了一下。 按时间过滤相关东西
#!/usr/bin/env python import os import re import time import tarfile import string bak_ser = "[email protected]" tar_dir = "/tmp/auto_tar_bak" ext_user = "/eda/bin/auto_bak.conf" config = {"deep":8, "exclude":"Code|INCA.libs", "filetype":"bmp|png|pdf|vsd|rar|log|dat|bak|sdb"} current_time = int(time.time()) def check_bak(bak_dir): tmp_list = [] for filepath,pathlist,filelist in os.walk(bak_dir): #filepath fullpath #pathlist fullpath child dir #filelist child dir file if filepath.count("/") >= config["deep"] or ".svn" in pathlist: continue for filename in filelist: if os.path.islink("%s/%s" % (filepath, filename)) or "/." in filepath or filename.startswith(".") or re.findall(config["filetype"], filename.split(".")[-1], re.I) or re.findall(config["exclude"], filepath): continue elif 120 <= os.path.getsize("%s/%s" % (filepath, filename)) <= 10485760 and current_time - os.stat("%s/%s" % (filepath, filename))[-2] < 172800 and istext("%s/%s" % (filepath, filename)): tmp_list.append("%s/%s" % (filepath, filename)) return tmp_list def istext(filename): s=open(filename).read(512) text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b")) _null_trans = string.maketrans("", "") if not s: # Empty files are considered text return True if "\0" in s: # Files with null bytes are likely binary return False # Get the non-text characters (maps a character to itself then # use the 'remove' option to get rid of the text characters.) t = s.translate(_null_trans, text_characters) # If more than 30% non-text characters, then # this is considered a binary file if float(len(t))/float(len(s)) > 0.30: return False return True def create_tarfile(tar_name, filename, tar_list): if not os.path.isdir(tar_name): os.makedirs(tar_name) bak_tar = tarfile.open(tar_name + filename, "w:bz2") for i in tar_list: bak_tar.add(i) bak_tar.close() def get_bak_dir(): tmp = [] with open("/etc/auto.nfs") as f: for i in f: if os.uname()[1] in i and not i.startswith("#"): tmp.append(i.split("/")[-1].rstrip()) return tmp if __name__ == "__main__": ext_list = [] with open(ext_user) as f: for i in f.read(): ext_list = i.split() for dirname in get_bak_dir(): if dirname in ext_list: continue file_list = check_bak("/local_home/" + dirname) if file_list: filename = time.strftime("/%F.tar.bz2", time.localtime()) create_tarfile(tar_dir + "/" + dirname, filename, file_list) os.system("rsync -az --remove-source-files %s/%s%s %s:/bak/auto_bak/%s/" %(tar_dir, dirname, filename, bak_ser, dirname))