1 |
8921
|
tao
|
#!/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 |
8939
|
tao
|
SQL_FILE=metacat-postgres-backup.gz
|
16 |
8921
|
tao
|
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 |
9011
|
tao
|
OLD_DB_CONFIG=/etc/postgresql/$OLD_DB_VERSION/main/postgresql.conf
|
23 |
8921
|
tao
|
OLD_DB_DATA_DIR=$DB_BASE/$OLD_DB_VERSION
|
24 |
|
|
OLD_DB_BACKUP_FILE=postgresql-$OLD_DB_VERSION.tar.gz
|
25 |
|
|
POSTGRES_USER=postgres
|
26 |
|
|
PORT=5432
|
27 |
9011
|
tao
|
PORT1=5435
|
28 |
8921
|
tao
|
echo "start to move database from $OLD_DB_VERSION to $NEW_DB_VERSION at"
|
29 |
|
|
echo `date`
|
30 |
|
|
|
31 |
|
|
echo "the length of argument is $#"
|
32 |
|
|
#echo $@
|
33 |
|
|
if [ $# -ne 1 ]; then
|
34 |
|
|
echo "This script should take one and only one parameter as the metacat backup file name.";
|
35 |
|
|
exit 1;
|
36 |
|
|
fi
|
37 |
|
|
METACAT_BACKUP_FILE_NAME=$1
|
38 |
|
|
echo "the backup file name is $METACAT_BACKUP_FILE_NAME"
|
39 |
8933
|
tao
|
|
40 |
|
|
if [ -f $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME ]; then
|
41 |
|
|
echo "the metacat backup file $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME exists.";
|
42 |
|
|
else
|
43 |
|
|
echo "the metacat backup file $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME doesn't exist, please double check the name.";
|
44 |
|
|
exit 1;
|
45 |
|
|
fi
|
46 |
|
|
|
47 |
8941
|
tao
|
echo "stop apache"
|
48 |
|
|
/etc/init.d/apache2 stop
|
49 |
|
|
|
50 |
|
|
echo "stop tomcat"
|
51 |
|
|
/etc/init.d/tomcat6 stop
|
52 |
|
|
|
53 |
8940
|
tao
|
echo "stop postgresql"
|
54 |
|
|
/etc/init.d/postgresql stop
|
55 |
|
|
|
56 |
8931
|
tao
|
DECOMPRESS_DIR_NAME=${METACAT_BACKUP_FILE_NAME%%.*}
|
57 |
8921
|
tao
|
echo "the decmporessed dir is $DECOMPRESS_DIR_NAME"
|
58 |
|
|
|
59 |
8928
|
tao
|
if [ -f "$DB_BASE/$OLD_DB_BACKUP_FILE" ]; then
|
60 |
|
|
echo "$DB_BASE/$OLD_DB_BACKUP_FILE does exist and we don't need to backup it again"
|
61 |
|
|
else
|
62 |
|
|
echo "back up the old db data at $OLD_DB_DATA_DIR"
|
63 |
|
|
su - $POSTGRES_USER -c "tar -zcvf $DB_BASE/$OLD_DB_BACKUP_FILE $OLD_DB_DATA_DIR"
|
64 |
9011
|
tao
|
#echo "delete the data directory - $OLD_DB_DATA_DIR"
|
65 |
|
|
#rm -rf $OLD_DB_DATA_DIR/main/*
|
66 |
8928
|
tao
|
fi
|
67 |
8921
|
tao
|
|
68 |
9011
|
tao
|
#echo "remove postgresql 8.4 and 9.1"
|
69 |
8921
|
tao
|
|
70 |
9011
|
tao
|
#apt-get -y remove postgresql-$OLD_DB_VERSION
|
71 |
|
|
#apt-get -y remove postgresql-$ANOTHER_OLD_DB_VERSION
|
72 |
8940
|
tao
|
|
73 |
9011
|
tao
|
echo "modify the port to 5435 in the old new db configuraiton file"
|
74 |
|
|
sed -i.bak --regexp-extended "s/(port =).*/\1${PORT1}/;" $OLD_DB_CONFIG
|
75 |
|
|
|
76 |
8921
|
tao
|
echo "modify the port to 5432 in the new db configuration file"
|
77 |
|
|
sed -i.bak --regexp-extended "s/(port =).*/\1${PORT}/;" $NEW_DB_CONFIG
|
78 |
|
|
|
79 |
8928
|
tao
|
if [ -d "$METACAT_BACKUP_DIR/$DECOMPRESS_DIR_NAME" ]; then
|
80 |
|
|
echo "$METACAT_BACKUP_DIR/$DECOMPRESS_DIR_NAME does exist and we don't need to decompress the metacat backup file again"
|
81 |
|
|
else
|
82 |
|
|
echo "decompress the metacat backup file"
|
83 |
|
|
tar zxvf $METACAT_BACKUP_DIR/$METACAT_BACKUP_FILE_NAME -C $METACAT_BACKUP_DIR
|
84 |
|
|
fi
|
85 |
8921
|
tao
|
|
86 |
8928
|
tao
|
echo "restart postgresql"
|
87 |
|
|
/etc/init.d/postgresql start
|
88 |
8921
|
tao
|
|
89 |
8934
|
tao
|
echo "change the groupship of $METACAT_BACKUP_DIR"
|
90 |
|
|
chown -R :$POSTGRES_USER $METACAT_BACKUP_DIR
|
91 |
|
|
|
92 |
8921
|
tao
|
echo "restore database"
|
93 |
8939
|
tao
|
su - $POSTGRES_USER -c "gunzip -c $METACAT_BACKUP_DIR/$DECOMPRESS_DIR_NAME/$SQL_FILE | psql postgres"
|
94 |
8921
|
tao
|
|
95 |
|
|
echo "end to move database from $OLD_DB_VERSION to $NEW_DB_VERSION at"
|
96 |
|
|
echo `date`
|
97 |
8931
|
tao
|
|
98 |
|
|
echo "start to vacuum the db at"
|
99 |
|
|
echo `date` >> /tmp/vacuumdb.out
|
100 |
|
|
su - postgres -c "/usr/lib/postgresql/$NEW_DB_VERSION/bin/vacuumdb --all"
|
101 |
|
|
echo "end to vacuum the db at "
|
102 |
|
|
echo `date`
|
103 |
8933
|
tao
|
exit 0
|