LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-18-2009, 08:43 AM   #1
beanbox
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.

backuptest.py
PHP Code:
#!/usr/bin/python

"""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 (coffebean@sbcglobal.net)"
__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
#-----------------------------Variables----------------------------------------------
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):
    print 
'The backup directory dose not exist. Creating now.'
    
os.mkdir(backupdir)

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

#Logging
backuplog open(log'a')

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

#Make incrementsdir
backupincdir incrementsdir backupruntime
os
.mkdir(backupincdir)

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

#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)
else:
    
backuplog open(log'a')
    print >> 
backuplog, ('\n')
    print >> 
backuplog, ('Files sent to'' '+backupincdir+':')
    for 
topdirsfiles in os.walk(backupincdir):
        for 
nm in files:
            print >> 
backuplog, (os.path.join(topnm))
    print >> 
backuplog, ('\n')

#Logging
backuplog open(log'a')
print >> 
backuplog, (restformat.subtitle('END BACKUP'))
print >> 
backuplog, ('\n\n')
backuplog.close() 
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.

config.ini
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:
Code:
Traceback (most recent call last):
  File "/home/coffeeboy/bin/backup_devel/src/backuptest.py", line 28, in <module>
    backupmedia = config['media']
  File "/usr/lib/pymodules/python2.6/configobj.py", 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/backuptest.py or ~/bin/backup_devel/src/backuptest.py 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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Original Poster
Rep: Reputation: 0
Smile

Quote:
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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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


Reply

Tags
cron, python



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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:49 AM.

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