#!/bin/bash
#By Spinestars
#2013-11-11
#This is a lvsnap of auto-create
Help(){
echo "Usage:
---device eg:sd[b-z]
---partition_number eg:-
---partition_size
-P \"1 2 3\" create partition
eg:lvsnap.sh -P \"sdc 1 4G\"
-p \"1 2\" create lv
-a \"1 2 3\"create all (interactive)
-h Usage
"
}
#**********************************************************
part(){
fdisk -l /dev/$ &&
read -p " if go on ,initialize the $1 partation [any press] or ctrl + c:"
dd if=/dev/zero of=/dev/$ bs= count=
sync
echo 'create partition:'
echo "n
p
$
+$
t
8e
w
p" | fdisk /dev/$1
sync
sleep
partx -a /dev/$$ /dev/$
echo "$1$2 has been created successfully!The size is $3!"
}
#**********************************************************
pv(){
ifpv=`pvdisplay | grep "/dev/$1$2"`
echo 'create pv...'
[ -z "$ifpv" ] &&
pvcreate /dev/$$ ||
echo "Warnning:PV:$1$2 is existed!" && read -p " if go on [any press] or ctrl + c:"
}
#**********************************************************
vg(){
read -p 'create vg(plesae input vgname):' vgname
ifvg=`vgdisplay | grep "$vgname"`
[ -z "$ifvg" ] &&
vgcreate $vgname /dev/$$ ||
echo "Warnning:$vgname is existed!" && read -p "if go on [any press] or ctrl + c:"
}
#**********************************************************
lv(){
read -p 'create lv(please input lvname):' lvname
read -p 'create lv(please input lvsize):' lvsize
iflv=`lvdisplay | grep "/dev/$vgname/$lvname"`
[ -z "$iflv" ] &&
lvcreate -n $lvname -L $lvsize $vgname &&
echo "initialize /dev/mapper/$vgname-$lvname:" &&
mke2fs -t ext4 /dev/mapper/$vgname-$lvname &&
sleep ||
echo "Warnning:$lvname is existed!" &&
read -p "if go on [any press] or ctrl + c:"
read -p "mount /dev/mapper/$vgname-$lvname(please input mount point and if point is not exist ,then create it):" point
ls / | grep "^`basename $point`$" &&
mount | grep "$lvname" &&
echo "$point is existed and is mounted!" ||
{
mkdir -p $point &&
mount -t ext4 /dev/mapper/$vgname-$lvname $point &&
[ $? -eq ] && echo "$vgname-$lvname is mounted successfully!"
}
}
#********************************************************
#while [ ];do
# [ $lvsnapsize -le $lvsize ] ||
# echo "Warnning:$lvsnapsize is too larger!" &&
# read -p 'Retry:input lvsnapsize:' lvsnapsize
#done
#********************************************************
lvsnap(){
read -p 'create lvsnap(please input lvsnapname):' lvsnapname
read -p 'create lvsnap(please input lvsnapsize):' lvsnapsize &&
iflv=`lvdisplay | grep "/dev/$vgname/$lvsnapname"`
[ -z "$iflv" ] &&
lvcreate -s -p r -n $lvsnapname -L $lvsnapsize /dev/mapper/$vgname-$lvname ||
echo "Warnning:$lvsnapname is existed!" && read -p "if go on [any press] or ctrl + c:"
read -p "mount /dev/mapper/$vgname-$lvsnapname(please input mount point and if point is not exist ,then create it):" snappoint
ls / | grep "^`basename $snappoint`$" &&
mount | grep "$lvsnapname" &&
echo "$snappoint is existed and is mounted!" ||
{
mkdir -p $snappoint &&
mount -t ext4 /dev/mapper/$vgname-$lvsnapname $snappoint &&
[ $? -eq ] && echo "$vgname-$lvsnapname is mounted successfully!"
}
}
#*********************************************************
tar2bak(){
bakfile_name="/backup/$lvname/$lvname-`date +%F_%T`.tar.xz"
echo $bakfile_name
mkdir "${bakfile_name%/*}"
echo "tar $snappoint to ${bakfile_name%/*}:"
tar Jcf $bakfile_name $snappoint
[ -f $bakfile_name ] &&
echo "$bakfile_name has been created successfully!"
sleep
echo "remove $lvsnapname:"
umount $snappoint
lvremove /dev/mapper/$vgname-$lvsnapname
}
while getopts P:p:ha: select
do
case $select in
P)
read -p 'partition automation to create[yes|no]:' Partif
[ $Partif = 'yes' ] && part $OPTARG
;;
p)
pv $OPTARG
vg
lv
lvsnap
tar2bak && echo "backup success!" &&
find ${bakfile_name%/*} -amin -1 -type f -ls
;;
a)
read -p 'partition automation to create[yes|no]:' Partif
[ $Partif = 'yes' ] && part $OPTARG
read -p 'Pv automation to create[yes|no]:' Pvif
[ $Pvif = 'yes' ] && pv
read -p 'Vg automation to create[yes|no]:' Vgif
[ $Vgif = 'yes' ] && vg
read -p 'Lv automation to create[yes|no]:' Lvif
[ $Lvif = 'yes' ] && lv
read -p 'Lvsnap automation to create[yes|no]:' Lvsnapif
[ $Lvsnapif = 'yes' ] && lvsnap
read -p 'bak is baginning [yes|no]:' tar2bakif
[ $tar2bakif = 'yes' ] && tar2bak && echo "backup success!" &&
find ${bakfile_name%/*} -amin -1 -type f -ls
;;
*)
Help
;;
esac
done