Script code for blog Backup Using perl

Source: Internet
Author: User

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 ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.