一个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>



 

目录
相关文章
|
7天前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
131 9
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
106 0
Linux系统初始化脚本
|
7天前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
60 3
|
3月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
211 1
|
5月前
|
Java Linux
自定义linux脚本用于快速jar包启动、停止、重启
自定义linux脚本用于快速jar包启动、停止、重启
270 29
|
5月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
128 4
|
5月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
270 3
|
6月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
433 25
|
6月前
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
446 28
|
6月前
|
关系型数据库 MySQL Linux
查看Linux、Apache、MySQL、PHP版本的技巧
以上就是查看Linux、Apache、MySQL、PHP版本信息的方法。希望这些信息能帮助你更好地理解和使用你的LAMP技术栈。
297 17