User Tools

Site Tools


bareos_power_on_off_sd

Bareos SD power on/off

Introduction

I have a SAS tapedrive connected to a dedicated server for backups. On this machine bareos-sd is running. I do not want the bareos-sd server powered on all the time when it is only backup-ping a litte time of the day.

So I have made some scripts to power on and off the server. The server is a Supermicro machine with IPMI, so I am using ipmiutils to power on/off the machine.

Powering off the SD

The catalog backup runs with a lower priority so shutting down the server after the catalog backup makes sense. In a schedule I always end with creating a catalog backup. When the SD is powered down, I do not want to power it down again.

Catalog backup

/etc/bareos/bareos-dir.d/job/catalog.conf
Job {
  Name = catalog
  Client = backup-mngt-bh
  Type = Backup
  Level = Full
  Storage = disk-bh
  Pool = disk
  Messages = Standard
  FileSet = catalog
  Schedule = catalog

  # Default
  #RunBeforeJob = /usr/lib/bareos/scripts/make_catalog_backup
  # Helux
  RunBeforeJob = /usr/lib/bareos/scripts/make_catalog_backup.sh
  # Default
  #RunAfterJob  = "/usr/lib/bareos/scripts/delete_catalog_backup"
  # Helux
  RunAfterJob  = "/usr/lib/bareos/scripts/delete_catalog_backup.sh"
#  Write Bootstrap = "|/usr/bin/bsmtp -h localhost -f \"\(Bareos\) \" -s \"Bootstrap for Job %j\" root@localhost" # (#01)
  Priority = 11
}
/usr/lib/bareos/scripts/delete_catalog_backup.sh
#!/bin/sh
#
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
# Copyright (C) 2013-2014 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# This script deletes a catalog dump
#

#
# Source the Bareos config functions.
#
. /usr/lib/bareos/scripts/bareos-config-lib.sh

db_name="${db_name:-bareos}"
working_dir=`get_working_dir`

rm -f ${working_dir}/${db_name}.sql

# added by Helux
/usr/lib/bareos/scripts/poweroff_bareos_sd.sh
/usr/lib/bareos/scripts/poweroff_bareos_sd.sh
#!/bin/bash
BAREOS_SD_IP=172.16.2.23
BAREOS_SD_PORT=9103
IPMI_IP=172.16.2.18
IPMI_USER=USER
IPMI_PASS=PASSWORD
SLEEP=5
NUM_TRIES=6

function check_bareos_sd {
  nc -w1 $BAREOS_SD_IP $BAREOS_SD_PORT < /dev/null 2>/dev/null
  if [ $? -eq 0 ]; then
  # if return value is 0 then port is reachable so we can exit the function with value 0
    return 0
  else
    return 1
  fi
}

COUNT=0
while true; do
  check_bareos_sd
  if [ $? -eq 0 ]; then
    # We are online, so add 1 to counter, sleep for $SLEEP seconds and continue loop
    # $COUNT=0 then we can power off the machine using ssh
    if [ $COUNT -eq 0 ]; then
      ssh -t $BAREOS_SD_IP sudo /usr/sbin/shutdown -h now
    fi
    let COUNT=$COUNT+1
    sleep $SLEEP
  else
    # We are offline, so we can exit the loop
    exit 0
  fi
  # check if $COUNT reaches $NUM_TRIES and when it does do a hard power down
  if [ $COUNT -eq $NUM_TRIES ]; then
    ireset -d -N $IPMI_IP -U $IPMI_USER -P $IPMI_PASS >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      exit 0
    else
      exit 1
    fi
  fi
done

Powering on the SD when running a job

When I need to write a backup to the bareos-sd server, I need to power it on. But I do not want to power it on when it is already on. It is possible a job will be triggered with another job still running.

/etc/bareos/bareos-dir.d/job/example.conf
Job {
  .
  .
  .
  RunBeforeJob = /usr/lib/bareos/scripts/poweron_bareos_sd.sh
}
/usr/lib/bareos/scripts/poweron_bareos_sd.sh
#!/bin/bash
BAREOS_SD_IP=172.16.2.23
BAREOS_SD_PORT=9103
IPMI_IP=172.16.2.18
IPMI_USER=USER
IPMI_PASS=PASSWORD
SLEEP=5
NUM_TRIES=24

function check_bareos_sd {
  nc -w1 $BAREOS_SD_IP $BAREOS_SD_PORT < /dev/null 2>/dev/null
  if [ $? -eq 0 ]; then
  # if return value is 0 then port is reachable so we can exit the function with value 0
    return 0
  else
    return 1
  fi
}

COUNT=0
while true; do
  check_bareos_sd
  if [ $? -eq 0 ]; then
    # We are online, so exit loop
    exit 0
  else
    # We are offline, so add 1 to counter, sleep for $SLEEP seconds and continue loop
    # $COUNT=0 then we can power on the machine using ireset (ipmiutil)
    if [ $COUNT -eq 0 ]; then
      ireset -u -N $IPMI_IP -U $IPMI_USER -P $IPMI_PASS >/dev/null 2>&1
    fi
    let COUNT=$COUNT+1
    sleep $SLEEP
  fi
  # check if $COUNT reaches $NUM_TRIES and when it does exit with value 1
  if [ $COUNT -eq $NUM_TRIES ]; then
    exit 1
  fi
done
bareos_power_on_off_sd.txt · Last modified: by herwarth