LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab for automated backup --> not running (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-for-automated-backup-not-running-4175453596/)

Wover 03-11-2013 04:51 AM

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 :)?

millgates 03-11-2013 05:00 AM

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

Wover 03-11-2013 05:37 AM

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."


millgates 03-11-2013 05:47 AM

Quote:

Originally Posted by Wover (Post 4909064)
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.

shivaa 03-11-2013 08:17 AM

You can simply make an crontab entry like:
Code:

~$ su - root
~$ crontab -e
0 3 * * * "cd /var/scripts; ./serverbackup.sh > /tmp/backup_logs"


Wover 03-11-2013 10:37 AM

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.

lleb 03-11-2013 12:05 PM

Quote:

Originally Posted by Wover (Post 4909211)
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


mandyapenguin 03-11-2013 12:14 PM

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


vineethsp 03-11-2013 12:52 PM

Quote:

Originally Posted by Wover (Post 4909046)
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

Wover 03-12-2013 06:02 AM

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."


millgates 03-12-2013 06:40 AM

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.

Wover 03-12-2013 07:24 AM

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...

lleb 03-12-2013 07:37 AM

the look at the cron logs, typically they will be located in /var/log/cron you will see what is there.

Wover 03-12-2013 07:45 AM

Hurray! It works :). In the last edits I left out username variable in the crontab lines. Thanks to all!

lleb 03-12-2013 07:48 AM

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.


All times are GMT -5. The time now is 03:29 AM.