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 is added to Path and CLASSPATH under Environment variables as well - anyone else with a suggestion for backing up the directory is most welcome

2
  • ok i did that(imported the tarfile module) and it ran quietly -- i did not give any message as there was nothing added to produce a message - i checked the location destination=os.path.join(root,"Documents and Settings","rgolwalkar","Desktop","Desktop","PyDevResourse") But there was no backup found. second if i add:- if os.system(tar) == 0: print 'Successful backup to', targetBackup else: print 'Backup FAILED' --it comes up with an error if os.system(tar) == 1: TypeError: system() argument 1 must be string, not TarFile Commented Mar 13, 2010 at 14:27
  • Just curious, why are you using rar instead of something cross-platform such as zip, which is built into python? Commented Mar 13, 2010 at 14:51

3 Answers 3

2

Maybe instead of writing your own backup script you could use python tool called rdiff-backup, which can create incremental backups?

Sign up to request clarification or add additional context in comments.

Comments

0

The source directory contains spaces, but you don't have quotes around it in the command line. This might be a reason for the backup to fail.

To avoid problems like this, use the subprocess module instead of os.system:

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

Comments

0

if the compression algorithm can be something else and its just to backup a directory, why not do it with python's own tar and gzip instead? eg

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()

that way, you are not dependent on rar.exe on the system.

5 Comments

Thanks a lot ghostdog74-- let me check the code and execute it that way --- i will check and paste the O/P
ghostdog74 -- I get the error :- Traceback (most recent call last): File "C:\Documents and Settings\rgolwalkar\Desktop\Desktop\Dr Py\Final_Py\backup_ver1.py", line 16, in <module> tar = tarfile.open(targetBackup, "w:gz") NameError: name 'tarfile' is not defined
tarfile is a module, import it like import tarfile
ok i did that and it ran quietly -- i did not give any message as there was nothing added to produce a message - i checked the location destination=os.path.join(root,"Documents and Settings","rgolwalkar","Desktop","Desktop","PyDevResourse") But there was no backup found. second if i add:- if os.system(tar) == 0: print 'Successful backup to', targetBackup else: print 'Backup FAILED' --it comes up with an error if os.system(tar) == 1: TypeError: system() argument 1 must be string, not TarFile
why do you use os.system? tar is a unix command. if you do os.system(tar), you are actually calling the tar command. the tar in the Python script is just the instance of tarfile module, NOT the tar command. Put in some print statements and make sure your directories are correct path. It works fine for me on my *nix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.