#!/bin/bash
#By Spinestars
#2013-11-11
#This is a lvsnap of auto-create
Help(){
echo "Usage:
1---device eg:sd[b-z]
2---partition_number eg:1-4
3---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/$1 &&
read -p " if go on ,initialize the $1 partation [any press] or ctrl + c:"
dd if=/dev/zero of=/dev/$1 bs=512 count=1
sync
echo 'create partition:'
echo "n
p
$2
+$3
t
8e
w
p" | fdisk /dev/$1
sync
sleep 3
partx -a /dev/$1$2 /dev/$1
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/$1$2 ||
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/$1$2 ||
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 3 ||
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 0 ] && echo "$vgname-$lvname is mounted successfully!"
}
}
#********************************************************
#while [ 1 ];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 0 ] && 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 3
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