ARCHIVED: Backing up your Legacy MySQL for Webserve database
mysql.iu.edu
and mysql-test.iu.edu
.
Following is an example of setting up an automatic backup script with a scheduled crontab. Be sure to replace all occurences of database_name
,
account
, port
, and
root_password
with your corresponding values. Optionally, you can replace the root user/password with any MySQL user you've set up that has proper privileges to access this database.
Backup script
The following code is a modified version of Julien Garino's
auto_mysql_dump.sh
, which can be found in the MySQL reference manual. The following example shows how to backup a database and place it in a backup folder on the account.
- Create the backup directory:
mkdir /ip/account/backups
- Create the shell script that performs the backup:
#!/bin/sh # Script name : auto_mysql_dump.sh # Backup database dir=`date +%Y-%m-%d` if [ -d /ip/account/backups ]; then mkdir /ip/account/backups/$dir /usr/local/mysql/bin/mysqldump -B --user=root --password='root_password' --host=localhost database_name > /ip/account/backups/$dir/database_name.sql fi # End of script auto_mysql_dump.sh
- Place this file in
/ip/account/auto_mysql_dump.sh
and make sure it is executable by typing:chmod +x auto_mysql_dump.sh
Crontab
The following crontab example runs the backup script weekly and logs the results. For a list of predefined scheduling definitions, see the Cron Wikipedia entry. For more about using crontabs, see ARCHIVED: Cron: Schedule jobs on the IU WWW servers.
@weekly /ip/account/auto_mysql_dump.sh >> /ip/account/backups/backup.log 2>&1 # run the backup script weekly
Restoring
The following example shows how to restore a database from a backup that was created in the backup section above. SSH into
mysql.iu.edu
, and move to the directory where your backup was saved. Then, type this command:
mysql --user=root --password='root_password' database_name < backup_filename.sql # be sure to replace root_password, database_name, and backup_filename
If the SQL backup file has been zipped, run this command:
gunzip database_name.sql.gz
Optionally, you can replace the root user/password with any MySQL user you've set up with proper privileges to access this database.
This is document bfod in the Knowledge Base.
Last modified on 2021-09-08 10:18:47.