#!/usr/bin/perl use Date::Calc qw(:all); #--------------------------------------------------------- # 設定 #--------------------------------------------------------- #バックアップするフォルダを指定 my @list = qw(/backup /home /etc /var /usr/local/sbin); #フルバックアップを取る曜日 my $fullbackup = 'Tue'; #バックアップファイルの保存先 my $backup_location = '/mnt/backup'; #バックアップログの保存先 my $backuplog_location = '/mnt/backup/log'; #バックアップ期間 (week にて指定) $period = 2; #--------------------------------------------------------- # 設定終わり #--------------------------------------------------------- #--------------------------------------------------------- # スクリプト開始 #--------------------------------------------------------- #今日のタイムスタンプを取得(形式:2003-06-12) ($year, $month, $day, $hour, $min, $sec) = Today_and_Now(); my $today = "$year-$month-$day"; #削除するファイルの日付を取得(この日付より以前のファイルを削除) my ($d_year, $d_month, $d_day) = Add_Delta_Days($year, $month, $day, - ($period * 7)); $delete = "$d_year" . "$d_month" . "$d_day"; #保存期間を超えたバックアップファイルを削除 my @file_list = `/usr/bin/find $backup_location -type f`; foreach my $file_path (@file_list){ chomp $file_path; my $filename = (split(/\//, $file_path))[-1]; $date = (split(/\./, $filename))[1]; my ($cyear, $cmonth, $cdate) = split(/-/, $date); if(Date_to_Days($cyear, $cmonth, $cdate) < Date_to_Days($d_year, $d_month, $d_day)) { `/bin/rm -f $file_path`; } } #今日の曜日($wday)を取得 (形式: Tue) my $week_num = Day_of_Week($year,$month,$day); my $wday = Day_of_Week_Abbreviation($week_num); #昨日のタイムスタンプを取得(2003-06-11 12:15) my ($y_year, $y_month, $y_day) = Add_Delta_Days($year, $month, $day, -1); my $yesterday = "$y_year-$y_month-$y_day $hour:$min"; ### 今日の曜日($wday) と 設定($fullbackup) が同じ場合、フルバックアップを取る### if ($wday eq "$fullbackup") { `tar --totals -cvzf $backup_location/fullbackup.$today.tar.gz @list 1> $backuplog_location/fullbackup.$today.log 2> $backuplog_location/fullbackup.$today.error`; } ###今日の曜日($wday) と 設定($fullbackup) が等しくない場合、差分バックアップを取る### else { `tar --totals --newer '$yesterday' -cvzf $backup_location/incremental.$today.tar.gz @list 1> $backuplog_location/incremental.$today.log 2> $backuplog_location/incremental.$today.error`; }