[セットトップ]データ7日間の自動バックアップスクリプト


#!/usr/bin/perl
#Author:laomeng
#Date:2016-04-11

=pod  :    :   =cut

use strict;
use warnings;
use POSIX qw(strftime);
my $now_date = strftime("%Y-%m-%d", localtime(time));
my $seven_agos = strftime("%Y-%m-%d", localtime(time - (7 * 24 * 60 * 60)));

# 
my $bak_dir="/opt/back_up";
mkdir $bak_dir,0755 if(!-e "$bak_dir");
mkdir "$bak_dir/$now_date";
mkdir $bak_dir,0755;
=pod   .............. =cut

# 
my $create_date=&get_file_create_date("$bak_dir/$now_date");
opendir(DIR,"$bak_dir/");
while(my $file = readdir DIR)
{
   next if $file =~ /\.|\.\./;
   my $create_date=&get_file_create_date("$bak_dir/$file");
   if("$create_date" eq "$seven_agos")
   {
       print "$bak_dir/$file/
"
; unlink glob "$bak_dir/$file/*"; rmdir "$bak_dir/$file"; } } closedir(DIR); # sub get_file_create_date { my $file = shift; my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat($file); return time_transfer($ctime); } # sub time_transfer { my $time_str = shift; my ($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday) = (localtime($time_str)); $sec = ($sec < 10)? "0$sec":$sec; $min = ($min < 10)? "0$min":$min; $hour = ($hour < 10)? "0$hour":$hour; $day = ($day < 10)? "0$day":$day; $mon = ($mon < 9)? "0".($mon+1):($mon+1); $year += 1900; return("$year-$mon-$day"); }