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
|
# Location the metacat.properties file
|
33
|
METACATPROPERTIESPATH=/var/lib/tomcat6/webapps/knb/WEB-INF/metacat.properties
|
34
|
#
|
35
|
# Below here lie demons
|
36
|
#
|
37
|
|
38
|
# Set up our umask to protect files from prying eyes
|
39
|
umask 007
|
40
|
|
41
|
# Make a temp dir for the backed up files
|
42
|
TAG=`date +%F-%H%M%S`
|
43
|
DATADIR="/var/metacat"
|
44
|
ARCHROOT="/var/metacat/metacat-backup"
|
45
|
mkdir $ARCHROOT
|
46
|
chgrp postgres $ARCHROOT
|
47
|
chmod g+rwxs $ARCHROOT
|
48
|
|
49
|
ARCHNAME="metacat-backup-$TAG"
|
50
|
ARCHDIR="$ARCHROOT/$ARCHNAME"
|
51
|
mkdir $ARCHDIR
|
52
|
|
53
|
# Shut down the tomcat server so nobody else changes anything while we backup
|
54
|
/etc/init.d/tomcat6 stop
|
55
|
|
56
|
# Shut down ldap too
|
57
|
#/etc/init.d/slapd stop
|
58
|
|
59
|
# Copy the metacat.properties file to /var/metacat
|
60
|
cp $METACATPROPERTIESPATH $DATADIR
|
61
|
|
62
|
# Backup postgres
|
63
|
su - postgres -c "pg_dump $DBNAME > $ARCHDIR/metacat-postgres-backup.sql"
|
64
|
|
65
|
# Backup the data files
|
66
|
tar czf $ARCHDIR/datafiles-backup.tgz --exclude=$ARCHROOT $DATADIR
|
67
|
|
68
|
# Backup LDAP to an LDIF file
|
69
|
slapcat -l $ARCHDIR/$DBNAME-ldap.ldif
|
70
|
|
71
|
# Restart LDAP
|
72
|
#/etc/init.d/slapd start
|
73
|
|
74
|
# Restart tomcat
|
75
|
/etc/init.d/tomcat6 start
|
76
|
|
77
|
# Tar up the archive and copy it to archive media
|
78
|
cd $ARCHROOT
|
79
|
tar czf $ARCHDIR.tgz $ARCHNAME
|
80
|
|
81
|
# Clean up the temp files
|
82
|
rm -rf $ARCHDIR
|
83
|
|
84
|
# Write the backup file to DVD
|
85
|
DAY=`date +%u`
|
86
|
DVDFLAG=-M
|
87
|
if [ $DAY == $SWAPDAY ] ; then
|
88
|
DVDFLAG=-Z
|
89
|
fi
|
90
|
growisofs $DVDFLAG $DVD -R -J $ARCHDIR.tgz
|
91
|
|
92
|
# clean up any of the backup files that are older than DAYSTOKEEP
|
93
|
find $ARCHROOT -mtime +$DAYSTOKEEP -exec rm -f {} \;
|