Python 备份脚本
2014-09-16 15:42
302 查看
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Filename: bak_ws.py
import os
import time
import sys
# 1, The files and directories to be backed up are specified in a list
# source = ['/home/my_prog','/usr/local/mydata/']
print '-' * 32
source=[]
print 'The command line arguments are: '
for i in sys.argv:
print i
if i == sys.argv[0]:
continue
source.append(i)
print '-' * 32
# check input, if error app exit
if len(source) == 0:
print '''You should input the files or directories, like
python bak.py /home/my_prog /usr/local/mydata/ ...'''
exit()
else:
print 'Some files or directorier will be saved into .tar.gz format:'
print source
# If you are using Windows, use
# source=[r'c:/Documents', r'd:/work'] or
# something like that
# 2, The backup must be stored in a main backup directory
# Remember to change this to what you will be using
target_dir = '/data/backup/'
# 判断目录是否存在
if not os.path.exists(target_dir):
# 创建多级目录
os.makedirs(target_dir)
# 3, The files are backed up into a tar file
# 4, The name of subdirectory and tar file
today = time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the tar file
comment = raw_input('Enter a comment: ')
if len(comment) == 0:
target = target_dir + today + os.sep + now + '.tar.gz'
else:
target = target_dir + today + os.sep + comment.replace(' ','-') + '_' + now + '.tar.gz'
# Create the subdirectory if it isn't already there
if not os.path.exists(target_dir+today):
os.mkdir(target_dir+today)
print 'Successfully created directory',today
# 5, We use the tar command(in Unix/Linux) to put the files in a tgz archive
tar_command = "tar -zcf %s %s" % (target,' '.join(source))
# Run the backup
if os.system(tar_command) == 0:
print 'Successful backup to',target
else:
print 'Backup failed.'
# end
本文出自 “心静梵音” 博客,请务必保留此出处http://masters.blog.51cto.com/6516495/1553313
# -*- coding:UTF-8 -*-
# Filename: bak_ws.py
import os
import time
import sys
# 1, The files and directories to be backed up are specified in a list
# source = ['/home/my_prog','/usr/local/mydata/']
print '-' * 32
source=[]
print 'The command line arguments are: '
for i in sys.argv:
print i
if i == sys.argv[0]:
continue
source.append(i)
print '-' * 32
# check input, if error app exit
if len(source) == 0:
print '''You should input the files or directories, like
python bak.py /home/my_prog /usr/local/mydata/ ...'''
exit()
else:
print 'Some files or directorier will be saved into .tar.gz format:'
print source
# If you are using Windows, use
# source=[r'c:/Documents', r'd:/work'] or
# something like that
# 2, The backup must be stored in a main backup directory
# Remember to change this to what you will be using
target_dir = '/data/backup/'
# 判断目录是否存在
if not os.path.exists(target_dir):
# 创建多级目录
os.makedirs(target_dir)
# 3, The files are backed up into a tar file
# 4, The name of subdirectory and tar file
today = time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the tar file
comment = raw_input('Enter a comment: ')
if len(comment) == 0:
target = target_dir + today + os.sep + now + '.tar.gz'
else:
target = target_dir + today + os.sep + comment.replace(' ','-') + '_' + now + '.tar.gz'
# Create the subdirectory if it isn't already there
if not os.path.exists(target_dir+today):
os.mkdir(target_dir+today)
print 'Successfully created directory',today
# 5, We use the tar command(in Unix/Linux) to put the files in a tgz archive
tar_command = "tar -zcf %s %s" % (target,' '.join(source))
# Run the backup
if os.system(tar_command) == 0:
print 'Successful backup to',target
else:
print 'Backup failed.'
# end
本文出自 “心静梵音” 博客,请务必保留此出处http://masters.blog.51cto.com/6516495/1553313
相关文章推荐
- (windows)python脚本:自动备份并压缩文件,同时删除过期文件
- Python Mysql自动备份脚本
- python备份脚本解析
- Python 数据库备份脚本(邮件通知)
- Python备份脚本——版本一(Windows)
- 用python脚本对svn库进行备份,压缩,加密,ftp上传
- CSDN收藏备份Python脚本
- python备份脚本
- PYTHON备份脚本
- (windows)python脚本:自动备份并压缩文件,同时删除过期文件
- Python备份Mysql脚本
- Python 发送 RMAN 备份 Log 脚本
- Python 发送 RMAN 备份 Log 脚本
- windows和ubuntu下使用python写备份脚本
- Python 发送 RMAN 备份 Log 脚本
- 用python脚本定期备份文件
- windows和linux下的python备份脚本
- Python 发送 RMAN 备份 Log 脚本
- python 网站文件及数据库备份脚本
- Python 数据库备份脚本(邮件通知+日志记录)