Script to backup groups of vms each day via cron job [message #50891] |
Thu, 21 November 2013 14:50 |
nolageek
Messages: 4 Registered: July 2013 Location: United States
|
Junior Member |
|
|
I wrote the following script that will determine how many VMs are on the machine, divide the number by 5 (week days) and then backup one 1/5 of the VMs each day. This is so that I don't backup all 20+ VMs each day - it takes about 12 hours to do so.
You can easily change the number of days as well, if you like (to 7, for example.)
My problem is that I have it set up as a cron and it doesn't seem to run. Is it a permissions issue? I can run the following command as root and the script runs fine:
sh /root/vm-backup.sh
If I use this in the cron job, nothing happens.
5 0 * * 1,2,3,4,5 sh /root/vm-backup.sh
I tried this as well to get some output and it's always empty:
5 0 * * 1,2,3,4,5 sh /root/vm-backup.sh > /root/vm.backup.log
The file is at /root/vm-backup.sh and is set to 755 with +x (just to be safe)
I also tried 5 0 * * 1,2,3,4,5 /root/vm-backup.sh
#!/bin/sh
if [[ $(date +%u) -gt 4 ]] ; then
echo 'Sorry, you cannot run this program today.'
exit
fi
#figure out how many VMs we have, and figure out how many to backup each day of the week
TOTAL=`vzlist -H -o ctid|wc -l`
DAILY=$(($TOTAL/5))
# if Monday, backup first set of $SET number of VMs, if tuesday then second set, etc...
DAY="$(date +'%u')"
if [ $DAY -eq 1 ]
then
NUM1=1
NUM2=$(($NUM1 + $DAILY))
else
NUM1=$((($DAILY * $DAY) + 1))
NUM2=$(($NUM1 + $DAILY))
fi
# Get list of all VMs, removing leading whitespace
for VM in `vzlist -H -o ctid | sed 's/^[ \t]*//' | sed -n "$NUM1,$NUM2 p"`; do vzdump --suspend $VM --tmpdir /vz/dump/tmp; done
|
|
|