#!/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"
);
?>