OpenVZ Forum


Home » General » Support » Script to backup groups of vms each day via cron job (Not sure where to put this...)
Script to backup groups of vms each day via cron job [message #50891] Thu, 21 November 2013 14:50 Go to next message
nolageek is currently offline  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
Re: Script to backup groups of vms each day via cron job [message #50892 is a reply to message #50891] Thu, 21 November 2013 21:15 Go to previous messageGo to next message
nolageek is currently offline  nolageek
Messages: 4
Registered: July 2013
Location: United States
Junior Member
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?
Re: Script to backup groups of vms each day via cron job [message #50893 is a reply to message #50891] Fri, 22 November 2013 09:10 Go to previous messageGo to next message
Paparaciz
Messages: 302
Registered: August 2009
Senior Member
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.

Re: Script to backup groups of vms each day via cron job [message #50898 is a reply to message #50893] Mon, 25 November 2013 20:00 Go to previous message
nolageek is currently offline  nolageek
Messages: 4
Registered: July 2013
Location: United States
Junior Member
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

Previous Topic: NOHZ: local_softirq_pending 100 - is there something to worry about?
Next Topic: Comparison with Docker
Goto Forum:
  


Current Time: Thu May 02 04:15:32 GMT 2024

Total time taken to generate the page: 0.01848 seconds