Parse Mysql backup and restore simple summary with the use of the tee command

  • 2020-05-24 06:22:42
  • OfStack

Data backup method:
1: sql statement.
LOCKS TABLES tablename READ; / / read lock
Before trying to lock a table, LOCK TABLES is not transaction-safe and implicitly commits all active transactions, while at the same time, implicitly starts a transaction (for example, using START TRANSACTION),
So, the correct way to use LOCK TABLES for transaction tables such as InnoDB is to set AUTOCOMMIT=0
FLUSH TABLES,
SELECT * INTO OUTFILE 'data_bck.sql' FIELDS TERMINATED BY ',' FROM tablename;
UNLOCK TABLES;

2: mysqldump tools.
Fully backup 1 table in the database:
mysqldump -h192.168.4.20 -uusername -puserpswd dbname tablename > data_bck.sql

Fully backup a database:
mysqldump -h192.168.4.20 -uusername -puserpswd dbname tablename > data_bck.sql
Full backup of all databases:
mysqldump -h192.168.4.20 -uusername -puserpswd --single-transaction --all-databases > data_bck.sql
After full backup of all databases, deactivate all previous incremental logs and generate new logs:
mysqldump -h192.168.4.20 -uusername -puserpswd --single-transaction --flush-logs --master-data=2 --all-databases > data_bck.sql
After full backup of all databases, delete all previous incremental logs and generate new logs:
mysqldump -h192.168.4.20 -uusername -puserpswd --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > data_bck.sql
Incremental backup:
Perform a full backup and then perform an update log:
mysqladmin -h192.168.4.20 -uusername -puserpswd flush-logs

3: directly copy the data directory.
Stop the mysql service or lock the database table, copy the data directory to the backup location, start the mysql service or unlock the database table.

Data recovery method:
1: sql statement.
LOAD DATA INFILE 'data_bck.sql' INTO TABLE tablename FIELDS TERMINATED BY ',';

2: mysql tool.
Full recovery:
mysql -h192.168.4.20 -uusername -puserpswd dbname < data_bck.sql
Restore incremental log:
mysqlbinlog log-bin.000001 log-bin.000008 | mysql

Here's another fun and useful tip: tee.
It will you operation and output all the visual information saved into a file, can be used to generate tabular files, report making, simple backup.
The usage goes like this:
On the mysql command line, execute the following command:
> tee E:/heihei.sql
This creates a record file, and then all of our actions and all of our output will be recorded as-is, just like photo 1.
> select * from tablename;
Both the command and the query result will be recorded in this file, which is very interesting. However, it is very careful, even your error command will be recorded. Don't be laughed at. :)


Related articles: