Project

General

Profile

1
#!/bin/sh
2

    
3
#This script is used to move the data of a postgresql 8.4 instance to the
4
#new installed postgresql 9.3 instance after the os was upgraded from ubuntu 10.04 to 14.04.
5
#Basically it will decompress a metacat backup file which contains a dumped-all file and
6
#resotre it in a postgresql 9.3 instance.
7
#You need to pass the metacat backup file to this script. We assume the backup file 
8
#locating at /var/metacat/metacat-backup directory.
9
#This script should be run as the root user. 
10
#Usage: nohup ./restore-dumped-data-to-pg9.3.sh metacat_backup_file_name &
11

    
12
#Check the argument of the script. It only can have one - the metacat backup file name.
13

    
14
METACAT_BACKUP_DIR=/var/metacat/metacat-backup
15
SQL_FILE=metacat-postgres-backup.gz
16
METACAT_BACKUP_FILE_SUFFIX=.tgz
17
DB_BASE=/var/lib/postgresql
18
OLD_DB_VERSION=8.4
19
ANOTHER_OLD_DB_VERSION=9.1
20
NEW_DB_VERSION=9.3
21
NEW_DB_CONFIG=/etc/postgresql/$NEW_DB_VERSION/main/postgresql.conf
22
OLD_DB_DATA_DIR=$DB_BASE/$OLD_DB_VERSION
23
OLD_DB_BACKUP_FILE=postgresql-$OLD_DB_VERSION.tar.gz
24
POSTGRES_USER=postgres
25
PORT=5432
26
echo "start to move database from $OLD_DB_VERSION to $NEW_DB_VERSION at"
27
echo `date`
28

    
29
echo "the length of argument is $#"
30
#echo $@
31
if [ $# -ne 1 ]; then
32
   echo "This script should take one and only one parameter as the metacat backup file name.";
33
   exit 1;
34
fi
35
METACAT_BACKUP_FILE_NAME=$1
36
echo "the backup file name is $METACAT_BACKUP_FILE_NAME"
37

    
38
if [ -f $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME ]; then
39
    echo "the metacat backup file $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME exists.";
40
else 
41
    echo "the metacat backup file $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME doesn't exist, please double check the name.";
42
    exit 1;
43
fi
44

    
45
echo "stop apache"
46
/etc/init.d/apache2 stop
47

    
48
echo "stop tomcat"
49
/etc/init.d/tomcat6 stop
50

    
51
echo "stop postgresql"
52
/etc/init.d/postgresql stop
53

    
54
DECOMPRESS_DIR_NAME=${METACAT_BACKUP_FILE_NAME%%.*}
55
echo "the decmporessed dir is $DECOMPRESS_DIR_NAME"
56

    
57
if [ -f "$DB_BASE/$OLD_DB_BACKUP_FILE" ]; then
58
        echo "$DB_BASE/$OLD_DB_BACKUP_FILE does exist and we don't need to backup it again"   
59
else
60
	echo "back up the old db data at $OLD_DB_DATA_DIR"
61
        su - $POSTGRES_USER -c "tar -zcvf $DB_BASE/$OLD_DB_BACKUP_FILE $OLD_DB_DATA_DIR"
62
	echo "delete the data directory - $OLD_DB_DATA_DIR"
63
	rm -rf $OLD_DB_DATA_DIR/main/*
64
fi
65

    
66
echo "remove postgresql 8.4 and 9.1"
67

    
68
apt-get -y remove postgresql-$OLD_DB_VERSION
69
apt-get -y remove postgresql-$ANOTHER_OLD_DB_VERSION
70

    
71
echo "modify the port to 5432 in the new db configuration file"
72
sed -i.bak --regexp-extended "s/(port =).*/\1${PORT}/;" $NEW_DB_CONFIG
73

    
74
if [ -d "$METACAT_BACKUP_DIR/$DECOMPRESS_DIR_NAME" ]; then
75
        echo "$METACAT_BACKUP_DIR/$DECOMPRESS_DIR_NAME does exist and we don't need to decompress the metacat backup file again"   
76
else
77
	echo "decompress the metacat backup file"
78
	tar zxvf $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME -C $METACAT_BACKUP_DIR
79
fi
80

    
81
echo "restart postgresql"
82
/etc/init.d/postgresql start
83

    
84
echo "change the groupship of $METACAT_BACKUP_DIR"
85
chown -R :$POSTGRES_USER $METACAT_BACKUP_DIR
86

    
87
echo "restore database"
88
su - $POSTGRES_USER -c "gunzip -c $METACAT_BACKUP_DIR/$DECOMPRESS_DIR_NAME/$SQL_FILE | psql postgres"
89

    
90
echo "end to move database from $OLD_DB_VERSION to $NEW_DB_VERSION at"
91
echo `date`
92

    
93
echo "start to vacuum the db at"
94
echo `date` >> /tmp/vacuumdb.out
95
su - postgres  -c "/usr/lib/postgresql/$NEW_DB_VERSION/bin/vacuumdb --all"
96
echo "end to vacuum the db at "
97
echo `date`
98
exit 0
(9-9/10)