Back up data using commands
MySQLl provides a mysqldump command, which can be used for data backup.
Enter the password as prompted. In this case, all the table structures of the tm database and # mysqldump-u root-p tm> tm_050519. SQL data are backed up to tm_050519. SQL because the backup is always required, if the data volume occupies a large space, you can use gzip to compress the data. The command is as follows:
# Mysqldump-u root-p tm | gzip> tm_050519. SQL .gz
When the system crashes and the system is rebuilt, data can be restored as follows:
# Mysqldump-u root-p tm <tm_050519. SQL
Direct Recovery from compressed files:
# Gzip <tm_050519. SQL .gz | mysqldump-u root-p tm
Of course, many MySQL tools provide more intuitive backup and recovery functions, such as phpMyAdmin. However, I think mysqldump is the most basic and common.
Ii. Using crontab, the system regularly backs up mysql Databases every day
The system crontab is used to regularly execute Backup files and save the backup results by date to achieve the purpose of backup.
1. Create the path to save the backup file/var/backup/mysqlbak
# Mkdir-p/var/backup/mysqlbak
2. Create the/usr/sbin/bakmysql file.
# Vi/usr/sbin/bakmysql. sh
#! /Bin/bash
# Mysql backup script
Cd/var/backup/mysqlbak/
DateDIR = 'date + "% y-% m-% d "'
Mkdir-p $ dateDIR/data
For I in '/usr/local/www/mysql/bin/mysql-uroot-plin31415926-e "show databases" |
Grep-v "Database" | grep-v "information_schema "'
Do
/Usr/local/www/mysql/bin/mysqldump-uroot-plin31415926 $ I |
Gzip>/var/backup/mysqlbak/$ dateDIR/${i1__1_1_datedir1_.gz
Done
3. Modify file attributes to make them executable.
# Chmod + x/usr/sbin/bakmysql
4. Modify/etc/crontab
# Crontab-e
Add
01 3 *** root/usr/sbin/bakmysql
# Indicates that a backup is performed at three o'clock every day.
In this way, you can view the backup SQL file under/var/backup/mysqlbak every day!