OpenVZ Forum - RDF feed
type: channel
items: Script to backup groups of vms each day via cron job
items: Re: Script to backup groups of vms each day via cron job

Script to backup groups of vms each day via cron job
type: item
description: 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
creator: nolageek
date: 2013-11-21T14:50:03-00:00

Re: Script to backup groups of vms each day via cron job
type: item
description: I've modified the vm-backup.sh so that instead of running the backups, it outputs the value of the different variables that are generated within the script. This is where the problem seems to be: --- the end of the script looks like this: echo $DAILY echo $NUM1 echo $NUM2 echo $TOTAL vzlist -H -o ctid | sed 's/^[ \t]*//' | sed -n "$NUM1,$NUM2 p" ------- This is the output when run as cron: 0 1 1 0 This is the output when run as root on the command line: 5 21 26 26 141 142 143 144 145 146 If I hard code the TOTAL value into the script, then it gets the first values, but not the last one. So, when running in a via cron job, neither of these lines of code in the script are working: TOTAL=`vzlist -H -o ctid|wc -l` vzlist -H -o ctid | sed 's/^[ \t]*//' | sed -n "$NUM1,$NUM2 p" If I put vzlist in a sh file by itself and run it, outputting to a file it doesn't work. Why would that be?
creator: nolageek
date: 2013-11-21T21:15:46-00:00

Re: Script to backup groups of vms each day via cron job
type: item
description: first of all it's not openvz issue so no more replies from me check if cron executed in logs. in cron environment variables are empty. so you have to set them or use full path to executables in scripts.
creator: Paparaciz
date: 2013-11-22T09:10:32-00:00

Re: Script to backup groups of vms each day via cron job
type: item
description: Well, glad to see how helpful the folks here are with fellow openvz users. Anyway, I was able to get this working and it's run the last few days. I'm posting this because I think it may be helpful for others that are looking for a way to stagger backing up larger numbers of VMs on their host nodes. #!/bin/sh # How many days per week do you want to run backups? (starts on monday) DPW=5 ####### END OF CONFIGURATION if [[ $(date +%u) -gt $DPW ]] ; 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=`/usr/sbin/vzlist -H -o ctid|wc -l` DAILY=$(($TOTAL/$DPW)) # if Monday, backup first set of $DAILY number of VMs, if tuesday then second set, etc... DAY="$(date +'%u')" NUM2=$(($DAILY * $DAY)) NUM1=$((($NUM2 - $DAILY)+1)) if [[ ($DAY -eq $DPW) && ($NUM2 -lt $TOTAL) ]]; then NUM2=$TOTAL fi # Get list of all VMs, remove leading whitespace, segment them and then back them up for VM in `/usr/sbin/vzlist -H -o ctid | sed 's/^[ \t]*//' | sed -n "$NUM1,$NUM2 p"`; do /usr/sbin/vzdump --suspend $VM --tmpdir /vz/dump/tmp; done
creator: nolageek
date: 2013-11-25T20:00:20-00:00