Changeset 476
- Timestamp:
- 07/25/08 18:47:27 (1 month ago)
- Files:
-
- mt4-backup/trunk/README.txt (added)
- mt4-backup/trunk/backup (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
mt4-backup/trunk/backup
r468 r476 1 1 #!/usr/bin/perl -w 2 2 # 3 # This software is provided as-is. You may use it for commercial or 4 # personal use. If you distribute it, please keep this notice intact. 5 # 6 # Copyright (c) 2008 Hirotaka Ogawa 7 # 3 8 # $Id$ 4 9 … … 10 15 use base qw( MT::Tool ); 11 16 12 our $VERSION = '0.2'; 13 14 sub usage { '[--dir <dir>] [--blog_ids <id1,id2,...>] [--size <num>] [--archive=zip|tgz] [--dryrun]' } 17 our $VERSION = '0.3'; 18 19 sub usage { 20 '[--dir <dir>] [--blog_ids <id1,id2,...>] [--size <num>] [--archive=zip|tgz] [--dryrun]'; 21 } 15 22 16 23 sub help { … … 18 25 backup $VERSION - A command-line backup tool for MTOS/MT version 4 19 26 20 --dir <dir>27 --dir=<dir> 21 28 Specify the destination directory. By default, the backup 22 29 files are created under the directory, specified by the 23 30 value of TempDir in your mt-config.cgi. 24 --blog_ids <id1,id2,...>31 --blog_ids=<id1,id2,...> 25 32 Specify the blogs to include this backup. By default, 26 33 all blogs are included in the backup. 27 --size <num>34 --size=<num> 28 35 Specify approximate file size (<num> kbytes) per backup file. 29 36 If not specified, backup files are never divided. … … 32 39 just generates a set of backup files and does not create 33 40 a compressed archive file. 41 --dry-run 34 42 --dryrun 35 43 Dry run; perform a simulation of backup task that would 36 44 occur but do not actually generate a backup. 45 --verbose 46 Show verbose messages. 37 47 }; 38 48 } 39 49 40 my ($temp_dir, $blog_ids, $size, $archive, $dryrun); 50 my ( $temp_dir, $blog_ids, $size, $archive, $dryrun ); 51 41 52 sub options { 42 53 return ( … … 46 57 'archive=s' => \$archive, 47 58 'dryrun' => \$dryrun, 59 'dry-run' => \$dryrun, 48 60 ); 49 61 } … … 60 72 @blog_ids = grep { $_ =~ /^\d+$/ } ( split ',', $blog_ids ); 61 73 die "There're no blogs to backup" unless scalar @blog_ids; 62 } else { 74 } 75 else { 63 76 die "There're no blogs to backup" unless MT->model('blog')->exist; 64 77 } … … 73 86 } 74 87 75 my $files = backup(\@blog_ids, $temp_dir, $size || 0, $archive, $verbose, $dryrun); 88 my $files = 89 backup( \@blog_ids, $temp_dir, $size || 0, $archive, $verbose, $dryrun ); 76 90 if ($verbose) { 77 if ( $files && scalar @$files) {91 if ( $files && scalar @$files ) { 78 92 for my $f (@$files) { 79 print STDERR MT->translate("'[_1]' created.\n", $f); 80 } 81 } else { 93 print STDERR MT->translate( "'[_1]' created.\n", $f ); 94 } 95 } 96 else { 82 97 print STDERR MT->translate("No backup created.\n"); 83 98 } … … 90 105 # This function is derived from MT::CMS::Tools::backup() 91 106 sub backup { 92 my ( $blog_ids, $temp_dir, $size, $archive, $verbose, $dryrun) = @_;107 my ( $blog_ids, $temp_dir, $size, $archive, $verbose, $dryrun ) = @_; 93 108 94 109 my $enc = MT->config('PublishCharset') || 'utf-8'; … … 103 118 require MT::BackupRestore; 104 119 my $count_term = 105 $blog_ids && scalar( @$blog_ids)120 $blog_ids && scalar(@$blog_ids) 106 121 ? { class => '*', blog_id => $blog_ids } 107 122 : { class => '*' }; … … 111 126 my $finisher; 112 127 my $progress = sub { 113 my ( $str, $id) = @_;128 my ( $str, $id ) = @_; 114 129 print STDERR "$str\n" if $verbose; 115 130 }; 116 131 117 132 if ($dryrun) { 118 $printer = sub { my ($data) = @_; return length($data); };133 $printer = sub { my ($data) = @_; return length($data); }; 119 134 $splitter = sub { }; 120 135 $finisher = sub { }; … … 131 146 my ($asset_files) = @_; 132 147 close $fh; 133 return [ "$file.xml"] unless $archive;148 return ["$file.xml"] unless $archive; 134 149 135 150 my $filepath = File::Spec->catfile( $temp_dir, "$file.$archive" ); 136 151 require MT::Util::Archive; 137 my $arc = MT::Util::Archive->new( $archive, $filepath);152 my $arc = MT::Util::Archive->new( $archive, $filepath ); 138 153 $arc->add_file( $temp_dir, "$file.xml" ); 139 154 $arc->add_string( 140 "<manifest xmlns='"155 "<manifest xmlns='" 141 156 . MT::BackupRestore::NS_MOVABLETYPE() 142 157 . "'><file type='backup' name='$file.xml' /></manifest>", 143 "$file.manifest"); 158 "$file.manifest" 159 ); 144 160 $arc->close; 161 145 162 # for safery, don't unlink before closing $arc here. 146 unlink File::Spec->catfile( $filename);147 return [ "$file.$archive"];163 unlink File::Spec->catfile($filename); 164 return ["$file.$archive"]; 148 165 }; 149 166 } … … 162 179 close $fh; 163 180 my $filename = 164 File::Spec->catfile( $temp_dir, $file . "-$findex.xml" );181 File::Spec->catfile( $temp_dir, $file . "-$findex.xml" ); 165 182 $fh = gensym(); 166 183 open $fh, ">$filename"; 167 184 push @files, $file . "-$findex.xml"; 168 my $header .= 169 "<movabletype xmlns='" 185 my $header .= "<movabletype xmlns='" 170 186 . MT::BackupRestore::NS_MOVABLETYPE() . "'>\n"; 171 187 $header = "<?xml version='1.0' encoding='$enc'?>\n$header" … … 186 202 for my $id ( keys %$asset_files ) { 187 203 my $asset = $asset_files->{$id}; 188 my $name = $id . '-' . $asset->[2];189 my $tmp = File::Spec->catfile( $temp_dir, $name );204 my $name = $id . '-' . $asset->[2]; 205 my $tmp = File::Spec->catfile( $temp_dir, $name ); 190 206 unless ( copy( $asset->[1], $tmp ) ) { 191 207 print STDERR MT->translate( 192 208 'Copying file [_1] to [_2] failed: [_3]', 193 $asset->[1], $tmp, $! 194 )if $verbose;209 $asset->[1], $tmp, $! ) 210 if $verbose; 195 211 next; 196 212 } … … 209 225 my $filepath = File::Spec->catfile( $temp_dir, "$file.$archive" ); 210 226 require MT::Util::Archive; 211 my $arc = MT::Util::Archive->new( $archive, $filepath);227 my $arc = MT::Util::Archive->new( $archive, $filepath ); 212 228 for my $f (@files) { 213 229 $arc->add_file( $temp_dir, $f ); 214 230 } 215 231 $arc->close; 232 216 233 # for safery, don't unlink before closing $arc here. 217 234 for my $f (@files) { 218 235 unlink File::Spec->catfile( $temp_dir, $f ); 219 236 } 220 return [ "$file.$archive"];237 return ["$file.$archive"]; 221 238 }; 222 239 } … … 235 252 }; 236 253 237 MT::BackupRestore->backup( $blog_ids, $printer, $splitter, $finisher, 238 $progress, $size * 1024, 239 $enc, $metadata ); 254 MT::BackupRestore->backup( 255 $blog_ids, $printer, $splitter, $finisher, 256 $progress, $size * 1024, $enc, $metadata 257 ); 240 258 } 241 259 … … 243 261 244 262 1; 263 __END__ 264 265 =head1 NAME 266 267 backup - A command-line backup tool for MTOS/MT version 4 268 269 =head1 SYNOPSIS 270 271 backup [--dir <dir>] [--blog_ids <id1,id2,...>] [--size <num>] [--archive=zip|tgz] [--dryrun] 272 273 =head1 INSTALLATION 274 275 Place this script inside your Movable Type "tools" directory. 276 277 =head1 DESCRIPTION 278 279 I<backup> CLI tool generates backup images, compatible with images 280 generated through Web UI. By invoking this command typically from 281 C<cron> daemon, you can automatically and periodically generate 282 snapshots of your blogs. 283 284 =head1 OPTIONS 285 286 =over 4 287 288 =item --dir=C<directory> 289 290 Specify the destination directory. By default, the backup files are 291 created under the directory, specified by the value of C<TempDir> in 292 your C<mt-config.cgi>. 293 294 =item --blog_ids=C<id1,id2,...> 295 296 Specify the blogs to include this backup. By default, all blogs are 297 included in the backup. 298 299 =item --size=C<num> 300 301 Specify approximate file size (C<num> kbytes) per backup file. If not 302 specified, backup files are never divided. 303 304 =item --archive=C<zip> 305 306 =item --archive=C<tgz> 307 308 Specify the archive format. If not specified, this program just 309 generates a set of backup files and does not create a compressed 310 archive file. 311 312 =item --dry-run 313 314 =item --dryrun 315 316 Dry run; perform a simulation of backup task that would occur but do 317 not actually generate a backup. 318 319 =item --verbose 320 321 Show verbose messages. 322 323 =back 324 325 =head1 DEVELOPMENT 326 327 The development version is always available from the following 328 subversion repository: 329 330 http://code.as-is.net/svn/public/mt4-backup/trunk/ 331 332 You can browse the files via Trac from the following: 333 334 http://code.as-is.net/public/browser/mt4-backup/trunk/ 335 336 Any comments, suggestions, or patches are welcome. 337 338 =head1 AUTHOR 339 340 Hirotaka Ogawa E<lt>hirotaka.ogawa at gmail.comE<gt> 341 342 This script is free software and licensed under the same terms as Perl 343 (Artistic/GPL). 344 345 =cut
