Project

General

Profile

1
#!/bin/bash
2

    
3
#
4
# Backup files needed for metacat.  This script creates a directory in /var/metacat/backup,
5
# backs up the postgres database, metacat data files, and certificate and web config files,
6
# then syncs those files to an Amazon S3 bucket for backup.
7
#
8
# To run this file, install it in /usr/sbin or a similar location and add an
9
# entry in the root user's crontab to run the command periodically.  The
10
# following crontab entry would run it every night at 2:00 am
11
# 0 2 * * *       /usr/sbin/backup-aws.sh >> $HOME/cron-output 2>&1
12
#
13
# This is really just an example script and may not work in your environment
14
# uless you modify it appropriately.
15
#
16
# 25 Apr 2017 Matt Jones
17

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

    
21
# Number of days of backups to keep online, anything older is removed
22
DAYSTOKEEP=7
23

    
24
# AWS S3 bucket to be used for backup
25
BUCKET=s3://arcticdata.io/backup
26

    
27
# Location of the metacat.properties file
28
METACATPROPERTIESPATH=/var/lib/tomcat7/webapps/metacat/WEB-INF/metacat.properties
29

    
30
# Location of the apache configuration file
31
APACHECONF=/etc/apache2/sites-enabled
32

    
33
#Location of the server key
34
KEYLOCATION=/etc/ssl/private 
35

    
36
#Location of the server certificate
37
CERTLOCATION=/etc/ssl/certs/www_arcticdata_io.crt
38

    
39
#
40
# Below here lie demons
41
#
42

    
43
# Set up our umask to protect files from prying eyes
44
umask 007
45

    
46
# Make a temp dir for the backed up files
47
TAG=`date +%F-%H%M%S`
48
DATADIR="/var/metacat"
49
ARCHROOT="/var/metacat/metacat-backup"
50
mkdir -p $ARCHROOT
51
chgrp postgres $ARCHROOT
52
chmod g+rwxs $ARCHROOT
53

    
54
ARCHDIR="$ARCHROOT"
55
mkdir -p $ARCHDIR
56

    
57
# Locations of file lists already archived
58
DATALIST=${ARCHDIR}/data-list
59
METALIST=${ARCHDIR}/meta-list
60

    
61
# Shut down the tomcat server so nobody else changes anything while we backup
62
#/etc/init.d/tomcat7 stop
63

    
64
echo Generate a list of new metadata files since the last backup
65
if [ ! -e ${METALIST} ];
66
then
67
	aws s3 ls ${BUCKET}/documents/ |awk -F" " '{print $4}' > ${METALIST}
68
fi
69
diff --old-line-format="" --unchanged-line-format="" <(sort ${METALIST}) <(ls -1 ${DATADIR}/documents | sort) > ${METALIST}-new
70

    
71
echo Generate a list of new data files since the last backup
72
if [ ! -e ${DATALIST} ];
73
then
74
	aws s3 ls ${BUCKET}/data/ |awk -F" " '{print $4}' > ${DATALIST}
75
fi
76
diff --old-line-format="" --unchanged-line-format="" <(sort ${DATALIST}) <(ls -1 ${DATADIR}/data | sort) > ${DATALIST}-new
77

    
78
echo Copy the metacat.properties file to /var/metacat
79
cp $METACATPROPERTIESPATH $ARCHDIR
80

    
81
echo Backup postgres
82
su - postgres -c "pg_dumpall | gzip > $ARCHDIR/metacat-postgres-backup.gz"
83

    
84
echo Copy the apache configuration files
85
tar czhf $ARCHDIR/apache-config-backup.tgz $APACHECONF $KEYLOCATION $CERTLOCATION
86

    
87
echo Sync the backup directory to Amazon S3
88
echo Handle each of the subdirectories independently
89
aws s3 sync $DATADIR/certs $BUCKET/certs
90
aws s3 sync $DATADIR/dataone $BUCKET/dataone
91
aws s3 sync $DATADIR/inline-data $BUCKET/inline-data
92
aws s3 sync $DATADIR/logs $BUCKET/logs
93
aws s3 sync $DATADIR/.metacat $BUCKET/.metacat
94
aws s3 sync $DATADIR/metacat-backup $BUCKET/metacat-backup
95
#aws s3 sync $DATADIR/solr-home $BUCKET/solr-home
96
#aws s3 sync $DATADIR/tdb $BUCKET/tdb
97
#aws s3 sync $DATADIR/temporary $BUCKET/temporary
98

    
99
echo Backup metadata files to S3
100
cat ${METALIST}-new | xargs -n1 -P30 -I {} aws s3 cp ${DATADIR}/documents/{} $BUCKET/documents/{}
101
cat ${METALIST}-new >> ${METALIST}
102

    
103
echo Backup data files to S3
104
cat ${DATALIST}-new | xargs -n1 -P30 -I {} aws s3 cp ${DATADIR}/data/{} $BUCKET/data/{}
105
cat ${DATALIST}-new >> ${DATALIST}
106

    
107
# Restart tomcat
108
#/etc/init.d/tomcat7 start
109

    
110
# Clean up the temp files
111
#rm -rf $ARCHDIR
112

    
113
# clean up any of the backup files that are older than DAYSTOKEEP
114
#find $ARCHROOT -mtime +$DAYSTOKEEP -exec rm -f {} \;
115

    
116
echo "DONE backup for $TAG"
117

    
(2-2/13)