用perl写的postgre数据库备份程序(摘录,没有注解)
2008-09-01 12:33
225 查看
#!/usr/bin/perl
#文件名:dbback.pl
mon,$day) = fdate(time());
my $deleteFilePattern = "customer_jfc".$year.$mon.$day;
print "$deleteFilePattern/n";
my @dbBackupFile = getPatternFile("customer_jfc".$year,"/usr/local/postgres/bin/dbbackup");
my $fileNO = 0;
my $fileNum = @dbBackupFile;
if(@dbBackupFile > 0){
my $lastDBbackupFile = $dbBackupFile[$#dbBackupFile];
if($lastDBbackupFile =~ /$deleteFilePattern/){
print $lastDBbackupFile."/n";
my @arrayLastDBbackupFileNO = split(/_/,$lastDBbackupFile);
my $lastDBbackupFileNO = $arrayLastDBbackupFileNO[2];
$fileNO = $lastDBbackupFileNO + 1;
}else{
$fileNO = 1;
}
}else{
$fileNO = 1;
}
my $fileName = "/usr/local/postgres/bin/dbbackup/customer_jfc".$year.$mon.$day."_".$fileNO;
my $cmd = "/usr/local/postgres/bin/pg_dump customer_jfc > ".$fileName;
my $rmcmd = "rm -f ".$fileName;
#system($rmcmd);
system($cmd);
#delPastFile($deleteFilePattern,"/usr/local/postgres/bin/dbbackup/",@dbBackupFile);
foreach(@dbBackupFile){
my $delPastFile = "/usr/local/postgres/bin/dbbackup/".$_;
rmOldFile($delPastFile);
}
sub fdate {
my @date = localtime($_[0]);
my $year = 1900+$date[5];
my $mon = sprintf("%02d",(1+$date[4]));
my $day = sprintf("%02d",($date[3]));
my $hour = sprintf("%02d",($date[2]));
my $minute = sprintf("%02d",($date[1]));
my $sec = sprintf("%02d",($date[0]));
return ($year, $mon, $day, $hour, $minute, $sec);
}
sub getPatternFile{
my ($pattern,$filePath)=@_;
my @files=();
opendir(TEMP,$filePath);
@files=grep(/^$pattern/,readdir TEMP);
closedir(TEMP);
return sort(@files);
}
sub delPastFile{
my($pattern,$filePath,@fileName)=@_;
my @arrayNewFile = ();
foreach(@fileName){
if($_ !~ /$pattern/){
my $file = $filePath.$_;
unlink $file;
}else{
push(@arrayNewFile,$_);
}
}
return @arrayNewFile;
}
sub rmOldFile{
my ($fileName)=@_;
if((-e $fileName) && (-s $fileName)){
my @arrayFileStats = stat($fileName);
my $fileTime=@arrayFileStats[9];
#24*3600*7 :代表7天是一个周期
$timeInterval=((int(time()/3600))*3600)-((int($fileTime/3600))*3600);
if($timeInterval>=24*3600*7){
unlink $fileName;
}
@arrayFileStats=();
}
}
#文件名:dbback.pl
mon,$day) = fdate(time());
my $deleteFilePattern = "customer_jfc".$year.$mon.$day;
print "$deleteFilePattern/n";
my @dbBackupFile = getPatternFile("customer_jfc".$year,"/usr/local/postgres/bin/dbbackup");
my $fileNO = 0;
my $fileNum = @dbBackupFile;
if(@dbBackupFile > 0){
my $lastDBbackupFile = $dbBackupFile[$#dbBackupFile];
if($lastDBbackupFile =~ /$deleteFilePattern/){
print $lastDBbackupFile."/n";
my @arrayLastDBbackupFileNO = split(/_/,$lastDBbackupFile);
my $lastDBbackupFileNO = $arrayLastDBbackupFileNO[2];
$fileNO = $lastDBbackupFileNO + 1;
}else{
$fileNO = 1;
}
}else{
$fileNO = 1;
}
my $fileName = "/usr/local/postgres/bin/dbbackup/customer_jfc".$year.$mon.$day."_".$fileNO;
my $cmd = "/usr/local/postgres/bin/pg_dump customer_jfc > ".$fileName;
my $rmcmd = "rm -f ".$fileName;
#system($rmcmd);
system($cmd);
#delPastFile($deleteFilePattern,"/usr/local/postgres/bin/dbbackup/",@dbBackupFile);
foreach(@dbBackupFile){
my $delPastFile = "/usr/local/postgres/bin/dbbackup/".$_;
rmOldFile($delPastFile);
}
sub fdate {
my @date = localtime($_[0]);
my $year = 1900+$date[5];
my $mon = sprintf("%02d",(1+$date[4]));
my $day = sprintf("%02d",($date[3]));
my $hour = sprintf("%02d",($date[2]));
my $minute = sprintf("%02d",($date[1]));
my $sec = sprintf("%02d",($date[0]));
return ($year, $mon, $day, $hour, $minute, $sec);
}
sub getPatternFile{
my ($pattern,$filePath)=@_;
my @files=();
opendir(TEMP,$filePath);
@files=grep(/^$pattern/,readdir TEMP);
closedir(TEMP);
return sort(@files);
}
sub delPastFile{
my($pattern,$filePath,@fileName)=@_;
my @arrayNewFile = ();
foreach(@fileName){
if($_ !~ /$pattern/){
my $file = $filePath.$_;
unlink $file;
}else{
push(@arrayNewFile,$_);
}
}
return @arrayNewFile;
}
sub rmOldFile{
my ($fileName)=@_;
if((-e $fileName) && (-s $fileName)){
my @arrayFileStats = stat($fileName);
my $fileTime=@arrayFileStats[9];
#24*3600*7 :代表7天是一个周期
$timeInterval=((int(time()/3600))*3600)-((int($fileTime/3600))*3600);
if($timeInterval>=24*3600*7){
unlink $fileName;
}
@arrayFileStats=();
}
}
相关文章推荐
- Hibernate 注解 没有加@Column一样会在数据库创建这些字段
- sql可定制的数据库备份和恢复程序
- 可定制的数据库备份和恢复程序
- django程序没有问题,保存远地测试数据库,save没问题,控制台也显示update成功,但是在数据库检索数据没有存进去
- VS 没有找到mspdb80.dll 错误;与 fatal error C1902: 程序数据库管理器不匹配;请检查安装
- oracle启动参数文件全部丢失 没有备份 简单三步正常启动数据库
- postgre数据库备份恢复shell
- 执行查询“BACKUP LOG [XXX] TO DISK = N'F:\\BackData\\事务日至备份\\...”失败,错误如下:“无法执行 BACKUP LOG,因为当前没有数据库备份。 BACKUP LOG 正在异常终止。
- 无法连接到数据库实例: ORA-12519: TNS: 没有找到适用的服务处理程序 (DBD ERROR: OCIServerAttach)。
- 可定制的数据库备份和恢复程序
- C#写winform程序,提示插入数据库成功却没有数据
- 关于PHP微信h5棋牌程序开发数据库的备份
- java程序中中文没有乱码,存入数据库后发现中文乱码(已解决)
- C#可定制的数据库备份和恢复程序
- C#可定制的数据库备份和恢复程序
- C#可定制的数据库备份和恢复程序
- QSqlDatabase 数据库操作没有removeDatabase 导致程序崩溃(此文极有可能是错误的,仅作个人留档参考)
- 数据库的自动备份与数据库被破坏后恢复(全文注解)
- 数据库不能打开,没有备份处理方法之一强行打开
- 事务日志 备份出错 属性ismanagementdatawarehouse在数据库不可用 或着没有权限