2010-03-13 5 views
0
#Filename:backup_ver1 

import os 
import time 

#1 Using list to specify the files and directory to be backed up 
source = r'C:\Documents and Settings\rgolwalkar\Desktop\Desktop\Dr Py\Final_Py' 

#2 define backup directory 
destination = r'C:\Documents and Settings\rgolwalkar\Desktop\Desktop\PyDevResourse' 

#3 Setting the backup name 
targetBackup = destination + time.strftime('%Y%m%d%H%M%S') + '.rar' 

rar_command = "rar.exe a -ag '%s' %s" % (targetBackup, ''.join(source)) 
##i am sure i am doing something wrong here - rar command please let me know 

if os.system(rar_command) == 0: 
    print 'Successful backup to', targetBackup 
else: 
    print 'Backup FAILED' 

O/P:- Backup FAILED 

WinRAR과뿐만 아니라 환경 변수에서 경로 및 CLASSPATH에 추가됩니다 디렉토리 - 디렉토리를 백업하기위한 제안과 다른 사람파이썬 스크립트는 백업

+0

괜찮 았어. (tarfile 모듈을 가져 왔어.) 조용히 달렸다. 메시지를 생성하기 위해 아무것도 추가하지 않았기 때문에 나는 메시지를주지 않았다. 나는 목적지를 확인했다. = os.path.join (루트, Documents and Settings ","rgolwalkar ","Desktop ","Desktop ","PyDevResourse ") 그러나 백업이 없습니다. os.system (tar) == 0이면 : 'Successful backup to', targetBackup else : 'Backup FAILED'인쇄 - os.system (tar) == 1이면 오류가 발생합니다. : TypeError : system() 인수 1은 TarFile이 아닌 string이어야합니다. – rgolwalkar

+0

궁금한데 왜 파이썬에 내장 된 zip과 같은 플랫폼이 아닌 rar를 사용하고 있습니까? –

답변

0

source 디렉토리에 공백이 포함 된 환영이지만, 명령 줄에서 따옴표를 사용하지 마십시오. 이는 백업 실패의 원인 일 수 있습니다. 증분 백업을 만들 수있는, 자신의 백업 스크립트는 rdiff는 백업라는 파이썬 도구를 사용할 수를 쓰는 어쩌면 대신

subprocess.call(['rar.exe', 'a', '-ag', targetBackup, source]) 
2

이 같은 문제를 방지하기 위해

대신 os.systemsubprocess 모듈을 사용할 수 있습니까?

0

압축 알고리즘이 뭔가 다른 것일 수도 있고 디렉토리를 백업하는 것이면 파이썬 자체의 tar와 gzip을 사용하지 않을까요? 예를 들어

import os 
import tarfile 
import time 
root="c:\\" 
source=os.path.join(root,"Documents and Settings","rgolwalkar","Desktop","Desktop","Dr Py","Final_Py") 
destination=os.path.join(root,"Documents and Settings","rgolwalkar","Desktop","Desktop","PyDevResourse") 
targetBackup = destination + time.strftime('%Y%m%d%H%M%S') + 'tar.gz'  
tar = tarfile.open(targetBackup, "w:gz") 
tar.add(source) 
tar.close() 

그런 식으로, 당신은 시스템에 rar.exe에 의존하지 않습니다.

+0

고맙습니다 ghostdog74-- 코드를 확인하고 그런 식으로 실행 해 봅니다. O/P를 확인하고 붙여 넣습니다. – rgolwalkar

+0

ghostdog74 - 오류가 발생합니다. - 추적 (가장 최근 통화 마지막) : 파일 "C : \ Documents and Settings \ rgolwalkar \ Desktop \ Desktop \ Dr Py \ Final_Py \ backup_ver1.py", 줄 16, tar = tarfile.open (targetBackup, "w : gz") NameError : name 'tarfile '정의되지 않았습니다. – rgolwalkar

+0

tarfile은 모듈입니다.'import tarfile'처럼 가져 오십시오. – ghostdog74