【问题标题】:Python Script to backup a directory用于备份目录的 Python 脚本
【发布时间】:2010-03-13 10:45:01
【问题描述】:
#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 - 任何其他有备份目录建议的人都非常欢迎

【问题讨论】:

  • 好的,我做到了(导入了 tarfile 模块),它安静地运行——我没有给出任何消息,因为没有添加任何内容来产生消息——我检查了位置 destination=os.path。 join(root,"Documents and Settings","rgolwalkar","Desktop","Desktop","PyDevResourse") 但是没有找到备份。第二个如果我添加:- 如果 os.system(tar) == 0: print 'Successful backup to', targetBackup else: print 'Backup FAILED' --如果 os.system(tar) == 1 会出现错误: TypeError: system() 参数 1 必须是字符串,而不是 TarFile
  • 只是好奇,你为什么使用 rar 而不是 python 内置的 zip 等跨平台的东西?

标签: python windows-xp backup rar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 非常感谢ghostdog74——让我检查代码并以这种方式执行——我将检查并粘贴O/P
      • ghostdog74 -- 我收到错误:- Traceback(最近一次调用最后一次):文件“C:\Documents and Settings\rgolwalkar\Desktop\Desktop\Dr Py\Final_Py\backup_ver1.py”,第 16 行,在 tar = tarfile.open(targetBackup, "w:gz") NameError: name 'tarfile' is not defined
      • tarfile 是一个模块,像import tarfile一样导入
      • 好的,我做到了,它安静地运行——我没有提供任何消息,因为没有添加任何内容来产生消息——我检查了位置 destination=os.path.join(root,"Documents和设置","rgolwalkar","Desktop","Desktop","PyDevResourse") 但没有找到备份。第二个如果我添加:- 如果 os.system(tar) == 0: print 'Successful backup to', targetBackup else: print 'Backup FAILED' --如果 os.system(tar) == 1 会出现错误: TypeError: system() 参数 1 必须是字符串,而不是 TarFile
      • 你为什么使用os.systemtar 是一个 unix 命令。如果您执行os.system(tar),您实际上是在调用 tar 命令。 Python 脚本中的 tar 只是 tarfile module 的实例,而不是 tar 命令。放入一些打印语句并确保您的目录是正确的路径。在我的 *nix 上它对我来说很好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多