[Tutor] writing sample program that zips files and saves in a backup directory

Dave Angel davea at ieee.org
Tue Oct 13 21:23:11 CEST 2009


David Eric wrote:
> doing a python tutorial and one of the assignments says to develop a program
> that backsup files to zip files into a backup directory
>
> im using Darwin 10.0.0 unix version:
>
> this is what i came up with thus far, i did copy the sample program given
> but made changes for my unix OS:
>
> #!/usr/bin/env python
> # Filename : backup_prog.py
>
>
> import os
> import time
>
> source = '/Users/davidteboul/bin/python'
>
> target_dir = '/Users/davidteboul/backups'
>
> target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.gz'
>
> zip_command = "gzip -qr {0} {1}".format(target, ' '.join(source))
>
> if os.system(zip_command) == 0:
>     print('Successful backup to', target)
> else: print('Backup FAILED')
>
>
>   
When launching external programs you're not familiar with, you really 
need to do a dry-run test, just to be sure.  It wouldn't have been hard 
to come up with something that trashes your system.  (Ask me, I issued a 
Unix command once that was deleting all files in a valuable tree, and 
the only thing that saved my bacon was that Unix file I/O was so slow.  
It only did about two files per second, so I was able to Ctrl-C after 
only losing about 5 files, all of which were quickly recoverable.  The 
next directory down would have been a disaster, however, and had it 
completed, we would have had to restore from the previous night's 
nightly backup, effectively wasting the day for most employees.)


I see two problems, but since I don't know gzip, there may very well be 
more.

The -r switch on nearly all Unix utilities means "recurse" which means 
to process all subdirectories of the starting location.

The ' '.join(source) takes your source string, and puts spaces as every 
second character.  The only thing I can think of you might have meant 
was to pass it a list of strings.  So if source were
  source = ['/Users/davidteboul/bin/python', '/Users/davidteboul/bin/perl']

then it'd make sense.

Anyway, I'd launch some innocuous script, that echoes its arguments, 
rather than trying it directly on gzip.   And of course, I'd look at the 
man page for gzip, to see just what its arguments are supposed to look like.

DaveA



More information about the Tutor mailing list