PetesProjects:   Backing Up With Python

<<< Backing Up With Perl Backing Up Home PetesProjects Home

Files that are described here:
bu2_py (The Python incremental backup script)

Here is a Python script called bu2_py that does the same basic thing as the Perl script, with a few differences. My knowledge of Python is very limited, and it's possible that many of the limitations I point out below are no limitation at all, and that someone else could do a better job of translating the Perl code to Python.

The Python script generally follows the same flow as the Perl script, except where it doesn't (of course). For example, Perl allows you to declare a variable but not assign a value to it: it's undefined. But in Python there's no test for whether there's a value assigned to the variable. As a result Python's code that reads in command line arguments is even less elegant than Perl's.

Perl allows the use of system commands; you can pass a command with arguments to the system and have something executed outside the Perl code. As far as I know you can't do that with Python.

For example, in Perl I can say "bu2_mtime = `which bu2`" which gets me the full path to the bu2 script. So you can't do that in Python. But it turns out Python puts the entire command line into the array (excuse me, the "list") sys.argv. The first value will be the command itself, followed by the arguments you provided.

For example:

In Perl, "bu2 incbackup bin/incbackuplist inc" will provide you with the following:

$ARGV[0] = "incbackup"
$ARGV[1] = "bin/incbackuplist"
$ARGV[2] = "inc"

In Python, "bu2_py incbackup bin/incbackuplist inc" will get you this: (okay, it gets me this)

sys.argv[0] = "/home/Peter/bin/bu2_py"
sys.argv[1] = "incbackup"
sys.argv[2] = "bin/incbackuplist"
sys.argv[3] = "inc"

I prefer Python's way of handling it. In Perl, if the length of @ARGV is 0, you still have to determine whether there is at least one argument. (To be more correct, $#ARGV is actually the index of the last entry of the array, not the array's length.)

Concatenating strings: Perl uses a period ".", Python uses a "+" plus sign.

Python has what seems to me to be a convoluted way of removing the newline from a string. In Perl, it's a simple

chomp($my_string);

In Python, you have to have a list of strings from which to remove newlines:

filelist = [b.rstrip() for b in a]

In Python, any specific string of characters is applied to one and only one element, such as a variable, subroutine (excuse me again, function), list, tuple, array, etc. In Perl, any string of characters can be used for a variable, subroutine, string, array, etc., and there's only the programmer to blame if he gets confused.

In the Perl script, bu2_mtime is given as the date of last modification in days, because of the -M switch:

(-M $bu2_mtime)

(Maybe it's a mistake to call it "mtime" since that's really the number of seconds since the epoch.)

Python treats mtime the same way as Perl, but has no switch to report modification age in days, so you subtract the mtime of the file from the current time and divide by 86400 to get number of days. Of course, you can do exactly the same thing in Perl.

Python doesn't have an equivalent to Perl's diamond operator, so it's simplest in Python to just read the file into an array, and operate on that until you run out of lines. The Perl script was written the same way to remain as consistent as possible.

And finally, because there is no facility to invoke system commands, the last line of the Python script simply says to:

"Exectute at the system prompt: 'backmeup",infile,outfile,"inc' to complete the backup."

And finally, it should also be possible to create a bash script which executes the Python script, which then creates a bash script that contains the backup command and the required arguments, which the first bash script then executes after it sets the proper permissions, and (supposedly) everything will happen like it does in the Perl script.

But let me know before you try that; I'll want to move to another country first...

<<< Backing Up With Perl Backing Up Home PetesProjects Home