免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2498 | 回复: 3
打印 上一主题 下一主题

备份脚本 仅供参考 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-26 22:34 |只看该作者 |倒序浏览
本帖最后由 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
  1. #!/usr/bin/python
  2. '''To backup one thing.
  3.    are you ready'''
  4. import os,sys,time
  5. def target_dir():
  6. global target
  7. target=raw_input("Please a target directory:")
  8. while len(target)==0:
  9.    print "You didn't input anything!"
  10.    target=raw_input("Please a target directory:")
  11. else:
  12.    if os.path.exists("%s" %target):
  13.      print "you have input a directory '%s'" %target
  14.    else:
  15.      os.mkdir("%s" %target)
  16.      print "Already create the dir %s" %target

  17. def source_dir():
  18. global source
  19. source=raw_input("Please a source directory:")
  20. while len(source)==0:
  21.    print "You didn't input anything!"
  22.    target=raw_input("Please a source directory:")
  23. else:
  24.      if os.path.exists("%s" %source):
  25.         print "you have input a directory '%s'" %source
  26.      else:
  27.        os.mkdir("%s" %source)
  28.        print "Already create the dir %s" %source

  29. def comment():
  30. global comment
  31. comment=raw_input("Please input the file comment:")
  32. while len(comment)==0:
  33.    print "You must input a comment about the backup file"
  34.    comment=raw_input("Please input the file comment:")

  35. def backup():
  36. timer=time.strftime('%Y%m%d')
  37. target_file=target+os.sep+comment+timer+'.tar.gz'
  38. source_dir=source
  39. tar_command="tar cvf %s %s" %(target_file,source_dir)
  40. print tar_command
  41. if os.system(tar_command)==0:
  42.    print "success to tar the source dir"
  43. else:
  44.    print "Backup Failed"

  45. if __name__=='__main__':
  46. target_dir()
  47. source_dir()
  48. comment()
  49. backup()
复制代码

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:55:28IT运维版块每日发帖之星
日期:2016-07-29 06:20:00
2 [报告]
发表于 2011-04-27 08:29 |只看该作者
提三点建议:
1·python的运行环境指明,这个脚本应该是python2.x
2·最好能在脚本的前面定义形参,这样按需求修改起来比较方便
3·这种交互的方式是否实用?

论坛徽章:
0
3 [报告]
发表于 2011-04-27 10:20 |只看该作者
回复 2# ning_lianjie


    这个脚本只是供自己公司的不太会linux的人备份用的,对于他人实不实用就不知道了,不过我会参考你的建议,非常感谢

论坛徽章:
0
4 [报告]
发表于 2011-04-27 23:25 |只看该作者
  读了下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP