3

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."
leeand00
  • 4,615

2 Answers2

5

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

slm
  • 369,824
2

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)

Ramesh
  • 39,297