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 moodlelynx -cmd_script=./backupScripts/turnOnMaintMode http://moodle.leeand00domain.locallogger END Turning on maintance mode complete.logger BEGIN Creating Backup Directoryexport bkdir=$(date +%Y-%m-%d)mkdir $bkdir cd $bkdirlogger END Creating Backup Directory#Get a backup copy of the databaselogger BEGIN Backing up the Moodle Databasemysqldump -u moodleuser --password=XXXXX -C -Q -e --create-options moodle > moodle-database.sqllogger END Backing up the Moodle Database#Get a backup copy of moodle data logger BEGIN Backing up moodledatatar -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/wwwlogger END Backing up moodledatacd ..tar -cvzf  $bkdir.tar.gz  $bkdir# Turn off Maintance mode and log it...logger BEGIN Turning off maintance mode in moodlelynx -cmd_script=./backupScripts/turnOffMaintMode http://moodle.leeand00domain.locallogger END Turning off maintance mode in moodle complete.
Automating backups run from a bash script in Linux the right way
bash;ssh;cron;backup
I would do 1 of the following things. Method #1 - system cronsAdd 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.weeklyYou can simply place a script you want to run at whatever frequency in the appropriate directory.Method #2 - password less sudoThe 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.shReferencesHow to run a specific program as root without a password prompt?