root/mysqldump2email/trunk/mysqldump2email

Revision 356, 5.2 kB (checked in by ogawa, 2 years ago)

Tracのpathを変更したのでURLを再修正。

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1#!/usr/bin/perl
2# mysqldump2email
3#
4# $Id$
5
6use strict;
7package mysqldump2email;
8use YAML;
9use MIME::Lite;
10use Encode;
11use DateTime;
12use File::Spec;
13use File::Basename;
14use Pod::Usage;
15use Getopt::Long;
16
17our $VERSION = '0.02';
18
19my %opt = (conf => 'config.yaml');
20GetOptions(\%opt, 'help', 'conf=s') or pod2usage(2);
21pod2usage(1) if $opt{help};
22
23my $cfg = YAML::LoadFile($opt{conf});
24
25my $dt = DateTime->now;
26$dt->set_time_zone($cfg->{time_zone} || 'local');
27
28my $zipfile = dump_mysql($cfg, $dt);
29my $attachfile = basename($zipfile);
30my $cfg_mail = $cfg->{mail}
31    or die 'No "mail" setting found in config.yaml';
32
33my $mailroute = $cfg_mail->{route} || { via => 'smtp', host => 'localhost' };
34MIME::Lite->send($mailroute->{via},
35                 $mailroute->{host} ? ($mailroute->{host}) : ());
36
37my $subject = __PACKAGE__;
38if ($cfg->{gspace} && $cfg->{gspace}->{enable}) {
39    $subject = join('|', (
40                          'GSPACE',
41                          $attachfile,
42                          $dt->epoch % 10000, # pseudo-random id ;)
43                          1,
44                          1,
45                          int((-s $zipfile) / 1024),
46                          'gs:' . ($cfg->{gspace}->{directory} || '/') . ' d$'
47                          ));
48}
49
50my $rfc822date = $dt->strftime('%a, %d %b %Y %H:%M:%S %z');
51my $msg = MIME::Lite->new(
52    Date => $rfc822date,
53    From => __PACKAGE__ . ' <' . $cfg_mail->{from} . '>',
54    To => $cfg_mail->{to},
55    Subject => encode('MIME-Header', $subject),
56    Type => 'multipart/mixed',
57);
58$msg->attach(
59    Type => 'TEXT',
60    Data => 'Dumped by ' . __PACKAGE__ . ', at ' . $rfc822date,
61);
62$msg->attach(
63    Type => 'application/zip',
64    Path => $zipfile,
65    Filename => $attachfile,
66    Disposition => 'attachment',
67);
68$msg->send();
69unlink($zipfile);
70
71sub dump_mysql {
72    my ($cfg, $dt) = @_;
73    my $cfg_mdump = $cfg->{mysqldump}
74        or die 'No "mysqldump" setting found in config.yaml';
75    my $cfg_zip = $cfg->{zip}
76        or die 'No "zip" setting found in config.yaml';
77
78    my $ts = $dt->strftime('%Y%m%d%H%M%S');
79    my $tmpdir = $cfg->{'tmpdir'} || '/tmp';
80    my $fname = __PACKAGE__ . '.' . $ts;
81    my $dumpfile = File::Spec->catfile($tmpdir, $fname);
82    my $zipfile = $dumpfile . '.zip';
83
84    system($cfg_mdump->{command} || 'mysqldump',
85           '--all-databases',
86           ($cfg_mdump->{username} ? ('--user=' . $cfg_mdump->{username}) : ()),
87           ($cfg_mdump->{password} ? ('--password=' . $cfg_mdump->{password}) : ()),
88           ($cfg_mdump->{host} ? ('--host=' . $cfg_mdump->{host}) : ()),
89           '--result-file=' . $dumpfile) == 0
90        or die 'Failed to execute "mysqldump" command';
91
92    system($cfg_zip->{command} || 'zip',
93           '-9mqj',
94           ($cfg_zip->{password} ? ('-P', $cfg_zip->{password}) : ()),
95           $zipfile, $dumpfile) == 0
96        or die 'Failed to execute "zip" command';
97
98    $zipfile;
99}
100
101=head1 NAME
102
103mysqldump2email - Dump MySQL data and send it to your mail server.
104
105=head1 SYNOPSIS
106
107  % mysqldump2email
108  % mysqldump2email --conf /path/to/config.yaml
109
110=head1 DESCRIPTION
111
112C<mysqldump2email> is a command line application that dumps MySQL
113databases and zip the dump file, and then sends it to your mail
114server.  It allows you to easily backup your MySQL databases into your
115mail spool, typically into your Google Mail spool.
116
117You can run this via crontab, for example, everyday.
118
119=head1 REQUIREMENT
120
121This application requires perl 5.8.0 with following Perl modules
122installed on your box.
123
124=over 4
125
126=item MIME::Lite
127
128=item DateTime
129
130=item YAML
131
132=back
133
134=head1 OPTIONS
135
136This application has a command-line option as follows:
137
138=over 4
139
140=item --conf /path/to/config.yaml
141
142Specifies the path to a configuration file. By default, C<config.yaml>
143in the current directory.
144
145=back
146
147=head1 CONFIGURATION
148
149The distributions includes a sample configuration file
150C<config.yaml.sample>. You can rename it to C<config.yaml> and
151configure C<mysqldump2email>.
152
153=over 4
154
155=item mysqldump:
156
157=over 4
158
159=item command
160
161Set the full path of C<mysqldump> command in your box.
162
163=item username, password
164
165Set your username and password for MySQL.
166
167=item host (Optional)
168
169Set your hostname of MySQL server.
170
171=back
172
173=item zip:
174
175=over 4
176
177=item command
178
179Set the full path of C<zip> command in your box.
180
181=item password (Optional)
182
183Set the password for encrypting/decrypting zip files.  If not set, no
184encryption is performed.
185
186=back
187
188=item mail:
189
190=over 4
191
192=item from
193
194Set email address for this application, which is used as C<From:> header.
195
196=item to
197
198Set the target email address, which is sent emails to.
199
200=item route
201
202Set how to send emails. Default is to use SMTP.
203
204=back
205
206=item gspace:
207
208=over 4
209
210=item enable
211
212Set 1 to enable gspace (http://www.rjonna.com/ext/gspace.php) support.
213
214=item directory
215
216Set the gspace directory, where attached files will be stored in.
217
218=back
219
220=item tmpdir:
221
222Set the temporary directory for this application.
223
224=item time_zone: (Optional)
225
226Sets the timezone for outgoing email C<Date:> header.  Default is set
227to use local timezone of your box.
228
229=back
230
231=head1 DEVELOPMENT
232
233The development version is always available from the following
234subversion repository:
235
236http://code.as-is.net/svn/public/mysqldump2email/trunk/
237
238You can browse the files via Trac from the following:
239
240http://code.as-is.net/public/browser/mysqldump2email/trunk/
241
242Any comments, suggestions, or patches are welcome.
243
244=head1 AUTHOR
245
246Hirotaka Ogawa E<lt>hirotaka.ogawa at gmail.comE<gt>
247
248This script is free software and licensed under the same terms as Perl
249(Artistic/GPL).
250
251=cut
Note: See TracBrowser for help on using the browser.