一个php写的linux下lvm自动快照实现脚本

简介: ==========crontab配置=============== #vi /etc/crontab SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# run-parts01 * * * * root run-parts /etc/cron.

 ==========crontab配置===============

#vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
*/1 * * * * root /usr/bin/php /opt/openfiler/var/etc/snp_script.php >> /root/rec.ini

蓝色字体标注标识要添加的部分。

在每一行的开头有五个“*”号,分别表示分、时、日、月、星期。“*/1”表示每分钟执行一次。

所有路径必须为绝对路径。

 

                      ===============php部分===================

#!/usr/bin/php
<?php
    date_default_timezone_set('PRC');
    $snp_date_rec=date("Y-m-d-H-i");
    $now=explode("-",$snp_date_rec);
    
    $snapshots_path="/opt/openfiler/var/www/htdocs/ipshot/etc/snapshots.xml";
    $schedules_path="/opt/openfiler/var/www/htdocs/ipshot/etc/schedules.xml";
    
    //print_r($now);
    //=======DOM读取snapshot=========
    $snp_xml=new DOMDocument('1.0');
    $snp_xml->load($snapshots_path);
    $snp_XMLDoc=$snp_xml->getElementsByTagName("snapshots")->item(0);
    
    
    $snp_date=date("Y-m-d H:i:s");
    
    $xml=new DOMDocument('1.0');
    $xml->load($schedules_path);
    $XMLDoc=$xml->getElementsByTagName("schedules")->item(0);
    
    
    $isLast=false;
    
    //========DOM读取XML========
    $schedules=array();
    $cou=0;
    foreach($XMLDoc->getElementsByTagName("schedule") as $schedule){
        $sch=array();
        $sch["name"]=$schedule->getAttribute("name");
        $sch["interval"]=$schedule->getAttribute("interval");
        $sch["count"]=$schedule->getAttribute("count");
        $sch["size"]=$schedule->getAttribute("size");
        $cou1=0;
        $sch_volumes=array();
        foreach($schedule->getElementsByTagName("volume") as $sch_volume){
            $last=$sch_volume->getAttribute("last");
            if(empty($last)||$last==""){
                global $isLast;
                $last=$sch_volume->getAttribute("time");
                $attr=$xml->createAttribute("last");
                $attr->appendChild($xml->createTextNode($last));
                $sch_volume->appendChild($attr);
                $isLast=true;
                $xml->save($schedules_path);    
            }
            $sch_volumes[$cou1++]=array($sch_volume->nodeValue,$last,$sch_volume->getAttribute("vg"));
        }
        $sch["volumes"]=$sch_volumes;
        $schedules[$cou++]=$sch;
    }
    foreach($schedules as $schedule){
        $interval=explode(" ",$schedule["interval"]);
        //print_r($interval);
        switch($interval[1]){
            case "min":
                $in=4;
                break;
            case "hour":
                $in=3;
                break;
            case "day":
                $in=2;
                break;
            case "month";
                $in=1;
                break;
            case "year":
                $in=0;
                break;
            case "week";
                $in=-1;
                break;
        }
        
        print("Last:".($isLast?"yes":"no")."\n");
        
        foreach($schedule["volumes"] as $volume){
                $start_time=explode("-",$volume[1]);
    
            if($in==-1){
                if($isLast||($now[$in]-$start_time[$in]!=0&&($now[2]-$start_time[2])%(7*$interval[0])==0)){
                    shell_exec("/sbin/lvcreate -s -L ".$schedule["size"]." -n of.snapshot.".$volume[0].".autosn".$snp_date_rec." ".$volume[2]);
                }
            }
            else if($isLast||($now[$in]-$start_time[$in]!=0&&($now[$in]-$start_time[$in])%$interval[0]==0)){
            //print("\n".$interval[0].":\n");
            $command="/sbin/lvcreate -s -L ".$schedule["size"]." -n of.snapshot.".$volume[0].".autosn".$snp_date_rec." /dev/".$volume[2]."/".$volume[0];
            print $command;
                $xpath=new DOMXPath($xml);
                $query="/schedules/user/schedule[@name='".$schedule["name"]."']/volume[@vg='".$volume[2]."' and text()='".$volume[0]."']";
                $node=$xpath->query($query)->item(0);
                $node->setAttribute("last",$snp_date_rec);
                $xml->save($schedules_path);
                
                shell_exec($command);
                $snp=$snp_xml->createElement("snapshot");
                    //print $snp_xml->saveXML();
                $attr=$snp_xml->createAttribute("id");
                $attr->appendChild($snp_xml->createTextNode("autosn".$snp_date_rec));
                $snp->appendChild($attr);
                $attr=$snp_xml->createAttribute("lvname");
                $attr->appendChild($snp_xml->createTextNode($volume[0]));
                $snp->appendChild($attr);
                $attr=$snp_xml->createAttribute("vgname");
                $attr->appendChild($snp_xml->createTextNode($volume[2]));
                $snp->appendChild($attr);
                $attr=$snp_xml->createAttribute("share");
                $attr->appendChild($snp_xml->createTextNode("no"));
                $snp->appendChild($attr);
                $attr=$snp_xml->createAttribute("rotateid");
                $attr->appendChild($snp_xml->createTextNode("100"));
                $snp->appendChild($attr);
                $attr=$snp_xml->createAttribute("timestamp");
                $attr->appendChild($snp_xml->createTextNode($snp_date));
                $snp->appendChild($attr);
                $attr=$snp_xml->createAttribute("size");
                $attr->appendChild($snp_xml->createTextNode($schedule["size"]));
                $snp->appendChild($attr);
                
                
                $snp_XMLDoc->appendChild($snp);
                print $snp_xml->saveXML();    
            }
            //删除过期快照
            $count=0;
            foreach($snp_XMLDoc->getElementsByTagName("snapshot") as $snapshot){
                if($volume[0]==$snapshot->getAttribute("lvname")&&$snapshot->getAttribute("rotateid")==100){
                    $count++;
                }
            }
            //print "\n\n".$count.": ".$schedule["count"];
            if($count>$schedule["count"]){
            
                foreach($snp_XMLDoc->getElementsByTagName("snapshot") as $snapshot){
                    $snpname=$snapshot->getAttribute("id");
                    $lvname=$snapshot->getAttribute("lvname");
                    if($volume[0]==$lvname && $snapshot->getAttribute("rotateid")==100){
                    
                        $command="/sbin/lvremove -f /dev/".$volume[2]."/of.snapshot.".$volume[0].".".$snpname;

                        print "\n".$command."\n";
                        shell_exec($command);
                        $snp_XMLDoc->removeChild($snapshot);
                        break;
                    }
                }
            }
            
            
            
        }        
    }
    $snp_xml->save($snapshots_path);
    print("\n====================================== \n");
    
?>

  

                       ==========XML部分============

snapshot.xml:

<?xml version="1.0"?>
<snapshots>
<snapshot id="autosn2010-11-09-01-17" lvname="mir1" vgname="ipshot-pool" share="no" rotateid="100" timestamp="2010-11-09 01:17:02 GMT"/>

</snapshots>



schedules.xml:

<?xml version="1.0"?>
<schedules>

<schedule name="test1" interval="1 min" count="4" size="10M">

<volume time="2010-11-08-11-35" vg="ipshot-pool">mir1</volume>

</schedule>

</schedules>



 

目录
相关文章
|
25天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
78 1
|
28天前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
27 1
|
4天前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
13 0
|
17天前
|
关系型数据库 Linux PHP
linux 编译安装php7.2 实测!!
linux 编译安装php7.2 实测!!
10 0
|
17天前
|
NoSQL Linux PHP
php添加redis扩展 linux和windos图文详解 l
php添加redis扩展 linux和windos图文详解 l
3 0
|
24天前
|
Ubuntu Unix Linux
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
|
25天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
16 0
|
25天前
|
Linux Shell
Linux脚本获取输入参数判断文件还是文件路径
Linux脚本获取输入参数判断文件还是文件路径
12 4
|
28天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
29 0
|
29天前
|
Linux Shell 调度
linux脚本任务调度
Linux的`crontab`用于计划任务,按照预设时间执行脚本或命令。步骤包括:1) 创建并赋予执行权限的脚本,如`backup.sh`;2) 使用`crontab -e`编辑任务列表;3) 添加cron表达式(如`0 1 * * * /path/to/backup.sh`,表示每天凌晨1点执行脚本);4) 保存并启动/检查cron服务。cron表达式包含分钟、小时、日期、月份和周几字段。根据需求调整表达式以满足不同任务需求。
12 3