| 1 | #!/usr/bin/perl |
|---|
| 2 | # mysqldump2email |
|---|
| 3 | # |
|---|
| 4 | # $Id$ |
|---|
| 5 | |
|---|
| 6 | use strict; |
|---|
| 7 | package mysqldump2email; |
|---|
| 8 | use YAML; |
|---|
| 9 | use MIME::Lite; |
|---|
| 10 | use Encode; |
|---|
| 11 | use DateTime; |
|---|
| 12 | use File::Spec; |
|---|
| 13 | use File::Basename; |
|---|
| 14 | use Pod::Usage; |
|---|
| 15 | use Getopt::Long; |
|---|
| 16 | |
|---|
| 17 | our $VERSION = '0.02'; |
|---|
| 18 | |
|---|
| 19 | my %opt = (conf => 'config.yaml'); |
|---|
| 20 | GetOptions(\%opt, 'help', 'conf=s') or pod2usage(2); |
|---|
| 21 | pod2usage(1) if $opt{help}; |
|---|
| 22 | |
|---|
| 23 | my $cfg = YAML::LoadFile($opt{conf}); |
|---|
| 24 | |
|---|
| 25 | my $dt = DateTime->now; |
|---|
| 26 | $dt->set_time_zone($cfg->{time_zone} || 'local'); |
|---|
| 27 | |
|---|
| 28 | my $zipfile = dump_mysql($cfg, $dt); |
|---|
| 29 | my $attachfile = basename($zipfile); |
|---|
| 30 | my $cfg_mail = $cfg->{mail} |
|---|
| 31 | or die 'No "mail" setting found in config.yaml'; |
|---|
| 32 | |
|---|
| 33 | my $mailroute = $cfg_mail->{route} || { via => 'smtp', host => 'localhost' }; |
|---|
| 34 | MIME::Lite->send($mailroute->{via}, |
|---|
| 35 | $mailroute->{host} ? ($mailroute->{host}) : ()); |
|---|
| 36 | |
|---|
| 37 | my $subject = __PACKAGE__; |
|---|
| 38 | if ($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 | |
|---|
| 50 | my $rfc822date = $dt->strftime('%a, %d %b %Y %H:%M:%S %z'); |
|---|
| 51 | my $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(); |
|---|
| 69 | unlink($zipfile); |
|---|
| 70 | |
|---|
| 71 | sub 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 | |
|---|
| 103 | mysqldump2email - 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 | |
|---|
| 112 | C<mysqldump2email> is a command line application that dumps MySQL |
|---|
| 113 | databases and zip the dump file, and then sends it to your mail |
|---|
| 114 | server. It allows you to easily backup your MySQL databases into your |
|---|
| 115 | mail spool, typically into your Google Mail spool. |
|---|
| 116 | |
|---|
| 117 | You can run this via crontab, for example, everyday. |
|---|
| 118 | |
|---|
| 119 | =head1 REQUIREMENT |
|---|
| 120 | |
|---|
| 121 | This application requires perl 5.8.0 with following Perl modules |
|---|
| 122 | installed 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 | |
|---|
| 136 | This application has a command-line option as follows: |
|---|
| 137 | |
|---|
| 138 | =over 4 |
|---|
| 139 | |
|---|
| 140 | =item --conf /path/to/config.yaml |
|---|
| 141 | |
|---|
| 142 | Specifies the path to a configuration file. By default, C<config.yaml> |
|---|
| 143 | in the current directory. |
|---|
| 144 | |
|---|
| 145 | =back |
|---|
| 146 | |
|---|
| 147 | =head1 CONFIGURATION |
|---|
| 148 | |
|---|
| 149 | The distributions includes a sample configuration file |
|---|
| 150 | C<config.yaml.sample>. You can rename it to C<config.yaml> and |
|---|
| 151 | configure C<mysqldump2email>. |
|---|
| 152 | |
|---|
| 153 | =over 4 |
|---|
| 154 | |
|---|
| 155 | =item mysqldump: |
|---|
| 156 | |
|---|
| 157 | =over 4 |
|---|
| 158 | |
|---|
| 159 | =item command |
|---|
| 160 | |
|---|
| 161 | Set the full path of C<mysqldump> command in your box. |
|---|
| 162 | |
|---|
| 163 | =item username, password |
|---|
| 164 | |
|---|
| 165 | Set your username and password for MySQL. |
|---|
| 166 | |
|---|
| 167 | =item host (Optional) |
|---|
| 168 | |
|---|
| 169 | Set your hostname of MySQL server. |
|---|
| 170 | |
|---|
| 171 | =back |
|---|
| 172 | |
|---|
| 173 | =item zip: |
|---|
| 174 | |
|---|
| 175 | =over 4 |
|---|
| 176 | |
|---|
| 177 | =item command |
|---|
| 178 | |
|---|
| 179 | Set the full path of C<zip> command in your box. |
|---|
| 180 | |
|---|
| 181 | =item password (Optional) |
|---|
| 182 | |
|---|
| 183 | Set the password for encrypting/decrypting zip files. If not set, no |
|---|
| 184 | encryption is performed. |
|---|
| 185 | |
|---|
| 186 | =back |
|---|
| 187 | |
|---|
| 188 | =item mail: |
|---|
| 189 | |
|---|
| 190 | =over 4 |
|---|
| 191 | |
|---|
| 192 | =item from |
|---|
| 193 | |
|---|
| 194 | Set email address for this application, which is used as C<From:> header. |
|---|
| 195 | |
|---|
| 196 | =item to |
|---|
| 197 | |
|---|
| 198 | Set the target email address, which is sent emails to. |
|---|
| 199 | |
|---|
| 200 | =item route |
|---|
| 201 | |
|---|
| 202 | Set 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 | |
|---|
| 212 | Set 1 to enable gspace (http://www.rjonna.com/ext/gspace.php) support. |
|---|
| 213 | |
|---|
| 214 | =item directory |
|---|
| 215 | |
|---|
| 216 | Set the gspace directory, where attached files will be stored in. |
|---|
| 217 | |
|---|
| 218 | =back |
|---|
| 219 | |
|---|
| 220 | =item tmpdir: |
|---|
| 221 | |
|---|
| 222 | Set the temporary directory for this application. |
|---|
| 223 | |
|---|
| 224 | =item time_zone: (Optional) |
|---|
| 225 | |
|---|
| 226 | Sets the timezone for outgoing email C<Date:> header. Default is set |
|---|
| 227 | to use local timezone of your box. |
|---|
| 228 | |
|---|
| 229 | =back |
|---|
| 230 | |
|---|
| 231 | =head1 DEVELOPMENT |
|---|
| 232 | |
|---|
| 233 | The development version is always available from the following |
|---|
| 234 | subversion repository: |
|---|
| 235 | |
|---|
| 236 | http://code.as-is.net/svn/public/mysqldump2email/trunk/ |
|---|
| 237 | |
|---|
| 238 | You can browse the files via Trac from the following: |
|---|
| 239 | |
|---|
| 240 | http://code.as-is.net/public/browser/mysqldump2email/trunk/ |
|---|
| 241 | |
|---|
| 242 | Any comments, suggestions, or patches are welcome. |
|---|
| 243 | |
|---|
| 244 | =head1 AUTHOR |
|---|
| 245 | |
|---|
| 246 | Hirotaka Ogawa E<lt>hirotaka.ogawa at gmail.comE<gt> |
|---|
| 247 | |
|---|
| 248 | This script is free software and licensed under the same terms as Perl |
|---|
| 249 | (Artistic/GPL). |
|---|
| 250 | |
|---|
| 251 | =cut |
|---|