LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2013, 05:51 AM   #1
Wover
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Rep: Reputation: Disabled
Crontab for automated backup --> not running


Hello,


I've created a script that will automatically back-up logs, www files and mysql database to dropbox and when I run it from shell it works as a charm.

However, I also added it to /etc/crontab:

0 3 * * * root sh /var/scripts/serverbackup.sh

This should run every day at 3AM, but it doesn't. Service crond status says that it's running. When I check cron logs, I can only see log entries related to the cron.daily, cron.hourly,... files.

I'm running this on CentOS.

What's my noob mistake ?
 
Old 03-11-2013, 06:00 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
What des the script look like?
Just a guess, but the most common mistake here is neglecting to specify full paths to whatever commands you are using in the script. So, for example, you have to write

Code:
/usr/bin/mysqldump whatever > sql_backup
instead of just

Code:
mysqldump whatever > sql_backup
 
1 members found this post helpful.
Old 03-11-2013, 06:37 AM   #3
Wover
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Here's the script. Does this mean that I have to use the full path for all commands (sh, tar, mkdir,...)?

Code:
#!/bin/bash
#Generic Server Backup With tar

DIR="serverbackup"
DATE=`date +%w`
SERVER=`uname -n`

echo "Starting backup for $SERVER..."

mkdir -p /root/$DIR/$DATE

# System Files Backup

echo "Backing up $SERVER /var/log..."
tar -cvzPf /root/$DIR/$DATE/$DATE-$SERVER-logs.tar.gz /var/log
sh /var/scripts/dropbox_uploader.sh upload  /root/$DIR/$DATE/$DATE-$SERVER-logs.tar.gz

#echo "Backing up $SERVER /var/www..."
#tar -cvzPf /root/$DIR/$DATE/$DATE-$SERVER-www.tar.gz /var/www
#sh /var/scripts/dropbox_uploader.sh upload /root/$DIR/$DATE/$DATE-$SERVER-www.tar.gz

echo "Dumping $SERVER MySQL databases files..."
mysqldump -u backupdba -pdbapass --all-databases > /var/lib/mysql/alldatabases.sql

echo "Backing up $SERVER MySQL configuration files..."

tar -cvzPf /root/$DIR/$DATE/$DATE-$SERVER-mysql.tar.gz /var/lib/mysql
sh /var/scripts/dropbox_uploader.sh upload /root/$DIR/$DATE/$DATE-$SERVER-mysql.tar.gz

echo "Done."
 
Old 03-11-2013, 06:47 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by Wover View Post
Does this mean that I have to use the full path for all commands (sh, tar, mkdir,...)?
Yes. Cron jobs are run in a limited environment, the $PATH variable is not set as it would be in your (root's) normal profile.
Alternatively, you may try to set the PATH variable in the begining of the script.
 
1 members found this post helpful.
Old 03-11-2013, 09:17 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You can simply make an crontab entry like:
Code:
~$ su - root
~$ crontab -e
0 3 * * * "cd /var/scripts; ./serverbackup.sh > /tmp/backup_logs"
 
Old 03-11-2013, 11:37 AM   #6
Wover
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
If I understand correctly, you are saying that cron doesn't use the same shell as root would and therefore needs exact paths, because the command paths are for example unknown in that shell.

But this would mean that I don't only have to change those few commands in serverbackup.sh, but also in the underlying script dropbox_uploader.sh, and that is not quite the compact script that serverbackup.sh is.

So I understand there is the different solution suggested by shivaa and millgates (in the end of that post) where I manually set some path somewhere, but I don't quite understand that.

Since this is the noob forum, can I ask you to tell me exactly what to add where and what the meaning of it is? Especially shivaa's crontab alternative is Chinese to me.
 
Old 03-11-2013, 01:05 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by Wover View Post
If I understand correctly, you are saying that cron doesn't use the same shell as root would and therefore needs exact paths, because the command paths are for example unknown in that shell.

But this would mean that I don't only have to change those few commands in serverbackup.sh, but also in the underlying script dropbox_uploader.sh, and that is not quite the compact script that serverbackup.sh is.
Correct, you either need to always use full path or you can try placing the PATH= in the first line of your crontab
Quote:
So I understand there is the different solution suggested by shivaa and millgates (in the end of that post) where I manually set some path somewhere, but I don't quite understand that.

Since this is the noob forum, can I ask you to tell me exactly what to add where and what the meaning of it is? Especially shivaa's crontab alternative is Chinese to me.
to find the PATH or root, log into root and run the following command:
[code]# echo $PATH[/quote]

Then you can place PATH=foo at the beginning of your script(s) or as the first line in your crontab. The PATH of my Rasberry Pi looks like the following:

Code:
pi@raspbmc:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/opt/vc/bin:/home/pi/.xbmc-current/xbmc-bin/bin
 
1 members found this post helpful.
Old 03-11-2013, 01:14 PM   #8
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
See millgates post(#4),
So su to root and find out the root user's path variable using
Code:
env | grep PATH
or
echo $PATH
Then add that path to either in bash script as below(Replace the path variable with your root user's path)
Code:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
then rest of your commands here.....
or in cronttab itself before the cron entries as below
Code:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
01 03 * * * /var/scripts/serverbackup.sh
if nessessary run
Code:
chmod +x /var/scripts/serverbackup.sh
service crond reload

Last edited by mandyapenguin; 03-11-2013 at 01:17 PM.
 
1 members found this post helpful.
Old 03-11-2013, 01:52 PM   #9
vineethsp
LQ Newbie
 
Registered: Mar 2013
Location: India
Distribution: RedHat Enterprise Linux Server Edition 5.5
Posts: 13

Rep: Reputation: Disabled
Quote:
Originally Posted by Wover View Post
Hello,


I've created a script that will automatically back-up logs, www files and mysql database to dropbox and when I run it from shell it works as a charm.

However, I also added it to /etc/crontab:

0 3 * * * root sh /var/scripts/serverbackup.sh

This should run every day at 3AM, but it doesn't. Service crond status says that it's running. When I check cron logs, I can only see log entries related to the cron.daily, cron.hourly,... files.

I'm running this on CentOS.

What's my noob mistake ?


TRY THIS in /etc/crontab
0 3 * * * root /bin/sh /var/scripts/serverbackup.sh
 
1 members found this post helpful.
Old 03-12-2013, 07:02 AM   #10
Wover
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
I completely understand what all of you wrote and I can implement it without a problem, yet the script still doesn't run automatically.

I'll post the content of /etc/crontab and /var/scripts/serverbackup.sh:

Code:
SHELL=/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
00 03 * * * /bin/sh /var/scripts/serverbackup.sh
--> doesn't matter if I use /bin/sh or sh or nothing at the actual command.

Code:
#!/bin/bash
#Generic Server Backup With tar

DIR="serverbackup"
DATE=`date +%w`
SERVER=`uname -n`

echo "Starting backup for $SERVER..."

mkdir -p /root/$DIR/$DATE

# System Files Backup

echo "Backing up $SERVER /var/log..."
tar -cvzPf /root/$DIR/$DATE/$DATE-$SERVER-logs.tar.gz /var/log
sh /var/scripts/dropbox_uploader.sh upload  /root/$DIR/$DATE/$DATE-$SERVER-logs.tar.gz

#echo "Backing up $SERVER /var/www..."
#tar -cvzPf /root/$DIR/$DATE/$DATE-$SERVER-www.tar.gz /var/www
#sh /var/scripts/dropbox_uploader.sh upload /root/$DIR/$DATE/$DATE-$SERVER-www.tar.gz

echo "Dumping $SERVER MySQL databases files..."
mysqldump -u backupdba -pdbapass --all-databases > /var/lib/mysql/alldatabases.sql

echo "Backing up $SERVER MySQL configuration files..."

tar -cvzPf /root/$DIR/$DATE/$DATE-$SERVER-mysql.tar.gz /var/lib/mysql
sh /var/scripts/dropbox_uploader.sh upload /root/$DIR/$DATE/$DATE-$SERVER-mysql.tar.gz

echo "Done."
 
Old 03-12-2013, 07:40 AM   #11
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Try redirecting stderr to a file.

Code:
00 03 * * * /bin/sh /var/scripts/serverbackup.sh 2>>/tmp/serverbackup.err
That should at least give us some clue about what's going on. It will probably just confirm that the paths are set incorrectly, but you never know.
 
1 members found this post helpful.
Old 03-12-2013, 08:24 AM   #12
Wover
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
I have a feeling crontab is just not being executed. I tried your line, which didn't output anything to that file. Then I tried this line:

* * * * * env > /tmp/env.output

and then also this one:

* * * * * /bin/env > /tmp/env.output

Which also did not output to that file.

But service crond status says it's running...
 
Old 03-12-2013, 08:37 AM   #13
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
the look at the cron logs, typically they will be located in /var/log/cron you will see what is there.
 
1 members found this post helpful.
Old 03-12-2013, 08:45 AM   #14
Wover
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hurray! It works . In the last edits I left out username variable in the crontab lines. Thanks to all!
 
Old 03-12-2013, 08:48 AM   #15
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
post exactly what you did to fix this issue so others who might have simular issues in the future can refer to this thread.

Thank you and congratulations.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Automated dcraw and cjpeg script from crontab henkka Programming 3 01-05-2012 03:33 PM
automated backup GameSky Linux - Software 23 06-18-2007 02:57 AM
Automated HD backup General Linux - Hardware 3 05-11-2006 09:39 PM
Automated Backup via FTP KePSuX Linux - Newbie 3 02-11-2004 09:25 AM
automated FTP backup josephswagner Linux - Software 2 06-06-2003 05:42 PM


All times are GMT -5. The time now is 01:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration