python備份指令碼

來源:互聯網
上載者:User

標籤:python

本文參考簡明python後修改

#!/usr/bin/pythonimport timeimport os# 1. The files and directories to be backed up are specified in a list.source = [‘/root‘, ‘/tmp‘]# 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 directorytarget_dir = ‘/mnt/e/backup/‘ # Remember to change this to what you will be using# 3. The files are backed up into a zip file.# 4. The current day is the name of the subdirectory in the main directorytoday = target_dir + time.strftime(‘%Y%m%d‘)# The current time is the name of the zip archivenow = time.strftime(‘%H%M%S‘)# Take a comment from the user to create the name of the zip filecomment = raw_input(‘Enter a comment --> ‘)if len(comment) == 0: # check if a comment was entered    target = today + os.sep + now + ‘.zip‘else:    target = today + os.sep + now + ‘_‘ +         comment.replace(‘ ‘, ‘_‘) + ‘.zip‘    # Notice the backslash!# Create the subdirectory if it isn‘t already thereif not os.path.exists(today):    os.makedirs(today) # make directory    print ‘Successfully created directory‘, today# 5. We use the zip command (in Unix/Linux) to put the files in a zip archivezip_command = "zip -qr ‘%s‘ %s" % (target, ‘ ‘.join(source))# Run the backupif os.system(zip_command) == 0:    print ‘Successful backup to‘, targetelse:    print ‘Backup FAILED‘
[[email protected] ~]# python backup.py Enter a comment --> weichenrongSuccessful backup to /mnt/e/backup/20161201/111319_weichenrong.zip[[email protected] ~]# python backup.py Enter a comment --> Successful backup to /mnt/e/backup/20161201/111341.zip[[email protected] ~]# [[email protected] ~]# ls /mnt/e/backup/20161201/111319_weichenrong.zip  111341.zip


本文出自 “天地冰寂” 部落格,請務必保留此出處http://yanconggod.blog.51cto.com/1351649/1878401

python備份指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.