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