muugua 发表于 2018-8-10 06:46:08

python 脚本实现备份文件

'''  
功能:备份文件
  
版本:1.0
  
作者:白
  
'''
  
importos,time,sys
  
d_dir = '/data/backup/'
  
d_file = 'system_bak.tar.gz'
  
s_dir = ['/etc','/boot','/var/www/html']
  
date = time.strftime('%Y%m%d')
  
r_dir = d_dir + date + '/'
  
r_name = r_dir + d_file
  
def all_bak():
  
    print('Backup Scripts is Starting!~ Please Waitting......')
  
    print('\033[32m------------------------------------------\033[0m')
  
    time.sleep(2)
  
    if os.path.exists(r_dir) == False:
  
      os.makedirs(r_dir)
  
      print('The DIR {} create success!'.format(r_dir))
  
    else:
  
      print ('The DIR {} is exists!'.format(r_dir))
  

  
    tar_cmd = 'tar -czvf {} {}'.format(r_name,' '.join(s_dir))
  

  
    if os.system(tar_cmd)== 0 :
  
      #打印绿色输出成功信息!~
  
      print('\033[32mThe backup Files {} exec success!~\033[0m'.format(r_name))
  
    else:
  
      print("The backup Files is failed!~")
  
try:
  
    if len(sys.argv) == 0:
  
      print('\033))
  
except IndexError:
  
    print('\033[34m----------------------------------\033[0m')
  
    print('                     ')
  
    print('\033))
  
try:
  
    if sys.argv == 'all_bak':
  
      all_bak()
  
    else:
  
      print('\033[34m----------------------------------\033[0m')
  
      print('\033))
  
except IndexError:
  
    pass
页: [1]
查看完整版本: python 脚本实现备份文件