Cron Backups for Moodle Db and Moodledata

Create directory /var/backups

mkdir /var/backups
crontab -e

Add Database backup to the cron file:

#Backup Database (Before each day starts, backup Moodle prod DB from MySQL/MariaDB)
4 4 * * * root mysqldump dbname -u user -ppsaaword | gzip > /var/backups/moodle-db-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.sql.gz

Add moodledata backup to the cron file:

4 10 * * * zip -r /var/backups/moodledata-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.zip  /var/www/moodledata

 

Save 30 lasts backups

# Keep 30 SQL database backups and moodledata backups (last 30 backups of moodle)
 2 2 * * * find /var/backups/* -mtime +10 -exec rm {} \;