-   Programming (
-   -   python rsync script runs from shell not from cron (

beanbox 11-18-2009 08:43 AM

python rsync script runs from shell not from cron
I am running Ubuntu 9.10 w/ system stats:

Mobo: M4178 Plus
BIOS: vendor: American Megatrends Inc.
version: 1101 (06/06/2009)
size: 64KiB
capacity: 960KiB
CPU: AMD Athlon II X2 240
MEM: 4Gigs (2 DDR2 2Gig sticks)

I have written a Python script to backup selected files and directories in my home folder.

I am posting my devel files for help.
PHP Code:


"""Python Rsync backup

This program will creat a set of directories on the chosen backup distenation media, and create
incremental backups at runtime.  Those files that have been either deleted or moved since the
last backup will be placed in an increments directory under a folder labeled with the backup run time
(Day Month Date mil-time year).  All backup logging will be stored in a log file "
rsync-backup.log" on
the backup media.

__author__ "Ryan Box ("
__version__ "$Revision: 0.6 $"
__date__ "$Date: 11/17/09 17:19 $"
__license__ "Python"

import os
import restformat
from configobj import ConfigObj
from time import asctime

##TO-DO remove test from all rsync-backuptest.log
config ConfigObj('config.ini')
backupruntime asctime()

backupmedia config['media']

sourcedir config['source']

backupdir backupmedia config['backupdir']

incrementsdir backupmedia config['incsdir']

exclude sourcedir config['exlist']

include = 
sourcedir config['inlist']

log backupmedia config['logfile']

backupincdir incrementsdir backupruntime
#-------------------First run directory creation--------------------------------------
if not os.path.exists(backupdir):
'The backup directory dose not exist. Creating now.'

not os.path.exists(incrementsdir):
'The increments directory dose not exist. Creating now.'

backuplog open(log'a')

print >> 
backuplog, (restformat.title(backupruntime))
print >> 
backuplog, ('Making increments directory: "'incrementsdir backupruntime +'"\n')

#Make incrementsdir
backupincdir incrementsdir backupruntime

print >> backuplog, (restformat.subtitle('BEGIN BACKUP'))

#Run rsync backup command with Linux logging 
#TO-DO remove n from -narEtb prior to release 
os.system('rsync -narEtb --delete --stats --log-file-format="%i %o %f %n" --files-from='+include+' --backup-dir='+'"'+backupincdir+'" '+sourcedir+' '+backupdir+'>>'+log)

backupincdir incrementsdir backupruntime
if os.listdir(backupincdir) == []:
backuplog open(log'a')
    print >> 
backuplog, ('\n')
    print >> 
backuplog, ('Nothing sent to'+' ''('+incrementsdir+backupruntime+')'' '+'new increments directory.')
    print >> 
backuplog, ('Deleting Empty Directory.')
    print >> 
backuplog, ('\n')
os.rmdir(incrementsdir backupruntime)
backuplog open(log'a')
    print >> 
backuplog, ('\n')
    print >> 
backuplog, ('Files sent to'' '+backupincdir+':')
topdirsfiles in os.walk(backupincdir):
nm in files:
            print >> 
backuplog, (os.path.join(topnm))
    print >> 
backuplog, ('\n')

backuplog open(log'a')
print >> 
backuplog, (restformat.subtitle('END BACKUP'))
print >> 
backuplog, ('\n\n')

This file does the brunt of the work and runs.

The Variables are loaded from the config.ini file. This is where the cron (gnome-scheduler) job pukes.

PHP Code:

media = /media/FreeAgent/                  #The distination drive for you Backup
backupdir backuptest/                  #The directory to backup into    
incsdir backup-testincrements/              #The directory to place files deleted or moved since last backup
source = /home/coffeeboy/                  #The source directory to be backedup
logfile rsync-backuptest.log                #Name of the log file
exlist bin/backup_devel/conf/backupexluded.txt     #The location of the excluded list
inlist bin/backup_devel/conf/backupincluded.txt    #The location of the included list 

The resformat file does some minor formating for the log file (not posting the code).

When I run this program from the shell it runs correctly, but when I run the "gnome-scheduler" job I get this error out put:

Traceback (most recent call last):
  File "/home/coffeeboy/bin/backup_devel/src/", line 28, in <module>
    backupmedia = config['media']
  File "/usr/lib/pymodules/python2.6/", line 580, in __getitem__
    val = dict.__getitem__(self, key)
KeyError: 'media'
Press ENTER to continue and close this window.

This occurs when I have python ~/bin/backup_devel/src/ or ~/bin/backup_devel/src/ in the 'Command' line of t the scheduled task.

Any help would be great. I have a running version of this code but this is the next rev. that I have worked up (having fun learning new things)

Thank you

ghostdog74 11-18-2009 08:54 AM

write the full path of the config.ini file

beanbox 11-18-2009 01:32 PM


Originally Posted by ghostdog74 (Post 3761335)
write the full path of the config.ini file

I have done this and it works but I was trying to keep all system dependent (i.e. home directory) stuff in the config file so that if others were to use this they would only have to change the config.ini file. Is there a way to add "~/bin/backup/src" or the like? I tried
PHP Code:

config ConfigObj(os.path.isdir('~/bin/backup/src')) 

but got the same old error message, shouldn't this pass the same directory path?

Thank you for the quick reply.

ghostdog74 11-18-2009 06:23 PM

if the user is running your script in his home directory when the config.ini is, then it should be alright. If he is running the script from somewhere, you have to
find a way to know his home directory, then os.chdir() to it. This is just one way. There might be other ways.

All times are GMT -5. The time now is 08:31 AM.