Hi There!
Our scenario in our student dormitory (500 Users): two main cluster servers (each dual-quad xeon/16gig) are hosting our machines. They back up to a NFS-Share on each other, so if one fails the other is able to run all important servers, they back up in raw and do not compress to make the backup as smooth as possible.
Now it was time to set up a third server with more storage for archiving and keeping some backups (compressed) ... i thougt: why not having the last backup imported on that machine to have a always actual hot-standby of ALL our computers? finally it's only a question of storage... the seperate machine has enough time to import, export & compress (i used the pigz-tip here in the forum to speed up compression) everything, so the idea is: the seperate machine fetches tonights backup, imports everything and manages the backup-files.
So I wrote something, below you can see the script. Please forgive the crappy and dirty way of coding, I'm not studying engeneering but medicine ;-)
Feel free co comment, use or what ever you like with it.
Regards from Aachen (Germany),
Thomas
P.S.: Since there is a lot of german language in the script i added some comments in english...
Our scenario in our student dormitory (500 Users): two main cluster servers (each dual-quad xeon/16gig) are hosting our machines. They back up to a NFS-Share on each other, so if one fails the other is able to run all important servers, they back up in raw and do not compress to make the backup as smooth as possible.
Now it was time to set up a third server with more storage for archiving and keeping some backups (compressed) ... i thougt: why not having the last backup imported on that machine to have a always actual hot-standby of ALL our computers? finally it's only a question of storage... the seperate machine has enough time to import, export & compress (i used the pigz-tip here in the forum to speed up compression) everything, so the idea is: the seperate machine fetches tonights backup, imports everything and manages the backup-files.
So I wrote something, below you can see the script. Please forgive the crappy and dirty way of coding, I'm not studying engeneering but medicine ;-)
Feel free co comment, use or what ever you like with it.
Regards from Aachen (Germany),
Thomas
P.S.: Since there is a lot of german language in the script i added some comments in english...
Code:
crontab:
30 10 * * * root /root/backup-script.sh >> /root/backupscriptlog.log
/root/backup-script.sh:
#!/bin/bash
#zum abschalten aller funktionen und des automatischen löschens der VMs hier ein exit einfügen!
# to switch of everything (case of failed main server...) insert a exit below...
#exit
heute=`date +%Y_%m_%d` #today
heutekurz=`date +%Y%m%d` #todayshort
jahrestag=`date +%j` #day of the year
wochentag=`date +%u` #day of the week
monatstag=`date +%e`#day of the month
#if [ -f $heute.heute ]
#then
# echo the file exists
# rm $heute.heute
# exit
#fi
#rm *.heute
#touch $heute.heute
echo lösche alle 400er vms #removing all VM-machines with id 400-499 ---caution, machines are really deleted, essential to have a range for backup
COUNTER=400
while [ $COUNTER -lt 499 ]; do
let COUNTER=COUNTER+1
qm destroy $COUNTER
vzctl destroy $COUNTER
done
date >>/mnt/euro35/proxback/daily/$heute.log
echo kopiere alle maschinen aus dem letzten täglichen backup #get today backups from the four source-directorys (two machines each backing up to each other daily and one weekly copy)
COUNTER=400
for i in $( ls /mnt/pve/wechsels-b1-day/*vzdump-qemu-*$heute*.tar /mnt/pve/wechsels-b2-day/*vzdump-qemu-*$heute*.tar /mnt/pve/wechsels-b1-week/*vzdump-qemu-*$heute*.tar /mnt/pve/wechsels-b2-week/*vzdump-qemu-*$heute*.tar); do
let COUNTER=COUNTER+1
echo importing: $i >>/mnt/euro35/proxback/daily/$heute.log
echo "as machine"$COUNTER >>/mnt/euro35/proxback/daily/$heute.log
qmrestore --storage proxb3 $i $COUNTER
done
for i in $( ls /mnt/pve/wechsels-b2-day/*vzdump-open*$heute*.tar /mnt/pve/wechsels-b1-day/*vzdump-open*$heute*.tar /mnt/pve/wechsels-b1-week/*vzdump-open*$heute*.tar /mnt/pve/wechsels-b2-week/*vzdump-open*$heute*.tar ); do
let COUNTER=COUNTER+1
echo importing: $i >>/mnt/euro35/proxback/daily/$heute.log
echo "as machine"$COUNTER >>/mnt/euro35/proxback/daily/$heute.log
vzrestore $i $COUNTER
done
qm list
vzlist -a
qm list >> /mnt/euro35/proxback/daily/$heute.log #since the id's are screwed up it's essential to have one file that helps to identify the machine
vzlist -a >> /mnt/euro35/proxback/daily/$heute.log
# making the daily backup to the specified directory - i used the pigz-trick to make compression really fast
# result: seven days backup
vzdump --node 10 --snapshot --dumpdir /mnt/euro35/proxback/daily --compress --tmpdir /var/lib/vz/backuptemp --maxfiles 7 --mailto root@kullen.rwth-aachen.de -all
#alt: vzdump --node 10 --snapshot --storage wechsels-b1-week --maxfiles 1 --mailto tkienzle@kullen.rwth-aachen.de -all >>$heute.log
echo $heute $heutekurz $jahrestag $wochentag $monatstag
if [ $jahrestag = 028 ] #if it's a specific day of the year the backup is moved to the archive
then
echo juhu, zeit für ein jahresbackup!
mv /mnt/euro35/proxback/daily/*$heute* /mnt/euro35/proxback/yehaar/
fi
if [ $monatstag = 15 ] #same but month
then
echo juhu, zeit für ein monatsbackup!
mv /mnt/euro35/proxback/daily/*$heute* /mnt/euro35/proxback/monthly/
fi
if [ $wochentag = 1 ] same but week
then
echo juhu, zeit für ein wochenbackup!
mv /mnt/euro35/proxback/daily/*$heute* /mnt/euro35/proxback/weekly/
fi
#altercheck
echo ---------ALTERSCHECK monate - max 1 jahr #of course old backups need to be deleted this deletes everything older than one year
echo heute: $heutekurz
for i in $( ls /mnt/euro35/proxback/monthly/*.tgz ); do
# i=/mnt/pve/wechsels-b1-day/vzdump-qemu-120-2010_01_13-04_28_50.tar
echo löschkandidat: $i #candidate for deletion
loeschkandidat=$i
# reading out the date out of the filename... the f4 specifies the block seperated by "-" = dash characters
datumloeschkandidatkurz=`echo $i | cut -d "-" -f4 | tr -d [_]` #entferne die underscores und nehme den mit nummer bezeichneten block zwischen den bindestrichen im pfadnamen
datumloeschkandidat=`echo $i | cut -d "-" -f4`
# echo $loeschkandidat
echo datum löschkand: $datumloeschkandidat
# ls /mnt/euro35/proxback/daily/*$datumloeschkandidat*.*
#!/usr/bin/bash
#some calculating to convert the dates into secounds
date1=$heutekurz
date2=$datumloeschkandidatkurz
#give the dates to awk
tageabstand=$(echo $date1 $date2 | awk '{
#parse
year1=substr($1,1,4);
month1=substr($1,5,2);
day1=substr($1,7,2);
year2=substr($2,1,4);
month2=substr($2,5,2);
day2=substr($2,7,2);
#get seconds
secs1=((year1 - 1970)*365.25+(month1*30.5)+day1)*24*60*60;
secs2=((year2 - 1970)*365.25+(month2*30.5)+day2)*24*60*60;
#subtract
print ((secs1 - secs2)/86400);
}'| sed -e 's/\..*$//')
#sed removes everything thats not a complete number after the dot
echo alter der datei etwa: $tageabstand Tage #age about ... days
if [ "$tageabstand" -ge "365" ]
then
echo also über ein jahr #that means more than a year
rm /mnt/euro35/proxback/monthly/*$datumloeschkandidat*.*
fi
done
echo ---------ALTERSCHECK woche - max 1 monat #exactly the same system, but now everything older than one month is deleted
echo heute: $heutekurz
for i in $( ls /mnt/euro35/proxback/weekly/*.tgz ); do
# i=/mnt/pve/wechsels-b1-day/vzdump-qemu-120-2010_01_13-04_28_50.tar
echo löschkandidat: $i
loeschkandidat=$i
datumloeschkandidatkurz=`echo $i | cut -d "-" -f4 | tr -d [_]` #entferne die underscores und nehme den mit nummer bezeichneten block zwischen den bindestrichen im pfadnamen
datumloeschkandidat=`echo $i | cut -d "-" -f4`
# echo $loeschkandidat
echo datum löschkand: $datumloeschkandidat
# ls /mnt/euro35/proxback/daily/*$datumloeschkandidat*.*
#!/usr/bin/bash
date1=$heutekurz
date2=$datumloeschkandidatkurz
#give the dates to awk
tageabstand=$(echo $date1 $date2 | awk '{
#parse
year1=substr($1,1,4);
month1=substr($1,5,2);
day1=substr($1,7,2);
year2=substr($2,1,4);
month2=substr($2,5,2);
day2=substr($2,7,2);
#get seconds
secs1=((year1 - 1970)*365.25+(month1*30.5)+day1)*24*60*60;
secs2=((year2 - 1970)*365.25+(month2*30.5)+day2)*24*60*60;
#subtract
print ((secs1 - secs2)/86400);
}'| sed -e 's/\..*$//')
echo alter der datei etwa: $tageabstand Tage
if [ "$tageabstand" -ge "31" ]
then
echo also über einen monat
rm /mnt/euro35/proxback/weekly/*$datumloeschkandidat*.*
fi
done