python實現(xiàn)備份目錄的方法
本文實例講述了python實現(xiàn)備份目錄的方法。分享給大家供大家參考。具體如下:
備份腳本1:
#!/usr/bin/python # Filename: backup_ver1.py import os import time # 1. The files and directories to be backed up are specified in a list. source = ['/home/swaroop/byte', '/home/swaroop/bin'] # 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 target_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 name of the zip archive is the current date and time target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip' # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive zip_command = "zip -qr '%s' %s" % (target, ' '.join(source)) # Run the backup if os.system(zip_command) == 0: print 'Successful backup to', target else: print 'Backup FAILED'
輸出:
$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip
備份腳本2:
#!/usr/bin/python # Filename: backup_ver2.py import os import time # 1. The files and directories to be backed up are specified in a list. source = ['/home/swaroop/byte', '/home/swaroop/bin'] # 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 target_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 directory today = target_dir + time.strftime('%Y%m%d') # The current time is the name of the zip 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) # make directory print 'Successfully created directory', today # The name of the zip file target = today + os.sep + now + '.zip' # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive zip_command = "zip -qr '%s' %s" % (target, ' '.join(source)) # Run the backup if os.system(zip_command) == 0: print 'Successful backup to', target else: print 'Backup FAILED'
輸出:
$ python backup_ver2.py
Successfully created directory /mnt/e/backup/20041208
Successful backup to /mnt/e/backup/20041208/080020.zip
$ python backup_ver2.py
Successful backup to /mnt/e/backup/20041208/080428.zip
備份腳本3:
#!/usr/bin/python # Filename: backup_ver4.py import os import time # 1. The files and directories to be backed up are specified in a list. source = ['/home/swaroop/byte', '/home/swaroop/bin'] # 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 target_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 directory today = target_dir + time.strftime('%Y%m%d') # The current time is the name of the zip archive now = time.strftime('%H%M%S') # Take a comment from the user to create the name of the zip file comment = 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 there if not os.path.exists(today): os.mkdir(today) # make directory print 'Successfully created directory', today # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive zip_command = "zip -qr '%s' %s" % (target, ' '.join(source)) # Run the backup if os.system(zip_command) == 0: print 'Successful backup to', target else: print 'Backup FAILED'
輸出:
$ python backup_ver4.py
Enter a comment --> added new examples
Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip
$ python backup_ver4.py
Enter a comment -->
Successful backup to /mnt/e/backup/20041208/082316.zip
希望本文所述對大家的Python程序設計有所幫助。
- 用python標準庫difflib比較兩份文件的異同詳解
- 通過Python模塊filecmp 對文件比較的實現(xiàn)方法
- python實現(xiàn)比較文件內(nèi)容異同
- Python比較文件夾比另一同名文件夾多出的文件并復制出來的方法
- Python實現(xiàn)比較兩個文件夾中代碼變化的方法
- Python 比較文本相似性的方法(difflib,Levenshtein)
- Python實現(xiàn)備份文件實例
- Python備份目錄及目錄下的全部內(nèi)容的實現(xiàn)方法
- Python實現(xiàn)配置文件備份的方法
- Python實現(xiàn)定期檢查源目錄與備份目錄的差異并進行備份功能示例
相關文章
Django集成Celery實現(xiàn)高效的異步任務處理的全過程
Django?作為一個強大的?Python?Web?框架,可以通過集成?Celery?這一異步任務隊列來優(yōu)化這些任務的處理,本文將深入探討如何在?Django?項目中集成?Celery,包括?Celery?的基本配置、定義任務、以及監(jiān)控任務執(zhí)行,需要的朋友可以參考下2023-11-11Python實現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中隊列的操作方法示例
這篇文章主要介紹了Python實現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中隊列的操作方法,結(jié)合實例形式演示了Python針對數(shù)據(jù)結(jié)構(gòu)中隊列的初始化、插入、刪除、判斷隊列滿及隊列空等相關操作技巧,需要的朋友可以參考下2017-12-12python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細使用方法與實例,需要的朋友可以參考下2020-03-03