unix上应用程序数据文件及日志备份脚本
#!/usr/bin/bash
##############################
#定期备份通讯、业务、数据处理进程的数据及日志
#作者:xxx
#创建时间:2013/7/4
#Ex: >chown a+x {/afc/timingDelDataLog.sh,/afc/timingDelDataLog.py}
# >export EDITOR=vi
# >crontab -e
# 30 0 * * * /afc/timingDelDataLog.sh&
# >svcadm disable cron
# >svcadm enable cron
##############################
cd /afc/
./timingDelDataLog.py >>/afc/log/timingDelDataLog.log
#!/usr/bin/python
#author:weihua.liu
#date:2013/07/03
#from datetime import *
import os,tarfile,bz2,shutil,time,datetime
arch_dir = '/afc/arch/'
cs_dataDir = '/afc/data/biiergsccs/'
bs_dataDir = '/afc/data/biiergscbs/'
ds_dataDir = '/afc/data/biiergscds/'
ds_logDir = '/afc/log/biiergscds/'
bs_logDir = '/afc/log/biiergscbs/'
tarEndStr='.tar.bz2'
#delete data file from saveDataCycle days before
saveDataCycle=30
def tarBackFile(tarFileName,compressDir):
archive = tarfile.open(tarFileName,'w:bz2')
archive.debug=1
archive.add(compressDir)
archive.close()
def backup_cs(archDate):
for file in os.listdir(cs_dataDir+'RCV'):
if archDate == file:
t_tarFileName=archDate+'-rcv'+tarEndStr
tarBackFile(t_tarFileName,cs_dataDir+'RCV/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergsccs/'+t_tarFileName)
shutil.rmtree(cs_dataDir+'RCV/'+archDate)
for file in os.listdir(cs_dataDir+'SNDS'):
if archDate == file:
t_tarFileName=archDate+'-snds'+tarEndStr
tarBackFile(t_tarFileName,cs_dataDir+'SNDS/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergsccs/'+t_tarFileName)
shutil.rmtree(cs_dataDir+'SNDS/'+archDate)
for file in os.listdir(cs_dataDir+'SNDF'):
if archDate == file:
t_tarFileName=archDate+'-sndf'+tarEndStr
tarBackFile(t_tarFileName,cs_dataDir+'SNDF/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergsccs/'+t_tarFileName)
shutil.rmtree(cs_dataDir+'SNDF/'+archDate)
def backup_ds(archDate):
for file in os.listdir(ds_dataDir+'NORMAL'):
if archDate == file:
t_tarFileName=archDate+'-normal'+tarEndStr
tarBackFile(t_tarFileName,ds_dataDir+'NORMAL/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergscds/'+t_tarFileName)
shutil.rmtree(ds_dataDir+'NORMAL/'+archDate)
for file in os.listdir(ds_dataDir+'REPACK'):
if archDate == file:
t_tarFileName=archDate+'-repack'+tarEndStr
tarBackFile(t_tarFileName,ds_dataDir+'REPACK/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergscds/'+t_tarFileName)
shutil.rmtree(ds_dataDir+'REPACK/'+archDate)
for file in os.listdir(ds_dataDir+'REPEAT'):
if archDate == file:
t_tarFileName=archDate+'-repeat'+tarEndStr
tarBackFile(t_tarFileName,ds_dataDir+'REPEAT/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergscds/'+t_tarFileName)
shutil.rmtree(ds_dataDir+'REPEAT/'+archDate)
for file in os.listdir(ds_dataDir+'ERROR'):
if archDate == file:
t_tarFileName=archDate+'-error'+tarEndStr
tarBackFile(t_tarFileName,ds_dataDir+'ERROR/'+archDate)
os.rename(t_tarFileName,arch_dir+'biiergscds/'+t_tarFileName)
shutil.rmtree(ds_dataDir+'ERROR/'+archDate)
t_tarLogFileName=archDate+'-dslog'+tarEndStr
t_compressLogFiles=''
for file in os.listdir(ds_logDir):
if file.count(archDate) > 0:
t_compressLogFiles=t_compressLogFiles+ds_logDir+file+' '
if t_compressLogFiles !='':
os.system('tar cvf '+t_tarLogFileName+' '+t_compressLogFiles)
os.rename(t_tarLogFileName,arch_dir+'biiergscds/'+t_tarLogFileName)
for file in os.listdir(ds_logDir):
if file.count(archDate)>0:
os.remove(ds_logDir+file)
def backup_bs(archDate1,archDate2):
for file in os.listdir(bs_dataDir):
if file.count(archDate1) > 0:
t_tarFileName=archDate2+'-tmp-'+file+tarEndStr
tarBackFile(t_tarFileName,bs_dataDir+file)
os.rename(t_tarFileName,arch_dir+'biiergscbs/'+t_tarFileName)
shutil.rmtree(bs_dataDir+file)
t_tarFileName=archDate2+'-a_sc'+tarEndStr
t_compressFiles=''
for file in os.listdir(bs_dataDir+'A_SC'):
if file.count(archDate2) > 0:
t_compressFiles=t_compressFiles+bs_dataDir+'A_SC/'+file+' '
if t_compressFiles !='':
os.system('tar cvf '+t_tarFileName+' '+t_compressFiles)
os.rename(t_tarFileName,arch_dir+'biiergscbs/'+t_tarFileName)
for file in os.listdir(bs_dataDir+'A_SC'):
if file.count(archDate2) > 0:
os.remove(bs_dataDir+'A_SC/'+file)
t_tarLogFileName=archDate2+'-bslog'+tarEndStr
t_compressLogFiles=''
for file in os.listdir(bs_logDir):
if file.count(archDate2) > 0:
t_compressLogFiles=t_compressLogFiles+bs_logDir+file+' '
if t_compressLogFiles !='':
os.system('tar cvf '+t_tarLogFileName+' '+t_compressLogFiles)
os.rename(t_tarLogFileName,arch_dir+'biiergscbs/'+t_tarLogFileName)
for file in os.listdir(bs_logDir):
if file.count(archDate2)>0:
os.remove(bs_logDir+file)
def getDateStr(formatStr):
archDateTime=datetime.datetime.now()-datetime.timedelta(days=saveDataCycle)
archDate=archDateTime.date()
return archDate.strftime(formatStr)
if __name__ == '__main__':
dateStr=getDateStr('%Y%m%d')
backup_cs(dateStr)
backup_ds(dateStr)
#spilt Ex:2013_6_20
dateStrNew=dateStr[0:4]+'_'+dateStr[4:6].lstrip('0')+'_'+dateStr[6:8].lstrip('0')
backup_bs(dateStrNew,dateStr)