Perl incremental, full-scale backup script

Source: Internet
Author: User
Tags rsync

The script takes the JSON configuration file, can be customized, backup directory, full backup cycle, incremental backup time, etc.

JSON configuration file:

{  "Backupdir": "/data_backup",  "Archivedir": "/archive_dir",  "original": " /www/",  " Fulldaybase ": Ten}

Main program

#!/usr/bin/perl Usewarnings; UseFile::copy::recursive qw/dircopy rcopy/; UseFile::Rsync; UseArchive::zip QW (: error_codes:CONSTANTS); UseJSON; UseFile::Basename; UsePOSIX QW (strftime); UseData::dumper;my($day,$json );my $backupDate= Strftime ("%y%m%d",localtime( Time) );OpenJson"/sh/rsyncfile/backupconf.json"Or die "$!";$json=$_  while<JSON>;my $hashRef= Decode_json$json;my $originalDir=$hashRef-{original}; #Remember to add/not add/play the chickenmy $backupDir=$hashRef-{Backupdir};my $archiveDir=$hashRef-{archivedir};my $fullDayBase=$hashRef-{fulldaybase}; #Full backup cycle $day= $1 if $backupDate=~/(\d{2})$/;mkdir $backupDir  unless-D$backupDir;mkdir $archiveDir unless-D$archiveDir; Rsync_dir ($originalDir,$backupDir ); #Synchronizing Directories if($day%$fullDayBase==0 ) {             #Judgment period     for my $zipName(Glob "$backupDir/*" ) {        my $zipFromDir= BaseName ($zipName); $zipName= BaseName ($zipName) ."_".$backupDate.'. zip'; Print $zipName,"\ n"; Zip_dir ($backupDir,$archiveDir,$zipName,$zipFromDir ); }} #Compressed DirectorySubZip_dir {my($backupDir,$archiveDir,$zipName,$zipFromDir) =@_; my $zip= archive::zip->new (); my $dirMember=$zip->addtree ("$backupDir/$zipFromDir",$zipFromDir ); #Recursive compression directory    unless($zip->writetofilenamed ("$archiveDir/$zipName") ==0 ) {         die 'Write Error'; } } #Synchronizing DirectoriesSubRsync_dir {my($source,$target) =@_; Print "rsync file from $source to $target \ n"; #Archive Archive Mode # compress compression # verbose print details    my $obj= file::rsync->New ({archive=1, compress =1, Del =1, verbose =1 } ); $obj-exec({src =$source, dest =$target}) orwarn "rsync failed\n"; #Output Sync content    Print $obj-Out ;} #CopySubCopy {my($source,$target) =@_; Print $source,"\ n"; if(-D$source ) {        $File:: Copy::recursive::cprfcomp =1; Dircopy ($source,$target) or die "$!"; }    Else{rcopy ($source,$target) or die "$!"; }    return 1;}

Perl incremental, full-scale backup script

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.