View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


LinkBack Search this Thread
Old 11-18-2009, 08:43 AM   #1
LQ Newbie
Registered: Apr 2006
Posts: 6

Rep: Reputation: 0
Question 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
Old 11-18-2009, 08:54 AM   #2
Senior Member
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
write the full path of the config.ini file
Old 11-18-2009, 01:32 PM   #3
LQ Newbie
Registered: Apr 2006
Posts: 6

Original Poster
Rep: Reputation: 0

Originally Posted by ghostdog74 View Post
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.

Last edited by beanbox; 11-18-2009 at 01:39 PM.
Old 11-18-2009, 06:23 PM   #4
Senior Member
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
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.


cron, python

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM
bash script runs different when cron executed jalder85 Linux - Server 4 02-20-2009 11:53 AM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
script doesn't run in cron (runs from shell just fine) Timur Sakayev Linux - Software 6 02-26-2007 12:56 PM

All times are GMT -5. The time now is 09:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration