0

This question already has an answer here:

I created a script to back up my databases and my websites. It runs fine if I run it straight up but it's not running at all as a cron job.

I created a symbolic link to the script:

lrwxrwxrwx 1 root root    44 Feb 11 14:58 backupsql -> /home/techguyalabama/dropboxbackup/backupsql

If I run the backupsites from the /etc/cron.daily directory, it does prompt me for my password. I'm not sure if that is the issue. If it is, how do I avoid that?

I did make a change to the time that cron jobs run. Maybe that's a no-no also?

This is in my crontab:

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
05 5    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
30 5    * * 6   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
00 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

I did restart the server to see if that made a difference and it did not.

I also checked the syslog, from this post Where is the cron / crontab log?, to see if there was anything in there about the backupsql:

tail -f /var/log/syslog | grep backupsql

I got no results.

Anyone see what I am doing wrong?

EDIT#1

This is my backupsql script:

#!/bin/bash

DB_BACKUP="/home/techguyalabama/dropboxbackup/sqlbackup"
DB_USER="root"
DB_PASSWD="wouldntyouliketoknow"
HN=`hostname | awk -F. '{print $1}'`

# Create the backup directory
mkdir -p $DB_BACKUP

# Backup each database on the system
for db in $(mysql --user=$DB_USER --password=$DB_PASSWD -e 'show databases' -s --skip-column-names|grep -viE '(staging|performance_schema|information_schema)');
do mysqldump --user=$DB_USER --password=$DB_PASSWD --events --opt --single-transaction $db | gzip > "$DB_BACKUP/mysqldump-$HN-$db-$(date +%Y-%m-%d).gz";
done

/home/techguyalabama/dropboxbackup/dropbox_uploader.sh upload /home/techguyalabama/dropboxbackup/sqlbackup/*.* /sqlbackup/
rm -f /home/techguyalabama/dropboxbackup/sqlbackup/*.*

marked as duplicate by Zanna, user117103, Eric Carvalho, Anwar, user364819 Jun 8 '17 at 16:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Don't forget that these cron scripts are run as root, which means that for example the home directory refers to root's home directory, and so on.

Since your script asks for a password, you probably need to run it with root privileges so you can't just create the usual .anacron/cron.daily and put your script in there, you have to use /etc/cron.daily so it executes with the right permissions.

I think the easiest solution is to go through your script and check whether it uses user-specific variables like $HOME, in which case you have to replace these with the ones for user 'techguyalabama'.

  • ty for the response, however, I'm not referencing any variables as such. I have posted the script that I am trying to run. There is not a whole lot in it. I don't think it is calling it at all though. – ErocM Feb 13 '15 at 13:35
0

I just followed this answer from How to Setup a root cron Job Properly:

If you want to run a script as a normal user:

crontab -e

And add the line:

07,37 * * * * /usr/bin/tunlrupdate.sh

If you want to run your script as root:

sudo crontab -e

And add the same line:

07,37 * * * * /usr/bin/tunlrupdate.sh

As the user and it worked right away.

Not the answer you're looking for? Browse other questions tagged or ask your own question.