Project

General

Profile

1
#!/bin/sh
2

    
3
#
4
# Backup files needed for metacat.  This script creates a temporary directory,
5
# backs up the postgres database, metacat data files, and ldap directory, and 
6
# then creates a gzipped tar file from those files.  It then writes the backup
7
# file to a DVD drive and then cleans up any old backup files.
8
#
9
# To run this file, install it in /usr/sbin or a similar location and add an
10
# entry in the root user's crontab to run the command periodically.  The
11
# following crontab entry would run it every night at 2:00 am
12
# 0 2 * * *       /usr/sbin/backup-metacat.sh >> $HOME/cron-output 2>&1
13
#
14
# This is really just an example script and may not work in your environment
15
# uless you modify it appropriately.
16
#
17
# 13 March 2006 Matt Jones
18

    
19
# Name of the metacat database in postgres to be backed up
20
DBNAME=metacat
21

    
22
# The day of the week on which the DVD is swapped (1-7, 1=Monday)
23
# This must be correct or else the write to DVD will not work
24
SWAPDAY=2
25

    
26
# Number of days of backups to keep online, anything older is removed
27
DAYSTOKEEP=7
28

    
29
# Device to be used for the DVD writer -- this may vary on your system
30
DVD=/dev/dvd
31

    
32
#
33
# Below here lie demons
34
#
35

    
36
# Set up our umask to protect files from prying eyes
37
umask 007
38

    
39
# Make a temp dir for the backed up files
40
TAG=`date +%F-%H%M%S`
41
DATADIR="/var/metacat"
42
ARCHROOT="/var/metacat/metacat-backup"
43
mkdir $ARCHROOT
44
chgrp postgres $ARCHROOT
45
chmod g+rwxs $ARCHROOT
46

    
47
ARCHNAME="metacat-backup-$TAG"
48
ARCHDIR="$ARCHROOT/$ARCHNAME"
49
mkdir $ARCHDIR
50

    
51
# Shut down the tomcat server so nobody else changes anything while we backup
52
/etc/init.d/tomcat stop
53

    
54
# Shut down ldap too
55
/etc/init.d/slapd stop
56

    
57
# Backup postgres
58
su - postgres -c "pg_dump $DBNAME > $ARCHDIR/metacat-postgres-backup.sql"
59

    
60
# Backup the data files
61
tar czf $ARCHDIR/datafiles-backup.tgz --exclude=$ARCHROOT $DATADIR
62

    
63
# Backup LDAP to an LDIF file
64
slapcat -l $ARCHDIR/$DBNAME-ldap.ldif
65

    
66
# Restart LDAP
67
/etc/init.d/slapd start
68

    
69
# Restart tomcat
70
/etc/init.d/tomcat start
71

    
72
# Tar up the archive and copy it to archive media
73
cd $ARCHROOT
74
tar czf $ARCHDIR.tgz $ARCHNAME
75

    
76
# Clean up the temp files
77
rm -rf $ARCHDIR
78

    
79
# Write the backup file to DVD
80
DAY=`date +%u`
81
DVDFLAG=-M
82
if [ $DAY == $SWAPDAY ] ; then
83
  DVDFLAG=-Z
84
fi
85
growisofs $DVDFLAG $DVD -R -J $ARCHDIR.tgz
86

    
87
# clean up any of the backup files that are older than DAYSTOKEEP
88
find $ARCHROOT -mtime +$DAYSTOKEEP -exec rm -f {} \;
(1-1/5)