- 论坛徽章:
- 0
|
本帖最后由 tomasea 于 2011-04-26 22:36 编辑
写了个备份的脚本,使用方法
[user@localhost dir]$ python newbackup.py
Please a target directory:/tmp/mybackup
Already create the dir /tmp/mybackup
Please a source directory:/tmp/newbackup
you have input a directory '/tmp/newbackup'
Please input the file comment:linshi
tar cvf /tmp/mybackup/linshi20110216.tar.gz /tmp/newbackup- #!/usr/bin/python
- '''To backup one thing.
- are you ready'''
- import os,sys,time
- def target_dir():
- global target
- target=raw_input("Please a target directory:")
- while len(target)==0:
- print "You didn't input anything!"
- target=raw_input("Please a target directory:")
- else:
- if os.path.exists("%s" %target):
- print "you have input a directory '%s'" %target
- else:
- os.mkdir("%s" %target)
- print "Already create the dir %s" %target
- def source_dir():
- global source
- source=raw_input("Please a source directory:")
- while len(source)==0:
- print "You didn't input anything!"
- target=raw_input("Please a source directory:")
- else:
- if os.path.exists("%s" %source):
- print "you have input a directory '%s'" %source
- else:
- os.mkdir("%s" %source)
- print "Already create the dir %s" %source
- def comment():
- global comment
- comment=raw_input("Please input the file comment:")
- while len(comment)==0:
- print "You must input a comment about the backup file"
- comment=raw_input("Please input the file comment:")
- def backup():
- timer=time.strftime('%Y%m%d')
- target_file=target+os.sep+comment+timer+'.tar.gz'
- source_dir=source
- tar_command="tar cvf %s %s" %(target_file,source_dir)
- print tar_command
- if os.system(tar_command)==0:
- print "success to tar the source dir"
- else:
- print "Backup Failed"
- if __name__=='__main__':
- target_dir()
- source_dir()
- comment()
- backup()
复制代码 |
|