Always do not pay attention to their own blog, the last lost just love dearly, now pay attention to it, decided to regularly back up the SQL. Write a small script as follows:
Copy Code code 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, we looked for the Mysql::backup module and tried to see the exported SQL, and Mysqldump's results were somewhat different. The SQL generic structure that mysqldump exports is this way:
Copy Code code 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 this:
Copy Code code as follows:
CREATE TABLE ' tablename ' (ID INT not NULL ...);
REPLACE into ' tablename ' (ID,...) VALUES (1,...);
REPLACE into ' tablename ' (ID,...) VALUES (2,...);
In fact, I'm not quite sure that REPLACE the insert is good, but the pod on the example used use_replace=> ' 1 ', copied, if the custom insert, in the new build object, do not use this param on the line. In addition this Mail::sender module, is in micro-blog on a comment, found a lot of friends in use, I will give up a net::smtp_auth, with a try, feel good ~ ~