VMBackup
Rotazione backup macchine virtuali
- pianificazione
NOTA: per mantenere una finestra scorrevole servono due backup; i.e. per avere sempre a disposizione un backup con un'eta' compresa tra 7 e 14 giorni e' possibile effettuare un backup ogni 7 giorni e mantenere gli ultimi 2 backup effettuati. In base a quanto discusso ieri e allo scheduling: 1/2/6/16/44 visto quanto scritto in NOTA ho pensato che possiamo adottare questo scheduling: 1daily/1week/1month (ciascuno con "profondita'" di due backup - avendo dunque per la serie mensile disponibile sempre almeno un backup compreso nella finestra temporale 1month ago - 2months ago
- la priorita' decrescente dei backup e' monthly, weekly, daily: i.e. quando viene eseguito il backup monthly non viene eseguito il backup daily
2. implementazione ed esecuzione i backup si suddividono in serie per frequenza,
- la serie viene etichettata nel nome del backup appendendo il suffisso "_monthly", "_weekly", "_daily"
- ogni serie ha una sua pianificazione in crontab; in virtu' della priorita' la serie weekly esegue solo nei dom (days of month) 2-31 e la serie daily esegue solo nei dom 2-31 e dow (days of week) 2-7.
- lo script si suddivide nella fase di backup e di rotazione (rotate) ed e' parametrizzato in funzione della frequenza ("daily", "weekly", "monthly").
#!/bin/bash
data=`date +%Y-%m-%d-%H%M-`
max_tentativi="30"
#sleep_time in seconds
sleep_time="60"
vmname='UbuLearning'
vmpath='/usr/local/virtual/Ubuntu7.10_elearning'
vm_bkppath=/media/usb/backup/$data$vmname
vmxfile=$vmpath/Ubuntu7.10_server.vmx
vmprocess=$vmxfile
# usage e.g. backup daily
function backup(){
freq="$1"
#check disco usb montato
if mount|grep /media/usb>/dev/null; then echo usb montato;else
exit 1;fi;
#stop the virtual machine
vmware-cmd $vmxfile stop soft
# checking vm status
#echo Checking $vmprocess status
counter="0"
while vmrun list | grep $vmprocess>/dev/null; do
sleep $sleep_time;
counter=$(($counter+1));
#echo counter is $counter;
if [ "$counter" -ge "$max_tentativi" ];
# vm did not shutdown
then exit 1;
fi
done
# assuming vm stopped
#echo Copying $vmpath to ${vm_bkppath}_$freq
cp -r $vmpath ${vm_bkppath}_$freq
# aggiungere codice di checksum e gestione errori et cetera
# restarting vm
#echo Restarting $vmxfile
vmrun start $vmxfile
}
#move the last (cronologically) backup from the $1 series to
#the $2 series. Backups are labelled per series.
function move_last(){
#source series
freq_from="$1";
#destination series
freq_to="$2";
# last backup is moved
to_be_moved=`ls -1t /media/usb/backup | grep .*UbuLearning_${freq_from} | tail -n 1`;
[ -n $to_be_moved ] && mv /media/usb/backup/${to_be_moved} /media/usb/backup/${to_be_moved%%${freq_from}}$freq_to;
}
# usage e.g rotate daily
function rotate(){
freq="$1";
#echo freq "$freq";
# number of backups to keep
nbkps="2";
#echo nbkps "$nbkps";
del_bkps=`ls -1t /media/usb/backup | grep .*UbuLearning_$freq | tail -n +$(($nbkps+1))`;
#echo $del_bkps;
for f in $del_bkps; do
rm -rf /media/usb/backup/"$f";
done;
}
[ `date +%d` -eq 1 ] && { move_last weekly monthly && rotate monthly; };
[ `date +%d` -eq 1 ] && { move_last daily weekly && rotate weekly; };
{backup daily && rotate daily; };
data=`date +%Y-%m-%d-%H%M-`
max_tentativi="30"
#sleep_time in seconds
sleep_time="60"
vmname='UbuLearning'
vmpath='/usr/local/virtual/Ubuntu7.10_elearning'
vm_bkppath=/media/usb/backup/$data$vmname
vmxfile=$vmpath/Ubuntu7.10_server.vmx
vmprocess=$vmxfile
# usage e.g. backup daily
function backup(){
freq="$1"
#check disco usb montato
if mount|grep /media/usb>/dev/null; then echo usb montato;else
exit 1;fi;
#stop the virtual machine
vmware-cmd $vmxfile stop soft
# checking vm status
#echo Checking $vmprocess status
counter="0"
while vmrun list | grep $vmprocess>/dev/null; do
sleep $sleep_time;
counter=$(($counter+1));
#echo counter is $counter;
if [ "$counter" -ge "$max_tentativi" ];
# vm did not shutdown
then exit 1;
fi
done
# assuming vm stopped
#echo Copying $vmpath to ${vm_bkppath}_$freq
cp -r $vmpath ${vm_bkppath}_$freq
# aggiungere codice di checksum e gestione errori et cetera
# restarting vm
#echo Restarting $vmxfile
vmrun start $vmxfile
}
#move the last (cronologically) backup from the $1 series to
#the $2 series. Backups are labelled per series.
function move_last(){
#source series
freq_from="$1";
#destination series
freq_to="$2";
# last backup is moved
to_be_moved=`ls -1t /media/usb/backup | grep .*UbuLearning_${freq_from} | tail -n 1`;
[ -n $to_be_moved ] && mv /media/usb/backup/${to_be_moved} /media/usb/backup/${to_be_moved%%${freq_from}}$freq_to;
}
# usage e.g rotate daily
function rotate(){
freq="$1";
#echo freq "$freq";
# number of backups to keep
nbkps="2";
#echo nbkps "$nbkps";
del_bkps=`ls -1t /media/usb/backup | grep .*UbuLearning_$freq | tail -n +$(($nbkps+1))`;
#echo $del_bkps;
for f in $del_bkps; do
rm -rf /media/usb/backup/"$f";
done;
}
[ `date +%d` -eq 1 ] && { move_last weekly monthly && rotate monthly; };
[ `date +%d` -eq 1 ] && { move_last daily weekly && rotate weekly; };
{backup daily && rotate daily; };
crontab scheduling
#m h dom mon dow command
0 5 * * * /home/virtu/elearningVM-backup.sh
0 5 * * * /home/virtu/elearningVM-backup.sh