版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u012896330/article/details/74915725
問題列表:
1.需要備份的文件和目錄列表指定
2.備份應存放在主備份目錄裏
3.備份的壓縮格式 zip 或 rar
4.zip 或 rar 存檔命令
5.維護環節:採用更好的文件名機制,使用時間作爲文件名,而當前的日期作爲目錄名,存放在主備份目錄中,這樣做的優勢是備份會以等級結構存儲,易於管理。
代碼部分:
#coding:utf-8
import os
import time
source = [r'C:\test.txt']
target_dir = r'E:\backup'
# 獲取系統時間
today = target_dir +time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
# 輸入備註
comment = raw_input('請輸入備註')
if len(comment) == 0:
print ('無備註')
target = today + os.sep + now + '.rar'
else:
target = today + os.sep + now + comment.replace('', '_') + '.rar'
if not os.path.exists(today):
os.mkdir(today)
print ('創建目錄' + today + '成功')
# 備份命令
#zip_command = "zip -qr '%s' %s" % (target, ''.join(source))
#rar_command = 'rar a{0} {1}'.format(target,source)
#rar_command = "rar a %s %s" % (target, ''.join(source))
rar_command = '"C:\Program Files (x86)\WinRAR\Rar.exe" a %s %s' % (target, ' '.join(source))
# Run the backup
print rar_command
if os.system(rar_command) == 0:
print('備份成功,存放在: ' + target)
else:
print os.system(rar_command)
print 'Backup FAILED'
慘遭問題:
因爲windows下的,所以下載的winrar,然後把執行文件放到window/system下,相當於配置環境變量, 一開始執行的備份命令爲rar_command = "rar a %s %s" % (target, ''.join(source)) 可是執行一直錯誤,cmd執行卻沒有事,百度了好多,字面都是這種執行命令,但這種壓根就沒有當做命令去處理,而是簡簡單單的字符串處理罷了。
錯誤提示:'zip' �����ڲ����ⲿ���Ҳ���ǿ����еij��� ���������ļ��� failed backup
當時心中一萬個 ***, 後面我直接把命令exe路徑填上,就可以了,希望對小哥們有用。