xen-tools/mdmxen/lib/librotation.sh

291 lines
7.0 KiB
Bash

# mdmxen project - library for backup rotation
day_rotation(){
AMOUNT_OF_DAYS=$1
putlog "prepare archive"
putlog "remove old day - ${AMOUNT_OF_DAYS}"
rm -rf "$DAYS_DIR/${AMOUNT_OF_DAYS}"
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS remove"
else putlog "--- FAILURE remove. error code $ret. exit" ; exit 2 ;
fi
tt=""
for (( c=$AMOUNT_OF_DAYS; c>=1; c-- ))
do
if [[ $c == $AMOUNT_OF_DAYS ]]
then
let "y=$c-1"
if ! [[ $y == "0" ]]
then tt=`echo "$y"`
fi
else
let "y=$c-1"
if ! [[ $y == "0" ]]
then tt=`echo "$tt $y"`
fi
fi
done
for j in $tt
do
let "pp=$j + 1"
putlog "move day $j to $pp"
mv "$DAYS_DIR/$j" "$DAYS_DIR/$pp"
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS move dir $j to $pp"
else putlog "--- FAILURE move dir $j to $pp. error code $ret. exit"; exit 3 ;
fi
done
putlog "start create curren day dir"
mkdir -p $DAYS_DIR/1
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS create current day dir"
else putlog "--- FAILURE create current day dir. error code $ret. exit"; exit 4;
fi
putlog "move tmp to last day"
mv $TMP_DIR/* $TARGET_DIR/
ltest $?
}
month_rotation(){
AMOUNT_OF_MONTH=$1
putlog "test first day of month"
if [[ $(date +%d) == "01" ]]
then
putlog "first day of month. start month copy and move"
putlog "remove old dir"
rm -rf $MONTHS_DIR/${AMOUNT_OF_MONTH}
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS remmove dir ${AMOUNT_OF_MONTH}"
else putlog "--- FAILURE remmove dir ${AMOUNT_OF_MONTH}"; exit 27;
fi
tt=""
for (( c=$AMOUNT_OF_MONTH; c>=1; c-- ))
do
if [[ $c == $AMOUNT_OF_DAYS ]]
then
let "y=$c-1"
if ! [[ $y == "0" ]]
then tt=`echo "$y"`
fi
else
let "y=$c-1"
if ! [[ $y == "0" ]]
then tt=`echo "$tt $y"`
fi
fi
done
for j in $tt
do
let "pp=$j + 1"
putlog "move month $j to $pp"
mv "$MONTHS_DIR/$j" "$MONTHS_DIR/$pp"
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS move dir $j to $pp"
else putlog "--- FAILURE move dir $j to $pp. error code $ret. exit"; exit 7;
fi
done
putlog "create current month dir"
mkdir -p $MONTHS_DIR/1
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS create current monts dir"
else putlog "--- FAILURE create current monts dir. error code $ret. exit"; exit 8;
fi
putlog "start copy current day to current month"
cp -r $DAYS_DIR/1/* $MONTHS_DIR/1/
ret=$?
if [[ $ret == "0" ]]
then putlog "--- SUCCESS copy"
else putlog "--- FAILURE copy. error code $ret. exit" ; exit 9;
fi
putlog "stop month copy and move"
else
putlog "skip month copy and move"
fi
}
day_rotation_state(){
DRS_AMOUNT_OF_DAYS=$1
putlog "prepare archive"
putlog "remove old day - ${DRS_AMOUNT_OF_DAYS}"
rm -rf "$DAYS_DIR/${DRS_AMOUNT_OF_DAYS}"
ltest_state "remove old day - ${DRS_AMOUNT_OF_DAYS}"
tt=""
for (( c=${DRS_AMOUNT_OF_DAYS}; c>=1; c-- ))
do
if [[ ${c} == ${DRS_AMOUNT_OF_DAYS} ]]
then
let "y=${c}-1"
if ! [[ ${y} == "0" ]]
then tt=`echo "${y}"`
fi
else
let "y=${c}-1"
if ! [[ ${y} == "0" ]]
then tt=`echo "${tt} ${y}"`
fi
fi
done
for j in ${tt}
do
let "pp=${j} + 1"
putlog "move day ${j} to ${pp}"
mv "${DAYS_DIR}/${j}" "${DAYS_DIR}/${pp}"
ltest_state "move day ${j} to ${pp}"
done
putlog "start create curren day dir"
mkdir -p ${DAYS_DIR}/1
ltest_state "start create curren day dir"
putlog "move tmp to last day"
mv ${TMP_DIR}/* ${TARGET_DIR}/
ltest_state "move tmp to last day"
}
month_rotation_state(){
AMOUNT_OF_MONTH=$1
putlog "test first day of month"
if [[ $(date +%d) == "01" ]]
then
putlog "first day of month. start month copy and move"
putlog "remove old dir"
rm -rf $MONTHS_DIR/${AMOUNT_OF_MONTH}
ltest_state "remove old dir"
tt=""
for (( c=$AMOUNT_OF_MONTH; c>=1; c-- ))
do
if [[ $c == $AMOUNT_OF_DAYS ]]
then
let "y=$c-1"
if ! [[ $y == "0" ]]
then tt=`echo "$y"`
fi
else
let "y=$c-1"
if ! [[ $y == "0" ]]
then tt=`echo "$tt $y"`
fi
fi
done
for j in $tt
do
let "pp=$j + 1"
putlog "move month $j to $pp"
mv "$MONTHS_DIR/$j" "$MONTHS_DIR/$pp"
ltest_state "move month $j to $pp"
done
putlog "create current month dir"
mkdir -p $MONTHS_DIR/1
ltest_state "create current month dir"
putlog "start copy current day to current month"
cp -r $DAYS_DIR/1/* $MONTHS_DIR/1/
ltest_state "start copy current day to current month"
putlog "stop month copy and move"
else
putlog "skip month copy and move"
fi
}
backuper_prepare_rotation_state(){
#okfile
BPR_OKFILE="bone.txt"
#mkdir bin
MKDIR_BIN="/usr/bin/mkdir"
#mv bin
MV_BIN="/usr/bin/mv"
#default status
BPR_DEF_FN_STATUS="0"
#current status
BPR_CUR_FN_STATUS="${BPR_DEF_FN_STATUS}"
if [[ ! ( -z ${1} && -z ${2} && -d ${1} && -d ${2} ) ]]
then
for BPR_CUR_DIR in `ls ${1}`;
do
if [[ -f ${1}/${BPR_CUR_DIR}/${BPR_OKFILE} ]]
then
putlog "Check directory: ${2}/${BPR_CUR_DIR}"
if [[ -d ${2}/${BPR_CUR_DIR} ]]
then
#skip
send_error_to_log "Directory ${2}/${BPR_CUR_DIR} exists. Skipping"
BPR_CUR_FN_STATUS="1"
else
#move
putlog "Make directory: ${2}/${BPR_CUR_DIR}"
${MKDIR_BIN} ${2}/${BPR_CUR_DIR}
ltest_state "Mkdir ${BPR_CUR_DIR}"
putlog "Moving files"
for BPR_CUR_FILE in `ls ${1}/${BPR_CUR_DIR}`
do
putlog "Move ${1}/${BPR_CUR_DIR}/${BPR_CUR_FILE}"
${MV_BIN} ${1}/${BPR_CUR_DIR}/${BPR_CUR_FILE} ${2}/${BPR_CUR_DIR}/
ltest_state "Move ${BPR_CUR_FILE}"
done
fi
else
#OKFILE NOT EXIST
putlog "${1}/${BPR_CUR_DIR}/${BPR_OKFILE} not exist. Skipping"
fi
done
fi
if [[ ${BPR_CUR_FN_STATUS} == ${BPR_DEF_FN_STATUS} ]]
then
[[ "1" == "1" ]]
else
[[ "1" == "0" ]]
fi
}
backuper_rotation_days_state(){
BRDS_DEF_FN_STATUS="0"
BRDS_CUR_FN_STATUS="${BRDS_DEF_FN_STATUS}"
putlog "Start rotation"
send_state "on rotation"
if [[ ! -z ${1} ]]
then
BRDS_AMOUNT_OF_DAYS=${1}
putlog "Start moving files from ${COL_TARGET}"
backuper_prepare_rotation_state "${COL_TARGET}" "${TMP_DIR}"
if [[ ${?} -ne "0" ]]
then
BRDS_CUR_FN_STATUS="1"
else
putlog "Moving files done"
putlog "Rotate dirs..."
day_rotation_state ${BRDS_AMOUNT_OF_DAYS}
putlog "Finish rotate dirs"
fi
else
send_error_to_log "amount of days not set. skipping"
BRDS_CUR_FN_STATUS="1"
fi
if [[ ${BRDS_CUR_FN_STATUS} == ${BRDS_DEF_FN_STATUS} ]]
then
[[ "1" == "1" ]]
else
[[ "1" == "0" ]]
fi
putlog "Finish rotation"
}