Site backups are recommended in order to have all data saved with the best confidence and the shortest recovery time.
Creating a backup of your Moodle site
There are two main things you need to make a copy of - the database and the uploaded files. The Moodle scripts themselves are less important, since you can always download a fresh copy if you have to.
There are many ways to do such backups. Here is an outline of a little script you can run on Unix to backup the database (it works well to have such a script run daily via a cron task):
cd /my/backup/directory mv moodle-database.sql.gz moodle-database-old.sql.gz mysqldump -h example.com -u myusername --password=mypassword -C -Q -e --create-options mydatabasename > moodle-database.sql gzip moodle-database.sql
For the files, you can use rsync regularly to copy only the changed files to another host:
rsync -auvtz --delete -e ssh email@example.com:/my/server/directory /my/backup/directory/
If you want to run the cronscript at the machine you are running moodle at you have to use following rsync syntax
rsync -auvtz --delete -e ssh /path/to/local/folder/ remoteuser@remoteserver:/path/to/remote/folder/
If you do not want the root mailbox be spammed by statusmails of the rsync use
rsync -autzq --delete -e ssh /path/to/local/folder/ remoteuser@remoteserver:/path/to/remote/folder/
When dumping the entire Moodle database, administrators should be careful to watch for possible character encoding issues. In some instances, backups created with mysqldump or phpmyadmin may not properly encode all of the data resulting in spurious A characters. One solution is to use mySQL Administrator 1.1 or another tool that will force a UTF-8 dump of the data.
Restoring a site backup
If you have followed the above instructions and created a backup of a Moodle site, you may need to know how to restore the site backup you created. Here is a set of basic steps that make up the restore process.
1. Rename the original moodle directory to something different (so you still have it) and copy the backed up moodle directory or a newly downloaded moodle directory in its place.
2. If you are running mysql, a backup of the database should be a .sql, .gz or .tar.gz file. If it is .tar.gz or .gz you need to extract it until it is an sql file.
tar -xzvf moodlesqlfile.tar.gz
3. If you are running mysql, import the sql file back into a newly created database on the mysql server. Be careful here, some backups try to import right back into the same working database that moodle is connected to. This causes database problems that damage a Moodle installation. The best thing to do is make a new database, restore the backed up database into it, and change the Moodle config.php file to connect to this new database (this way you still have the original database).
once you have created the new database:
mysql -p new_database < moodlesqlfile.sql