python 备份脚本

#!/usr/bin/env python
#script name: backup_ssdb.py
#desc:        copy /data/ssdb 
#Time:        2017-08-10 09:58
#autho:       lei
import os
import datetime,time

source_dir = '/data/ssdb'
targat_dir = '/home/ssdb_backup/'

now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-7)
yes_time_nry = yes_time.strftime('%Y%m%d')

#copy /data/ssdb
copy_ssdb = 'cp -rf %s %s' % (source_dir,targat_dir)
os.system(copy_ssdb)

home_ssdb_dir = '%sssdb' % targat_dir

if os.path.exists(home_ssdb_dir):
    target = home_ssdb_dir + time.strftime('%Y%m%d') + '.tar.gz'

    tar_command = "tar czvf %s %s --exclude=log* --exclude=*log" %(target,source_dir)
    print tar_command

    if os.system(tar_command) == 0:
        print 'Successful backup to',target
    else:
        print 'Backup FAILED'
else:
    print 'directory does not exist'

#Delete /home/ssdb_backup/ssdb
delete_home_file = '%sssdb' % targat_dir
delete_home_ssdb = 'rm %s -rf ' % delete_home_file
os.system(delete_home_ssdb)

#Delete data one day before
yes_file = 'ssdb%s.tar.gz' % yes_time_nry
delte_dir = targat_dir + yes_file

del_file = 'rm %s -rf ' % delte_dir
os.system(del_file)

 

转载于:https://www.cnblogs.com/rayong/p/7800640.htmlhtml