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 也被添加到环境变量下的 Path 和 CLASSPATH - 任何其他建议备份目录的人都非常受欢迎

4

3 回答 3

2

也许您可以使用名为 rdiff-backup 的 python 工具来创建增量备份,而不是编写自己的备份脚本?

于 2010-03-13T11:53:04.787 回答
0

source目录包含空格,但在命令行中没有引号。这可能是备份失败的原因。

为避免此类问题,请使用subprocess模块而不是os.system

subprocess.call(['rar.exe', 'a', '-ag', targetBackup, source])
于 2010-03-13T11:15:34.230 回答
0

如果压缩算法可以是别的东西并且它只是为了备份一个目录,为什么不使用 python 自己的 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系统。

于 2010-03-13T12:13:07.687 回答