[置顶] 数据七天自动备份脚本

#!/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/\n";
       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"); } 

你可能感兴趣的:(数据,备份,脚本)