mdmxen_backup.sh test version

This commit is contained in:
Mike D'Morto 2021-01-28 17:12:52 +07:00
parent 658c4748c9
commit 824aa710df
10 changed files with 1834 additions and 0 deletions

View File

@ -2,3 +2,8 @@ The problem:
1 - manage xen server with simple commands 1 - manage xen server with simple commands
2 - new backup script 2 - new backup script
mdmxen - root dir, on deploy put it /root/bin/mdmxen
- lib - library dir
- etc - config dir, for backup config etc
- bin - shell scripts and other utils
/Users/mikedm/Projects/xen-tools/mdmxen/lib/fn_rotation.fsh

View File

@ -0,0 +1,62 @@
#!/bin/bash
# mdmxen project - backuper for backup vm with days rotation etc
# version 0-test
# include this host config
. /root/bin/mdmxen/etc/host.cfg
# include libraries for work
. /root/bin/mdmxen/lib/libsys.sh
. /root/bin/mdmxen/lib/librotation.sh
. /root/bin/mdmxen/lib/libxen.sh
#main
putlog "---------- Start $ME ----------"
send_state "starting script"
# lock file test
putlog "test lock file"
locktest_state
# test backup directory
putlog "backup target test"
hddtest
ltest_state "backup target test"
# create lock file
putlog "create lock file"
lockcreate
ltest_state "create lock file"
# clear tmp directory
putlog "clear tmp directory"
cleartmp
ltest_state "clear tmp directory"
# begin backup
putlog "begin backup"
send_state "on backup"
send_state "run before actions"
putlog "run before action"
. /root/bin/mdmxen/etc/before.cfg
send_state "run vms actions"
putlog "run vms action"
. /root/bin/mdmxen/etc/vms.cfg
# rotation
putlog "call rotation function"
# AMOUNT_OF_DAYS
send_state "on rotation"
day_rotation_state 3
send_state "run after actions"
putlog "run after action"
. /root/bin/mdmxen/etc/before.cfg
putlog "delete lock file"
lockdelete
ltest_state "delete .lock file"
putlog "create ok file"
okcreate
ltest_state "create ok file"
send_state "done"
putlog "SUCCESS_BACKUP"
putlog "---------- finish $ME ----------"

1
mdmxen/etc/after.cfg Normal file
View File

@ -0,0 +1 @@
# mdmxen project = put here exec to run after backup

1
mdmxen/etc/before.cfg Normal file
View File

@ -0,0 +1 @@
# mdmxen project = put here exec to run before backup

20
mdmxen/etc/host.cfg Normal file
View File

@ -0,0 +1,20 @@
# mdmxen project = host configuration
# state for monitoring
STATE_DIR="/var/tmp/backuper.state"
# log file
FLOG="/var/log/backup.log"
# current hostname
ME="XEN_ME"
# temporary directory for export vms
TMP_DIR="/mnt/backup/tmp"
# lock file
FLOCK="/var/run/backup.lock"
# flag file for backup volume
FBACK="/mnt/backup/iambackup.txt"
# directory for day rotation
DAYS_DIR="/mnt/backupxfs/days"
# current target dir
TARGET_DIR="/mnt/backupxfs/days/1"
# months rotation dir
MONTHS_DIR="/mnt/backupxfs/months"

4
mdmxen/etc/readme.md Normal file
View File

@ -0,0 +1,4 @@
host.cfg current host config
vms.cfg virtual machines list with backup configurations (format vmuuid vmname compress)
before.cfg tasks before backup
after.cfg tasks after backup, for example rsync time machine etc

11
mdmxen/etc/vms.cfg Normal file
View File

@ -0,0 +1,11 @@
# mdmxen project = virtual machines list for backup
#
# backup vm run state from snapshot where UUID and file name and archive trigger (true false)
# backup_vm_run_snap UUID name true
#
# backup vm poweroff state
# backup_vm_off UUID name true
#
# backup vm poweroff state and snapshot created and vm running again.
# so backup from snapshot when poweroff state
# backup_vm_off_snap UUID name true

290
mdmxen/lib/librotation.sh Normal file
View File

@ -0,0 +1,290 @@
# 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"
}

1358
mdmxen/lib/libsys.sh Normal file

File diff suppressed because it is too large Load Diff

82
mdmxen/lib/libxen.sh Normal file
View File

@ -0,0 +1,82 @@
# mdmxen project - library for xen
backup_vm_run_snap(){
VM_UUID=$1
VM_NAME=$2
COMPRESS=$3
putlog "start backup_vpm_run_snap $VM_NAME"
putlog "create snapshot"
SNAP_UUID=$(xe vm-snapshot uuid=$VM_UUID new-name-label=snap-$VM_NAME)
ltest_state "create snapshot"
putlog "prepare export"
xe template-param-set is-a-template=false ha-always-run=false uuid=$SNAP_UUID
ltest_state "prepare export"
putlog "export vm"
xe vm-export vm=$SNAP_UUID filename=$TMP_DIR/$VM_NAME.xva compress=$COMPRESS
ltest_state "export vm"
putlog "delete snapshot"
xe vm-uninstall uuid=$SNAP_UUID force=true
ltest_state "delete snapshot"
putlog "stop backup_vpm_run_snap $VM_NAME"
}
backup_vm_off(){
VM_UUID=$1
VM_NAME=$2
COMPRESS=$3
putlog "start backup_vm_off $VM_NAME"
putlog "shutdown vm $VM_NAME"
xe vm-shutdown vm=$VM_UUID
ltest_state "shutdown vm $VM_NAME"
sleep 60
putlog "export vm $VM_NAME"
xe vm-export vm=$VM_UUID filename=$TMP_DIR/$VM_NAME.xva compress=$COMPRESS
ltest_state "export vm $VM_NAME"
putlog "start vm $VM_NAME"
xe vm-start vm=$VM_UUID
ltest_state "run vm $VM_NAME"
sleep 60
putlog "stop backup_vm_off $VM_NAME"
}
backup_vm_off_snap(){
VM_UUID=$1
VM_NAME=$2
COMPRESS=$3
putlog "start backup_vm_off_snap $VM_NAME"
putlog "shutdown vm $VM_NAME"
xe vm-shutdown vm=$VM_UUID
ltest_state "shutdown vm $VM_NAME"
sleep 60
putlog "create snapshot"
SNAP_UUID=$(xe vm-snapshot uuid=$VM_UUID new-name-label=snap-$VM_NAME)
ltest_state "create snapshot"
putlog "start vm $VM_NAME"
xe vm-start vm=$VM_UUID
ltest_state "run vm $VM_NAME"
sleep 60
putlog "prepare export"
xe template-param-set is-a-template=false ha-always-run=false uuid=$SNAP_UUID
ltest_state "prepare export"
putlog "export vm"
xe vm-export vm=$SNAP_UUID filename=$TMP_DIR/$VM_NAME.xva compress=$COMPRESS
ltest_state "export vm"
putlog "delete snapshot"
xe vm-uninstall uuid=$SNAP_UUID force=true
ltest_state "delete snapshot"
putlog "stop backup_vm_off_snap $VM_NAME"
}