This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Source Backups With Python
  Submitted by

Losing code has been known to reduce big strong coders to tears. I came up with this little Python script to do automatic backups. After every session of coding I double click this script which creates a uniquely named & encrypted zip file of my source tree and then uploads it to my ftp site. Think of it as a poor mans source control.

You'll need Python, which you can download for free from here and pkzipc.exe, a command line zip tool which you can get from here.

You'll also need some ftp space. If you don't already have some I can recommend They do webspace, but if you ask nicely the sysop will create a folder that isn't accessible from the web. They charge for storage and transfer, but if you only use the space for backups it will cost you next to nothing.

Next, edit the script. Do not fear, you wont have to learn Python. There are a bunch of variables at the top that contain all the details, replace what is in the strings with something sensible. If all goes well when you run it there will be a file such as 'work_(17-October-2002)_(09-52).zip' on your hard drive and a copy on your ftp server.

FileTypes= [ "*.h", "*.c", "*.cpp", "*.asm", "*.inc", "*.dsp", "*.rc",
".wdr" ]
BackupDir= "Path to the root folder you want backed up"
DestDir= "Path on your local machine where you want to store the backups"
FTPUpload= "url of your ftp site"
FTPUsername= "your ftp login"
FTPPassword= "your ftp password"
FTPDir= "folder on the ftp site to store backups"
Pkzip= "Location of pkzipc.exe "
ZipPassword= "Password to encrypt the zip file"

import time import os

ZipName= time.strftime( "work_(%d-%B-%Y)_(%H-%M).zip" ) ZipPath= DestDir + "/" + ZipName

os.chdir( BackupDir ) os.spawnv( os.P_WAIT, Pkzip, [ Pkzip ] + [ "-add", "-rec", "-path", "-pass="+ZipPassword ] + [ ZipPath ] + FileTypes )

from ftplib import FTP ftp= FTP( FTPUpload ) ftp.login( FTPUsername, FTPPassword ) ftp.cwd( FTPDir ) UploadFile= file( ZipPath, "rb" ) ftp.storbinary( "STOR " + ZipName, UploadFile ) ftp.quit() UploadFile.close()

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.