backup script - moodledata/sessions zip warning: permission denied

Re: backup script - moodledata/sessions zip warning: permission denied

by Francisco Cortes -
Number of replies: 0
Hi Howard, Rick, Ken

thank you so much for getting back to me

I didn't want to use either root or the apache user to get my backup script working so with some trickery in the /etc/sudoers I manage to give my custom backup-user permission to run zip and cp commands as sudo for the data and html files of my moodle installation and then finally today, after weeks on tries and failures, I managed to get my script to execute as needed and now I'm able to backup the moodle install locally and remotely everyday and automatically.

Thank you again for your feedbackup and info on centos 6 and for the gar/gunzip suggestion which I will look into, I do plan to get centos 7 setup in gcloud (google compute vm instance) if we were to consider continuing with moodle which at this point is a conversation that the school is having given the cost and hassle that it takes to maintain the server ourselves, we're considering implementing google classroom instead which of course has it cons and pros.

It's definitely good, though, to know that we're not by ourselves out there so I do want to thank you all three for your input and for sharing your knowledge.

In case someone is interested here's the script I've been using and that I've been adapting

any suggestions and feedback is always welcome.

#!/bin/bash

# script Name: Local and Remote MoodleBackup
# By: Edward Owens and Adapted by Francisco Cortes
# Date: April 2011 - 2019
# Purpose: Backup Moodle, Moodledata, and Database and zip them in a single zip file. Keep only two backups on hand.
# If there is already two backups on hand, then overwrite the oldest one.
#==========================================================================================================================

#------Variables-----------------------------------------------------------------------------------------------------------
suffix=$(date +%B-%Y) # date stamp
logname="/home/moodlersyncuser/backups/"$suffix"_backup.log" # absolute path to logfile
file_name1="/home/moodlersyncuser/backups/moodle1gc.zip" # absolute path to backup 1
file_name2="/home/moodlersyncuser/backups/moodle2gc.zip" # absolute path to backup 2
zip1="/var/www/html/moodleic/" # absolute path to moodle root
zip2="/var/www/data/" # absolute path to moodle data
sql_name="/home/moodlersyncuser/backups/moodle_db.sql" # absolute path to sql file
dbName="[moodle's db]" # db Name
dbUser="[moodle's db user]" # db User
dbPassword="[moodle's db user's passwod]" # db Password
dbHost="localhost" # db Host
email="[email@fornotification.edu]" # email where notificaiton is sent
rsyncpath="/usr/bin/sudo /usr/bin/rsync" # rsync path on remote server
remotesudouser="moodlersyncuser" # user used to ssh into remote server
remoteserver="remote server's ip" # remote backupserver
remotersyncbackuplocation="/home/moodlersyncuser/backups/" # location on remote server to store rysnc bckps
#----------------------------------------------------------------------------------------------------------------------------

echo "Started--> "$(date +%H":"%M":"%S) > $logname
echo "Dumping database"
mysqldump --opt --user=$dbUser --password=$dbPassword --host=$dbHost $dbName > $sql_name

if test -e $file_name1;
then
if test -e $file_name2;
then
echo "copying moodle2gc.zip to moodle1gc.zip"
sudo cp $file_name2 $file_name1 >> $logname
echo "removing moodle2gc.zip"
rm -f $file_name2 >> $logname
echo "creating moodle2gc.zip"
sudo zip -r $file_name2 $zip1 $zip2 $sql_name >> $logname
else
echo "creating moodle2gc.zip"
sudo zip -r $file_name2 $zip1 $zip2 $sql_name >> $logname
fi
else
echo "creating moodle1gc.zip"
sudo zip -r $file_name1 $zip1 $zip2 $sql_name >> $logname
fi

# remove sql file
echo "removing "$sql_name
rm $sql_name

if test -e $file_name1;
then
# rsync moodle1gz to remote server
echo "rsyncing zip moodle1gc.zip to remote server"
rsync -avz -e "ssh" \
--rsync-path="$rsyncpath" \
--numeric-ids \
$file_name1 $remotesudouser@$remoteserver:$remotersyncbackuplocation >> $logname
fi
if test -e $file_name2;
then
# rsync moodle2gz to remote server
echo "rsyncing zip moodle2gc.zip to remote server"
rsync -avz -e "ssh" \
--rsync-path="$rsyncpath" \
--numeric-ids \
$file_name2 $remotesudouser@$remoteserver:$remotersyncbackuplocation >> $logname
fi
echo "Finished--> "$(date +%H":"%M":"%S) >> $logname
echo "Finished--> "$(date +%H":"%M":"%S)

# send a message to the email noted above.
echo "Sending email to "$email
echo "Moodle Backup and rsync Completed on gc" | mail -s "Moodle Backup and rsync Completed on gc" -a $logname $email
echo "done"
Average of ratings: Useful (1)