Showing posts with label backup. Show all posts
Showing posts with label backup. Show all posts

Friday, June 1, 2012

Debian APC UPS client-server monitoring

Apcupsd is a UPS control system that permits orderly shutdown of your computer in the event of a power failure. We will take a look at NIS (Network Information Server) server and client configuration (this is the case when a single UPS powers several computers).

Server

NIS (Network Information Server) mode allows communication between different hosts. Only one of those hosts, the server, needs to talk to the UPS directly.
  1. Ensure device is connected and recognized. In most cases your UPS is connected to server via USB cable. In Linux you can check this by listing USB devices (provided by usbhid driver):
    deby1:~# ls /dev/usb/
    hiddev0
    
    If your device is no connected, most likely, you will get a message like this:
    ls: cannot access /dev/usb/: No such file or
    directory
    
    Note, in our case the UPS device is available at /dev/usb/hiddev0.
  2. Install UPS monitoring software. Apcupsd is a software designed to control APC UPS devices, let get it installed:
    apt-get install apcupsd
    
  3. Configure apcupsd. Ensure the following settings (file /etc/apcupsd/apcupsd.conf):
    UPSCABLE usb
    
    UPSTYPE  usb
    DEVICE   /dev/usb/hiddev0
    
    NISIP    0.0.0.0
    
    Let apcupsd daemon know it is configured (file /etc/default/apcupsd):
    ISCONFIGURED=yes
    
  4. Start apcupsd service:
    /etc/init.d/apcupsd start
    
    Check UPS status:
    apcaccess status <server name>
    
    Take a look at any errors reported (file /var/log/apcupsd.events):
    ...  apcupsd 3.14.10 (...) debian startup succeeded
    

Client

The client computer will communicate with server via network.
  1. Install UPS monitoring software.
    apt-get install apcupsd
    
  2. Configure apcupsd. Ensure the following settings (file /etc/apcupsd/apcupsd.conf):
    UPSCABLE ether
    
    UPSTYPE net
    #DEVICE  hostname:port
    DEVICE  deby1.dev.local:3551
    
    NETSERVER off
    
    Let apcupsd daemon know it is configured (file /etc/default/apcupsd):
    ISCONFIGURED=yes
    
  3. Start apcupsd service:
    /etc/init.d/apcupsd start
    

Notifications

You are able receive a number of notification events, e.g. power failure, etc (see a complete list of events here). By default apcupsd calls script located at /etc/apcupsd/apccontrol. This script echo some events to user console, as well as shuts down host per doshutdown event. You can easily extend this script to email you events. Here is the script (file /usr/local/sbin/notify.sh):
#!/bin/sh

domain=`hostname -d`
mail=root@$domain
msg=Test

if [ ! -z "$2" ]; then
    mail=$1; msg=$2
    if ! echo $mail | grep -q "$domain"; then
        mail=$mail@$domain
    fi
else
    if [ ! -z "$1" ]; then msg=$1; fi
fi

# strip whitespace at the end of message
msg=`echo "$msg" | sed 's/ *$//g'`

echo $msg | mail -s "$msg" $mail
echo $msg | wall
Ensure the following in apc event handler (file /etc/apcupsd/apccontrol):
#WALL=wall
WALL="xargs -0 notify.sh ups@dev.local"
This will email all events handled by apccontrol to ups@dev.local.

Wednesday, June 1, 2011

How to Backup and Restore OpenLDAP Database

Instead of backup / restore of actual ldap database (hdb, etc) we will export/import ldap directory tree into ldif format that ultimately let us do the same, however without any particular database implementation specifics.

Backup

The backup will be stored in backup.ldif text file.
#!/bin/sh

slapcat -v -l ldap.diff

Restore

The restore will go through replacing current database from a one we have in ldif backup.
#!/bin/sh

# Stop slapd daemon
/etc/init.d/slapd stop

# Remove current database
rm -rf /var/lib/ldap/*

# Import directory tree from backup
slapadd -l backup.ldif

# Fix permissions
chown -R openldap:openldap /var/lib/ldap/*

# Start slapd daemon
/etc/init.d/slapd start

Saturday, April 24, 2010

File backups with tar

Creating an Archive File

tar -czf scripts.tgz scripts/ scripts-test/

List the Contents

tar -tzf scripts.tgz
find a file in archive:
tar -tzf scripts.tgz | grep file.txt

Extracting an Archive File

Now that you know how to create an archive file, it’s rather easy to extract it.
tar -xzf scripts.tgz
You might need extract just one file:
tar -xzf scripts.tgz scripts/readme.txt

Friday, April 23, 2010

Simple tar backup script

Here is a simple backup script (/usr/local/sbin/backup):
#!/bin/bash
#
# creates backups of essential files
#

BACKUPDIR=/var/backups
KEEPDAYS=7

DATA="/root /home /srv \
/boot/grub/menu.lst \
--exclude=$BACKUPDIR"

CONFIG="/etc /var /usr/local \
--exclude=/var/cache/apt/archives \
--exclude=/var/cache/man \
--exclude=/var/run \
--exclude=/var/tmp \
--exclude=/var/lock \
--exclude=$BACKUPDIR \
--exclude=*.gz"

#
# implementation details
#

DATE=`date +%Y-%m-%d-%H-%M`
COMPUTER=`hostname`

# Remove files older than KEEPDAYS days
find "$BACKUPDIR/" -name $COMPUTER-*.tgz \
-type f -mtime +$KEEPDAYS -delete

# Backup data

BACKUPFILE="$BACKUPDIR/$COMPUTER-data-$DATE.tgz"
tar cfz $BACKUPFILE $DATA \
--ignore-failed-read 2> /dev/null

chmod o-o $BACKUPFILE
chgrp adm $BACKUPFILE

# Backup configs

BACKUPFILE="$BACKUPDIR/$COMPUTER-config-$DATE.tgz"
tar cfz $BACKUPFILE $CONFIG \
--ignore-failed-read 2> /dev/null

chmod o-o $BACKUPFILE
chgrp adm $BACKUPFILE
Run it by cron per schedule (file /usr/local/sbin/cron-backup):
#
# Regular cron jobs for backup
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
LOG=/dev/null

# Every day at 21:15
15 21 * * * root test -x /usr/local/sbin/backup && backup >> $LOG
Let cron know about our scheduled backup:
ln -s /usr/local/sbin/cron-backup /etc/cron.d/cron-backup
The backup files are in $BACKUPDIR (/var/backups):
deby:~# ls -lh /var/backups/
total 38M
-rw-r----- 1 root adm   19M 2010-04-23 23:30 deby-config-2010-04-23-23-30.tgz
-rw-r----- 1 root adm  4.3K 2010-04-23 23:30 deby-data-2010-04-23-23-30.tgz
...

What to backup in Linux

Every system should have a backup strategy. It is often not clear what needs to be backed up, and what can be safely ignored. The hierarchies of /var, /etc, and /home are valuable and cannot be restored without a backup. Therefore, these are primary candidates for backups. I have never backed up more than:
/etc, /var, /usr/local, /home, /root and /srv 
This is quite enough to have always been able to restore a broken system or a deleted file when the need arose.