Changeset 476

Show
Ignore:
Timestamp:
07/25/08 18:47:27 (1 month ago)
Author:
ogawa
Message:

Add POD and README.txt.
Beutify code by using perltidy.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • mt4-backup/trunk/backup

    r468 r476  
    11#!/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
    38# $Id$ 
    49 
     
    1015use base qw( MT::Tool ); 
    1116 
    12 our $VERSION = '0.2'; 
    13  
    14 sub usage { '[--dir <dir>] [--blog_ids <id1,id2,...>] [--size <num>] [--archive=zip|tgz] [--dryrun]' } 
     17our $VERSION = '0.3'; 
     18 
     19sub usage { 
     20'[--dir <dir>] [--blog_ids <id1,id2,...>] [--size <num>] [--archive=zip|tgz] [--dryrun]'; 
     21
    1522 
    1623sub help { 
     
    1825        backup $VERSION - A command-line backup tool for MTOS/MT version 4 
    1926 
    20         --dir <dir> 
     27        --dir=<dir> 
    2128                Specify the destination directory. By default, the backup 
    2229                files are created under the directory, specified by the 
    2330                value of TempDir in your mt-config.cgi. 
    24         --blog_ids <id1,id2,...> 
     31        --blog_ids=<id1,id2,...> 
    2532                Specify the blogs to include this backup. By default, 
    2633                all blogs are included in the backup. 
    27         --size <num>     
     34        --size=<num> 
    2835                Specify approximate file size (<num> kbytes) per backup file. 
    2936                If not specified, backup files are never divided. 
     
    3239                just generates a set of backup files and does not create  
    3340                a compressed archive file. 
     41        --dry-run 
    3442        --dryrun 
    3543                Dry run; perform a simulation of backup task that would 
    3644                occur but do not actually generate a backup. 
     45        --verbose 
     46                Show verbose messages. 
    3747    }; 
    3848} 
    3949 
    40 my ($temp_dir, $blog_ids, $size, $archive, $dryrun); 
     50my ( $temp_dir, $blog_ids, $size, $archive, $dryrun ); 
     51 
    4152sub options { 
    4253    return ( 
     
    4657        'archive=s'  => \$archive, 
    4758        'dryrun'     => \$dryrun, 
     59        'dry-run'    => \$dryrun, 
    4860    ); 
    4961} 
     
    6072        @blog_ids = grep { $_ =~ /^\d+$/ } ( split ',', $blog_ids ); 
    6173        die "There're no blogs to backup" unless scalar @blog_ids; 
    62     } else { 
     74    } 
     75    else { 
    6376        die "There're no blogs to backup" unless MT->model('blog')->exist; 
    6477    } 
     
    7386    } 
    7487 
    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 ); 
    7690    if ($verbose) { 
    77         if ($files && scalar @$files) { 
     91        if ( $files && scalar @$files ) { 
    7892            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 { 
    8297            print STDERR MT->translate("No backup created.\n"); 
    8398        } 
     
    90105# This function is derived from MT::CMS::Tools::backup() 
    91106sub backup { 
    92     my ($blog_ids, $temp_dir, $size, $archive, $verbose, $dryrun) = @_; 
     107    my ( $blog_ids, $temp_dir, $size, $archive, $verbose, $dryrun ) = @_; 
    93108 
    94109    my $enc = MT->config('PublishCharset') || 'utf-8'; 
     
    103118    require MT::BackupRestore; 
    104119    my $count_term = 
    105       $blog_ids && scalar( @$blog_ids
     120      $blog_ids && scalar(@$blog_ids
    106121      ? { class => '*', blog_id => $blog_ids } 
    107122      : { class => '*' }; 
     
    111126    my $finisher; 
    112127    my $progress = sub { 
    113         my ($str, $id) = @_; 
     128        my ( $str, $id ) = @_; 
    114129        print STDERR "$str\n" if $verbose; 
    115130    }; 
    116131 
    117132    if ($dryrun) { 
    118         $printer = sub { my ($data) = @_; return length($data); }; 
     133        $printer = sub { my ($data) = @_; return length($data); }; 
    119134        $splitter = sub { }; 
    120135        $finisher = sub { }; 
     
    131146            my ($asset_files) = @_; 
    132147            close $fh; 
    133             return [ "$file.xml" ] unless $archive; 
     148            return ["$file.xml"] unless $archive; 
    134149 
    135150            my $filepath = File::Spec->catfile( $temp_dir, "$file.$archive" ); 
    136151            require MT::Util::Archive; 
    137             my $arc = MT::Util::Archive->new($archive, $filepath); 
     152            my $arc = MT::Util::Archive->new( $archive, $filepath ); 
    138153            $arc->add_file( $temp_dir, "$file.xml" ); 
    139154            $arc->add_string( 
    140                     "<manifest xmlns='" 
     155                "<manifest xmlns='" 
    141156                  . MT::BackupRestore::NS_MOVABLETYPE() 
    142157                  . "'><file type='backup' name='$file.xml' /></manifest>", 
    143                   "$file.manifest"); 
     158                "$file.manifest" 
     159            ); 
    144160            $arc->close; 
     161 
    145162            # 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"]; 
    148165        }; 
    149166    } 
     
    162179            close $fh; 
    163180            my $filename = 
    164                 File::Spec->catfile( $temp_dir, $file . "-$findex.xml" ); 
     181              File::Spec->catfile( $temp_dir, $file . "-$findex.xml" ); 
    165182            $fh = gensym(); 
    166183            open $fh, ">$filename"; 
    167184            push @files, $file . "-$findex.xml"; 
    168             my $header .= 
    169               "<movabletype xmlns='" 
     185            my $header .= "<movabletype xmlns='" 
    170186              . MT::BackupRestore::NS_MOVABLETYPE() . "'>\n"; 
    171187            $header = "<?xml version='1.0' encoding='$enc'?>\n$header" 
     
    186202            for my $id ( keys %$asset_files ) { 
    187203                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 ); 
    190206                unless ( copy( $asset->[1], $tmp ) ) { 
    191207                    print STDERR MT->translate( 
    192208                        'Copying file [_1] to [_2] failed: [_3]', 
    193                         $asset->[1], $tmp, $! 
    194                     ) if $verbose; 
     209                        $asset->[1], $tmp, $! ) 
     210                    if $verbose; 
    195211                    next; 
    196212                } 
     
    209225            my $filepath = File::Spec->catfile( $temp_dir, "$file.$archive" ); 
    210226            require MT::Util::Archive; 
    211             my $arc = MT::Util::Archive->new($archive, $filepath); 
     227            my $arc = MT::Util::Archive->new( $archive, $filepath ); 
    212228            for my $f (@files) { 
    213229                $arc->add_file( $temp_dir, $f ); 
    214230            } 
    215231            $arc->close; 
     232 
    216233            # for safery, don't unlink before closing $arc here. 
    217234            for my $f (@files) { 
    218235                unlink File::Spec->catfile( $temp_dir, $f ); 
    219236            } 
    220             return [ "$file.$archive" ]; 
     237            return ["$file.$archive"]; 
    221238        }; 
    222239    } 
     
    235252    }; 
    236253 
    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    ); 
    240258} 
    241259 
     
    243261 
    2442621; 
     263__END__ 
     264 
     265=head1 NAME 
     266 
     267backup - A command-line backup tool for MTOS/MT version 4 
     268 
     269=head1 SYNOPSIS 
     270 
     271backup [--dir <dir>] [--blog_ids <id1,id2,...>] [--size <num>] [--archive=zip|tgz] [--dryrun] 
     272 
     273=head1 INSTALLATION 
     274 
     275Place this script inside your Movable Type "tools" directory. 
     276 
     277=head1 DESCRIPTION 
     278 
     279I<backup> CLI tool generates backup images, compatible with images 
     280generated through Web UI.  By invoking this command typically from 
     281C<cron> daemon, you can automatically and periodically generate 
     282snapshots of your blogs. 
     283 
     284=head1 OPTIONS 
     285 
     286=over 4 
     287 
     288=item --dir=C<directory> 
     289 
     290Specify the destination directory. By default, the backup files are 
     291created under the directory, specified by the value of C<TempDir> in 
     292your C<mt-config.cgi>. 
     293 
     294=item --blog_ids=C<id1,id2,...> 
     295 
     296Specify the blogs to include this backup. By default, all blogs are 
     297included in the backup. 
     298 
     299=item --size=C<num> 
     300 
     301Specify approximate file size (C<num> kbytes) per backup file.  If not 
     302specified, backup files are never divided. 
     303 
     304=item --archive=C<zip> 
     305 
     306=item --archive=C<tgz> 
     307 
     308Specify the archive format.  If not specified, this program just 
     309generates a set of backup files and does not create a compressed 
     310archive file. 
     311 
     312=item --dry-run 
     313 
     314=item --dryrun 
     315 
     316Dry run; perform a simulation of backup task that would occur but do 
     317not actually generate a backup. 
     318 
     319=item --verbose 
     320 
     321Show verbose messages. 
     322 
     323=back 
     324 
     325=head1 DEVELOPMENT 
     326 
     327The development version is always available from the following 
     328subversion repository: 
     329 
     330    http://code.as-is.net/svn/public/mt4-backup/trunk/ 
     331 
     332You can browse the files via Trac from the following: 
     333 
     334    http://code.as-is.net/public/browser/mt4-backup/trunk/ 
     335 
     336Any comments, suggestions, or patches are welcome. 
     337 
     338=head1 AUTHOR 
     339 
     340Hirotaka Ogawa E<lt>hirotaka.ogawa at gmail.comE<gt> 
     341 
     342This script is free software and licensed under the same terms as Perl 
     343(Artistic/GPL). 
     344 
     345=cut