Previously, I did not pay much attention to my blog. I felt distressed when I lost my blog. Now I am paying attention to it and decided to regularly back up SQL statements. Write a script as follows:
Copy codeThe Code is as follows :#! /Usr/bin/perl
Use warnings;
Use strict;
Use MySQL: Backup;
Use Mail: Sender;
Open my $ tmp_ SQL, '>', "backup. SQL ";
My $ mb = new MySQL: Backup ('dbname', 'localhost', 'dbuser', 'dbpasswd', {'use _ REPLACE '=> 1, 'Show _ TABLE_NAMES '=> 1 });
Print $ tmp_ SQL $ mb-> create_structure ();
Print $ tmp_ SQL $ mb-> data_backup ();
Close $ tmp_ SQL;
My $ sender = new Mail: Sender {smtp => 'smtp .163.com ',
From => 'mailuser @ 163.com ',
# Debug => 'backup _ debug. log ',
Auth => 'login ',
Authid => 'mailuser ',
Authpwd => 'mailpasswd ',
};
$ Sender-> MailFile ({to => 'mailuser @ gmail.com ',
Subject => 'backup Blog SQL _ '. time (),
Msg => '3q ',
File => 'backup. SQL ',});
Instead of using mysqldump directly, I found this MySQL: Backup module and tried to read the exported SQL. The result is somewhat different from that of mysqldump. The general structure of the SQL exported by mysqldump is as follows:Copy codeThe Code is as follows: drop table if exists 'tablename ';
Create table 'tablename' (id int not null ...);
Lock tables 'tablename' WARITE;
Insert into 'tablename' VALUES (...),(...),(...);
Unlock tables;
The SQL structure exported by MySQL: Backup is as follows:Copy codeThe Code is as follows: create table 'tablename' (id int not null ...);
Replace into 'tablename' (ID,...) VALUES (1 ,...);
Replace into 'tablename' (ID,...) VALUES (2 ,...);
In fact, I do not know that replace is better than insert, but the example on the pod uses USE_REPLACE => '1' to copy it. If you are used to insert, when building an object in new, you don't need this param. In addition, when this Mail: Sender module was commented on Weibo, it was found that many of my friends were using it, so I gave up Net: SMTP_auth and tried it once, I feel pretty good ~~