Site backup: Difference between revisions

From MoodleDocs
 
(16 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Site backup and restore}}
{{Backup}}
<p class="note">'''Please refer to [[Page_notes#Site backup & restore |these notes]] before editing this page.'''</p>
A '''site backup''' allows a site administrator to save everything associated with a moodle site. These backups can be restored to bring a site back to the point in time when the backup was made.  
A site backup allows a site administrator to save everything associated with a moodle site. These backups can be restored to bring a site back to the point in time when the backup was made.


Performing regular backups are highly recommended to reduce the amount of lost information in the event of a problem on the site and to speed the overall recovery process.
Performing regular backups are highly recommended to reduce the amount of lost information in the event of a problem on the site and to speed the overall recovery process.
== What needs to be backed up? ==
== What needs to be backed up? ==
A Moodle system comprises three parts:
A Moodle system comprises three parts:
# The data stored in the '''[[Development:Database schema introduction|database]]''' (For example, a MySQL database)
# The data stored in the database (For example, a MySQL database)
# The uploaded '''[[Files|files]]''' (For example, site and course files uploaded via Moodle located in moodledata)
# The uploaded files (For example, site and course files uploaded via Moodle located in the moodledata folder)
# The '''Moodle code''' (For example, everything in server/htdocs/moodle)
# The '''Moodle code''' (For example, everything in server/htdocs/moodle)
You can confirm where all these things are located in a Moodle installation by checking the '''[[Configuration file|config.php]]''' file.  
You can confirm where all these things are located in a Moodle installation by checking the '''[[Configuration file|config.php]]''' file.  
 
:# '''$CFG->dbname''' shows the database name (e.g., moodledb)
:# '''$CFG->dbname''' shows the database name
:# '''$CFG->prefix''' shows the database tables name prefix (e.g., mdl_course, where "mdl_" is the prefix)
:# '''$CFG->prefix''' shows the the database table name prefix
:# '''$CFG->dataroot''' shows where the ''uploaded files'' are stored (e.g., server/moodledata)
:# '''$CFG->dataroot''' controls where the ''uploaded files'' are stored; and
:# '''$CFG->wwwroot''' shows where the ''moodle code'' is stored (e.g., server/htdocs/moodle).
:# '''$CFG->wwwroot''' points to where the ''code'' is stored.


;Tip  
;Tip  
Generally speaking, the database ("dbname and prefix") and the uploaded files (dataroot) are the two most important to copy on a regular basis. These contain information that will change most often.  
Generally speaking, the database ("dbname and prefix") and the uploaded files (dataroot) are the two most important to copy on a regular basis. These contain information that will change most often.  


The Moodle code (wwwroot) is less important as a frequent backup, since it will only change when the the actual code is changed through upgrades, addins and code tweaks. You can always get a copy of the standard Moodle code from http://download.moodle.org so you only have to backup the parts you added or changed yourself.
The Moodle code (wwwroot) is less important as a frequent backup, since it will only change when the actual code is changed through upgrades, addins and code tweaks. You can always get a copy of the standard Moodle code from http://download.moodle.org so you only have to backup the parts you added or changed yourself. However, if your Moodle has plugins and any code modifications, you should consider backing up your moodle code as an added security measure.
 
== Creating a backup of your Moodle site ==
== Creating a backup of your Moodle site ==
=== Database ===
=== Database ===
The right way to back up your database depends on which database system you are using. The instructions below are one way to back up a MySQL database. Another option would be to use a tool like phpMyAdmin to manually make a backup. The documentation for your database will give more options.
The right way to back up your database depends on which database system you are using. The instructions below are one way to back up a MySQL database. Another option would be to use a tool like phpMyAdmin to manually make a backup. The documentation for your database will give more options.


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):
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. You need study this example script and make adjustments to this script for your instance of Moodle.  It works well to run such a script daily via a cron task.
 
  cd /my/backup/directory
  cd /my/backup/directory
  mv moodle-database.sql.gz moodle-database-old.sql.gz
  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
  mysqldump --default-character-set=utf8mb4 -h example.com -u myusername --password=mypassword -C -Q -e --create-options mydatabasename > moodle-database.sql
  gzip moodle-database.sql
  gzip moodle-database.sql
Also consider using an option to help ensure that the backup is consistent (e.g. avoid the data changing half-way through the backup), e.g. for MySQL/MariaDB and mysqldump try [https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_single-transaction --single-transaction].


If you wanted to write your own script for backing up a database automatically at regular intervals (e.g. Daily), for small databases this site may be a good starting point: [https://www.spacerex.co/how-to-automatically-backup-a-mysql-or-mariadb-server-with-mysqldump/ How to Automatically Backup a MySQL or MariaDB Server with MySQLDump]. You could also modify this to make a copy of the moodledata folder, and include it in the .zip file created.
The mysqldump file is typically an ASCII text file that you can explore with most text editors.  The last command in the above script, "gzip," converts the ASCII text file into a compresses file.
==== Character encoding ====
==== Character encoding ====
Make sure that a database backup uses the correct character encoding. In most databases, use [[UTF-8]].
Make sure that a database backup uses the correct character encoding. In most databases, use [[UTF-8]].


When dumping the entire Moodle database, check for possible character encoding issues. In some instances, backups created with [http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html mysqldump] or [http://www.phpmyadmin.net phpMyAdmin] may not properly encode all of the data. This will result in non-readable characters when the database is restored.  
When dumping the entire Moodle database, check for possible character encoding issues. In some instances, backups created with [http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html mysqldump] or [http://www.phpmyadmin.net phpMyAdmin] may not properly encode all of the data. This will result in non-readable characters when the database is restored.  
 
:''Tip'': One solution is to use MySQL Administrator 1.1 or another tool that will force a UTF-8 dump of the data.
:''Tip'': One solution is to use MySQL Administrator 1.1 or another tool that will force a UTF-8 dump of the data.
==== Tools for database backups ====
==== Tools for database backups ====
 
; mysql command-line (or similar for your particular database)
: Your database typically has a command-line tool to manage your database. In the example script, above, "mysqldump" is the command for backing up your database.  You typically need SSH access to your server to use this tool.  It is the most reliable tool for database backups.
; phpMyAdmin
; phpMyAdmin
: [http://www.phpmyadmin.net phpMyAdmin] is the tool of choice with most web hosting providers.
: [http://www.phpmyadmin.net phpMyAdmin] is the tool of choice with most web hosting providers, especially if you do not have ssh access to your server.


; MySQLDumper
; MySQLDumper
: [http://www.mysqldumper.de/en/ MySQLDumper] is a backup script for MySQL databases, written in PHP and Perl. MySQLDumper uses a proprietary technique to avoid execution interruption when running PHP scripts (the max. execution time is usually set to 30 seconds). MySQLDumper also cares for the encoding problems mentioned above. It also works with compressed files and allows setting up regular cron jobs for updating and updating to a remote FTP site.
: [https://sourceforge.net/projects/mysqldumper/ MySQLDumper] is a backup script for MySQL databases, written in PHP and Perl. MySQLDumper uses a proprietary technique to avoid execution interruption when running PHP scripts (the max. execution time is usually set to 30 seconds). MySQLDumper also cares for the encoding problems mentioned above. It also works with compressed files and allows setting up regular cron jobs for updating and updating to a remote FTP site.


; AutoMySQLBackup
: [https://github.com/sixhop/AutoMySQLBackup AutoMySQLBackup] is a backup script for MySQL databases, written in PHP. AutoMySQLBackup will create Daily, Weekly and Monthly backups of one or more of your MySQL databases from one or more of your MySQL servers. Features include email notification, compression and encryption, configurable backup rotation, incremental database backups, and more.
=== Uploaded files (moodledata) ===
=== Uploaded files (moodledata) ===
Through the Moodle interface, users can upload or create files and folders. These are located in a directory, often called "moodledata". Since they are just files and folders, there are many different ways to backup or copy moodledata.
Through the Moodle interface, users can upload or create files and folders. These are located in a directory, often called "moodledata". Since they are just files and folders, there are many different ways to backup or copy moodledata.  
 
* For example, using a file transfer program, copy the entire moodledata directory to a different area, drive or computer. Example of file transfer programs include: FTP, WinSP, wget, rsync.
* For example, using a file transfer program, copy the entire moodledata directory to a different area, drive or computer. Example of file transfer programs include: FTP, WinSP, wget, rsync.
 
* You might use a compression program  to create compact files (tar, zip. 7z, XZ, BZIP2, GZIP, and WIM are a few file formats) of the entire directory.  This can be done before or after file transfers.


* You can use a compression program to create compact files (tar, zip. 7z, XZ, BZIP2, GZIP, and WIM are a few file formats) of the entire directory. This creates one backup file that might be easier (and more reliable) to manage before a file transfer.
; Tips  
; Tips  
* Typically not all moodledata files change between regular/periodic backups. A new Administrator might want to look into [http://en.wikipedia.org/wiki/Incremental_backup incremental or other efficient backups] procedures.  
* Typically not all moodledata files change between regular/periodic backups. A new Administrator might want to look into [http://en.wikipedia.org/wiki/Incremental_backup incremental or other efficient backups] procedures.  
* Depending upon the operating environment there are many tools for backing up server files and ways of backing up moodledata. See [[Tools_for_backing_up_server_files]] for tips on using rsync, FTP or Wget.
* Depending upon the operating environment there are many tools for backing up server files and ways of backing up moodledata. See [[Tools for backing up server files]] for tips on using rsync, FTP or Wget.
 
=== Moodle code ===
=== Moodle code ===
Backing up the Moodle code, will be similar to backing up moodledata. See [[Tools for backing up server files]].
Backing up the Moodle code, will be similar to backing up moodledata. See [[Tools for backing up server files]].
;Tip
;Tip
It is always a good idea to have several backup copies of your Moodle code files. While you can always download a fresh base copy of the Moodle code from http://download.moodle.org, you might have customized that code. It is a good idea to create a separate backup of your Moodle code before you customize the code. This includes installing [[Contributed code]], [[Themes]] and upgrading.
It is always a good idea to have several backup copies of your Moodle code files. While you can always download a fresh base copy of the Moodle code from http://download.moodle.org, you might have customized that code. It is a good idea to create a separate backup of your Moodle code before you customize the code. This includes installing [[Contributed code]], [[Themes]] and upgrading.
 
 
 
== See also ==
== See also ==
 
* [https://www.youtube.com/watch?v=k5rwTy3sNh0 Screencast: How to back up a Moodle site] (thanks to Bruce Chambr)
*[[Backup and restore FAQ]]
*[[Site restore]] Now that you have a backup, how to restore it
*[[Site restore]] Now that you have a backup, how to restore it
*[[Automated course backup]]
*[http://www.moodletutorials.org/view_video.php?viewkey=e257e44aa9d5bade97ba Video showing how to backup a whole Moodle site (on Linux)]
* [[Moodle migration]] - move a Moodle site to a different server
* [[Moodle migration]] - move a Moodle site to a different server
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=140949 Site DatabaseBackup Using phpMyAdmin] forum discussion
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=140949 Site DatabaseBackup Using phpMyAdmin] forum discussion
* [[Site Backup for Low-tech Users]]
* [[Site Backup for Low-tech Users]]
* [[Course backup]] A course backup is done within the Moodle interface
[[Category:Backup]]
[[de:Sicherung der Moodle-Installation]]
[[de:Sicherung der Moodle-Installation]]
[[ja:サイトバックアップ]]
[[ja:サイトバックアップ]]
[[es:Copia de seguridad del sitio]]
[[fr:Sauvegarde de site]]

Latest revision as of 14:17, 15 November 2024

A site backup allows a site administrator to save everything associated with a moodle site. These backups can be restored to bring a site back to the point in time when the backup was made.

Performing regular backups are highly recommended to reduce the amount of lost information in the event of a problem on the site and to speed the overall recovery process.

What needs to be backed up?

A Moodle system comprises three parts:

  1. The data stored in the database (For example, a MySQL database)
  2. The uploaded files (For example, site and course files uploaded via Moodle located in the moodledata folder)
  3. The Moodle code (For example, everything in server/htdocs/moodle)

You can confirm where all these things are located in a Moodle installation by checking the config.php file.

  1. $CFG->dbname shows the database name (e.g., moodledb)
  2. $CFG->prefix shows the database tables name prefix (e.g., mdl_course, where "mdl_" is the prefix)
  3. $CFG->dataroot shows where the uploaded files are stored (e.g., server/moodledata)
  4. $CFG->wwwroot shows where the moodle code is stored (e.g., server/htdocs/moodle).
Tip

Generally speaking, the database ("dbname and prefix") and the uploaded files (dataroot) are the two most important to copy on a regular basis. These contain information that will change most often.

The Moodle code (wwwroot) is less important as a frequent backup, since it will only change when the actual code is changed through upgrades, addins and code tweaks. You can always get a copy of the standard Moodle code from http://download.moodle.org so you only have to backup the parts you added or changed yourself. However, if your Moodle has plugins and any code modifications, you should consider backing up your moodle code as an added security measure.

Creating a backup of your Moodle site

Database

The right way to back up your database depends on which database system you are using. The instructions below are one way to back up a MySQL database. Another option would be to use a tool like phpMyAdmin to manually make a backup. The documentation for your database will give more options.

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. You need study this example script and make adjustments to this script for your instance of Moodle. It works well to run such a script daily via a cron task.

cd /my/backup/directory
mv moodle-database.sql.gz moodle-database-old.sql.gz
mysqldump --default-character-set=utf8mb4 -h example.com -u myusername --password=mypassword -C -Q -e --create-options mydatabasename > moodle-database.sql
gzip moodle-database.sql

Also consider using an option to help ensure that the backup is consistent (e.g. avoid the data changing half-way through the backup), e.g. for MySQL/MariaDB and mysqldump try --single-transaction.

If you wanted to write your own script for backing up a database automatically at regular intervals (e.g. Daily), for small databases this site may be a good starting point: How to Automatically Backup a MySQL or MariaDB Server with MySQLDump. You could also modify this to make a copy of the moodledata folder, and include it in the .zip file created.

The mysqldump file is typically an ASCII text file that you can explore with most text editors. The last command in the above script, "gzip," converts the ASCII text file into a compresses file.

Character encoding

Make sure that a database backup uses the correct character encoding. In most databases, use UTF-8.

When dumping the entire Moodle database, check for possible character encoding issues. In some instances, backups created with mysqldump or phpMyAdmin may not properly encode all of the data. This will result in non-readable characters when the database is restored.

Tip: One solution is to use MySQL Administrator 1.1 or another tool that will force a UTF-8 dump of the data.

Tools for database backups

mysql command-line (or similar for your particular database)
Your database typically has a command-line tool to manage your database. In the example script, above, "mysqldump" is the command for backing up your database. You typically need SSH access to your server to use this tool. It is the most reliable tool for database backups.
phpMyAdmin
phpMyAdmin is the tool of choice with most web hosting providers, especially if you do not have ssh access to your server.
MySQLDumper
MySQLDumper is a backup script for MySQL databases, written in PHP and Perl. MySQLDumper uses a proprietary technique to avoid execution interruption when running PHP scripts (the max. execution time is usually set to 30 seconds). MySQLDumper also cares for the encoding problems mentioned above. It also works with compressed files and allows setting up regular cron jobs for updating and updating to a remote FTP site.
AutoMySQLBackup
AutoMySQLBackup is a backup script for MySQL databases, written in PHP. AutoMySQLBackup will create Daily, Weekly and Monthly backups of one or more of your MySQL databases from one or more of your MySQL servers. Features include email notification, compression and encryption, configurable backup rotation, incremental database backups, and more.

Uploaded files (moodledata)

Through the Moodle interface, users can upload or create files and folders. These are located in a directory, often called "moodledata". Since they are just files and folders, there are many different ways to backup or copy moodledata.

  • For example, using a file transfer program, copy the entire moodledata directory to a different area, drive or computer. Example of file transfer programs include: FTP, WinSP, wget, rsync.
  • You can use a compression program to create compact files (tar, zip. 7z, XZ, BZIP2, GZIP, and WIM are a few file formats) of the entire directory. This creates one backup file that might be easier (and more reliable) to manage before a file transfer.
Tips
  • Typically not all moodledata files change between regular/periodic backups. A new Administrator might want to look into incremental or other efficient backups procedures.
  • Depending upon the operating environment there are many tools for backing up server files and ways of backing up moodledata. See Tools for backing up server files for tips on using rsync, FTP or Wget.

Moodle code

Backing up the Moodle code, will be similar to backing up moodledata. See Tools for backing up server files.

Tip

It is always a good idea to have several backup copies of your Moodle code files. While you can always download a fresh base copy of the Moodle code from http://download.moodle.org, you might have customized that code. It is a good idea to create a separate backup of your Moodle code before you customize the code. This includes installing Contributed code, Themes and upgrading.

See also