Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script I wrote to backup a Moodle installation. It works fine, I've tested the backups, but there's a problem with it; when I do the backups, since I have to use sudo to type the password every time, I have to physically type in the password instead of just running a cron job to do it automatically.

Now I suspect this has something to do with either something I don't know about cron or using an SSH key; either way I'd like to automate the backup.

#!/bin/bash

# Turn on Maintance mode and log it...
logger "BEGIN Turning on maintance mode in moodle"
lynx -cmd_script=./backupScripts/turnOnMaintMode http://moodle.leeand00domain.local
logger "END Turning on maintance mode complete."

logger "BEGIN Creating Backup Directory"
export bkdir=$(date +"%Y-%m-%d")
mkdir $bkdir 
cd $bkdir
logger "END Creating Backup Directory"

#Get a backup copy of the database
logger "BEGIN Backing up the Moodle Database"
mysqldump -u moodleuser --password=XXXXX -C -Q -e --create-options moodle > moodle-database.sql
logger "END Backing up the Moodle Database"

#Get a backup copy of moodle data 
logger "BEGIN Backing up moodledata"
tar -cvzf moodledata.tar.gz --exclude='/var/moodledata/cache' --exclude='/var/moodledata/lang' --exclude='/var/moodledata/sessions' --exclude='/var/moodledata/temp' /var/moodledata  && tar -cvzf moodleinstallation.tar.gz /var/www
logger "END Backing up moodledata"

cd ..

tar -cvzf  $bkdir.tar.gz  $bkdir

# Turn off Maintance mode and log it...
logger "BEGIN Turning off maintance mode in moodle"
lynx -cmd_script=./backupScripts/turnOffMaintMode http://moodle.leeand00domain.local
logger "END Turning off maintance mode in moodle complete."
share|improve this question

2 Answers 2

up vote 3 down vote accepted

I would do 1 of the following things.

Method #1 - system crons

Add the backup script to the system's crons rather then to a actual user's crontab entry. Most systems maintain a directory structure under /etc like this:

$ ls -1d /etc/cron.*
/etc/cron.d
/etc/cron.daily
/etc/cron.deny
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

You can simply place a script you want to run at whatever frequency in the appropriate directory.

Method #2 - password less sudo

The other approach would involve setting up a entry in your /etc/sudoers file using the command visudo to edit it. This entry would grant passwordless access to the user's crontab entry for this particular script. Your entry in their crontab would be something like this:

$ sudo ...your script...

And the entry in your /etc/sudoers file would be something like this:

user ALL=(root) NOPASSWD: /home/user/cronscript.sh

References

share|improve this answer

I would suggest getting into the crontab of root using,

sudo crontab -e

When you include it as the first command in your script, all the commands will be executed as root crontab.

If you need more information look into this page.

Commands that normally run with administrative privileges (i.e. they are generally run using sudo) should be added to the root user's crontab (instead of the user's crontab)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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