码迷,mamicode.com
首页 > 编程语言 > 详细

Linux环境数据备份Python脚本

时间:2015-06-08 17:09:52      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:


#!/usr/bin/python
#Filename:backupscript.py
import os
import time

# The files and directories to be backed up are specified in a list.
source = [‘/data/‘]

# The backup must be stored in a main backup directory
target_dir = ‘/mnt/backup/‘

# The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime(‘%Y_%m_%d‘)

# The current time is the name of the tar archive
now = time.strftime(‘%H_%M_%S‘)

 

# Create the subdirectory if it isn‘t already there

if not os.path.exists(today):
  os.mkdir(today)
  print ‘Successfully created directory‘, today

# The name of the tar file

target = today + os.sep + now +‘.tar.gz‘

# We use the tar command (in Unix/Linux) to put the files in a tar archive

tar_command = "tar zcvf ‘%s‘ %s" % (target, ‘ ‘.join(source))

if os.system(tar_command) == 0:
  print ‘Successful backup to‘, target
else:
  print ‘Backup failed‘

Linux环境数据备份Python脚本

标签:

原文地址:http://www.cnblogs.com/songyuejie/p/4561016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!