Crontab issue with Kubuntu - all help appreciated!

The place to post if you need help or advice

Moderators: ChriThor, LXF moderators

Crontab issue with Kubuntu - all help appreciated!

Postby Karti » Sat Aug 11, 2007 8:34 am

Hi all,
I have set up a cron job to backup my Kontact emails etc. The bash script that I have written is basic (as I am just learning) but it works fine if Irun it normally and I am generally happy with it. But when it runs at a set time it fails. It copies the kmail.rc across fine, but the tar.gz appears to come across only in name, with very little included in the file.
Any suggestions are welcome.
Code: Select all
#!/bin/bash

# This will tar and gzip the folder structure
cd /home/jim/.kde/share/apps/kmail
tar cvzf mails.tar.gz mail

# It will also copy this and a separate file across to external drive
cp /home/jim/.kde/share/config/kmailrc /media/IMAGES/testBackupTwo
mv /home/jim/.kde/share/apps/kmail/mails.tar.gz /media/IMAGES/testBackupTwo

****************************************************
This is my crontab -e
0 9 * * *       /home/jim/programming/scripts/backupOne
# This file was written by KCron. Copyright (c) 1999, Gary Meyer
# Although KCron supports most crontab formats, use care when editing.
# Note: Lines beginning with "#\" indicates a disabled task.
****************************************************


Many thanks

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm

RE: Crontab issue with Kubuntu - all help appreciated!

Postby nordle » Sat Aug 11, 2007 11:59 am

Maybe a little change the the create command and how it references the location of the files, eg:

#!/bin/bash

# This will tar and gzip the folder structure
/bin/cat /dev/null > /tmp/backup.log
/bin/tar -cvzf /media/IMAGES/testBackupTwo/mails.tar.gz /home/jim/.kde/share/apps/kmail 1> /tmp/backup.log

This should work, although I haven't tested it. Hopefully it should provide some more info in /tmp/backup.log if its still failing to do it properly.

( the v switch is for verbose output, you can remove this once your happy its working).
I think, therefore I compile
nordle
LXF regular
 
Posts: 1501
Joined: Fri Apr 08, 2005 9:56 pm

RE: Crontab issue with Kubuntu - all help appreciated!

Postby nelz » Sun Aug 12, 2007 6:45 pm

cron does not inherit most of your user environment, so you cannot make any assumptions about $PATH or the current directory. Giving full paths is the best option.

If you want to see the differences, write a cron script that does "env >/dome/file" and compare the contents with the output from env when logged in normally.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 9041
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Karti » Sun Aug 12, 2007 9:13 pm

Sorry to be a pain, (and thanks for the advice), but I am a bit new to this scripting lark.

I think you want me to pipe the details to a log file. Could you please elaborate a little if you have the time. I had to redo my machine as the motherboard died and I had no replavement but the new code is as follows:
Code: Select all
#!/bin/bash

# This will tar and gzip the folder structure for Kmail and Korganiser
cd /home/jim/.kde/share/apps/kmail
tar cvzf mails.tar.gz mail
cd /home/jim/.kde/share/apps/
tar cvzf organiser.tar.gz korganizer
###

# It will also copy these and other important files across to external drive
mv /home/jim/.kde/share/apps/organiser.tar.gz /media/IMAGES/testBackupTwo
cp /home/jim/.kde/share/config/kmailrc /media/IMAGES/testBackupTwo
cp /home/jim/.kde/share/config/korganizerrc /media/IMAGES/testBackupTwo
mv /home/jim/.kde/share/apps/kmail/mails.tar.gz /media/IMAGES/testBackupTwo
###


Again works perfectly when I run the file. But fails on the tar.gz files on cron. If you could explain the pipe idea a bit more I would love to try it.

Once again many thanks

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm

Postby ollie » Mon Aug 13, 2007 12:28 am

Karti wrote:
Code: Select all
tar cvzf mails.tar.gz mail
cd /home/jim/.kde/share/apps/
tar cvzf organiser.tar.gz korganizer
###



What nelz was getting at was that the command "tar" needs to include the full path to the actual command:
Code: Select all
/bin/tar cvzf mails.tar.gz mail
cd /home/jim/.kde/share/apps/
/bin/tar cvzf organiser.tar.gz korganizer


This should fix the problem.
ollie
Moderator
 
Posts: 2749
Joined: Mon Jul 25, 2005 11:26 am
Location: Bathurst NSW Australia

Postby Karti » Mon Aug 13, 2007 4:12 pm

I have tried the full path but to no avail.

Here is the full code again:
Code: Select all
#!/bin/bash

# This will tar and gzip the folder structure for Kmail and Korganiser

/bin/tar cvzf /home/jim/.kde/share/apps/kmail/mails.tar.gz /home/jim/.kde/share/apps/kmail/mail
/bin/tar cvzf /home/jim/.kde/share/apps/organiser.tar.gz /home/jim/.kde/share/apps/korganizer
###

# It will also copy these and other important files across to external drive
mv /home/jim/.kde/share/apps/organiser.tar.gz /media/IMAGES/testBackupTwo
cp /home/jim/.kde/share/config/kmailrc /media/IMAGES/testBackupTwo
cp /home/jim/.kde/share/config/korganizerrc /media/IMAGES/testBackupTwo
mv /home/jim/.kde/share/apps/kmail/mails.tar.gz /media/IMAGES/testBackupTwo
###


I would like to pipe the results so I can view any errors but not quite sure of the code......

My main pain.....is that it works if I run it, but not if its cron'd

arghh...once again all help is greatly appreciated.

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm

Postby Karti » Mon Aug 13, 2007 5:13 pm

OK, I played about and added this as mentioned above:

Code: Select all
# This will tar and gzip the folder structure for Kmail and Korganiser
/bin/cat /dev/null > /tmp/backup.log
/bin/tar cvzf /home/jim/.kde/share/apps/kmail/mails.tar.gz /home/jim/.kde/share/apps/kmail/mail > /tmp/backup.log

For soem reason it works?

I assume that the /bin/cat just reads the dev/null which inturn is read to backup.log.

I am just confused that this works and mine did not. Any ideas?

MAny thanks again for the help

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm

Postby nordle » Mon Aug 13, 2007 10:23 pm

man pages will usually give a quick flavor of a command eg:
man cat
cat - concatenate files and print on the standard output

/dev/null is nothing, a nothing device that contains nothing and stores nothing. A black whole, where anything you send to it will be lost.

"Talk to the hand, coz the /dev/null aint listening"

I just made that up, terrible wasn't it.....mmmm idea for a geeky t-shirt there me thinks.

It's also handy for creating files. So by concatenating nothing to a file, you end up with a blank file. There are many ways of doing this:
echo "" > /tmp/backup.log
touch /tmp/backup.log

I have no idea why your last example did not work, I can get by using bash, but am not a knowledgeable user really.
I think, therefore I compile
nordle
LXF regular
 
Posts: 1501
Joined: Fri Apr 08, 2005 9:56 pm

Postby nelz » Mon Aug 13, 2007 10:50 pm

Karti wrote:
Code: Select all
tar cvzf /home/jim/.kde/share/apps/kmail/mails.tar.gz /home/jim/.kde/share/apps/kmail/mail > /tmp/backup.log


This is asking for trouble. You are backing up your mails into an archive within the mail directory. And each time you are overwriting that backup, but including it in the list of files to be backed up. Aside from the recursive problems this will cause, the backup isn't that useful because if you trash the mail directory, you lose the backup too.

Try this to take a separate backup for each cron event, and put them somewhere safe.

Code: Select all
tar czf /somewhere/safe/mail-$(date -I).tar.gz home/jim/.kde/share/apps/kmail/mail 2>&1


You shouldn't need to redirect standard output when running a script from cron, because a properly set up cron will mail you the output anyway. All you need to do is redirect stderr to stdout (2>&1).
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 9041
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Karti » Tue Aug 14, 2007 6:48 am

Many thanks for the answers.

Nelz, I create the tar.gz but lower down in the file I move it to a test backup area. When I get this to work properly I was going to do it so that it saves the file with a date code ie. 20070813mails.tar.gz, but I will need to investigate that further (and it looks like you gave some tips on).

I'm still at a loss that the back up works with the extra
Code: Select all
/bin/cat /dev/null > /tmp/backup.log

When it should work (I believe) with the initial code.
I'm not at home at the moment, but I have a VM image here that I will try it on.

Many thanks all for the help so far

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm

Postby nelz » Tue Aug 14, 2007 8:59 am

You should create it in the backup area, otherwise you are trying to create a backup containing your backup.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 9041
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Karti » Tue Aug 14, 2007 7:13 pm

I have changed the position of my backup, so that the tar.gz is saved to the media drive. However it still fails by creating a shell rather than the full file structure unless I put the
Code: Select all
/bin/cat /dev/null > /tmp/backup.log

I just don't see why.......its driving me mad. I understand that I could pipe the backup.log to /dev/null now that nordle explained it (PS - Get me one of those teeshirts!)
I'm used to Windows code and although it seems strange to me, I would think it should work.....sorry.

Any ideas?

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm

Postby nelz » Tue Aug 14, 2007 7:36 pm

What do you mean by "creating a shell"?
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 9041
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Karti » Tue Aug 14, 2007 9:10 pm

By shell, I mean that rather than create the full folder structure and the items it contains, it may only create a few folders with few items.

If I run the script it works perfectly, I have all my 25mb of email etc. When it crons the file size is about 200 bytes

Hope that helps

K
;)
Karti
 
Posts: 44
Joined: Tue Jul 18, 2006 1:33 pm


Return to Help!

Who is online

Users browsing this forum: No registered users and 6 guests

cron