1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #!/bin/sh
#Demande de toutes les informaions dont nous avons besoin
echo -n "Entrez le nom d'utilisateur PostgreSQL [postgres] : "
read UserPG
#Vérification qu'un utilisateur à bien été renseigné pour la connection à PostgreSQL.
case $UserPG in
"") UserPG="postgres"
echo "Bienvenu $UserPG\n";;
* ) echo "Bienvenu $UserPG\n";;
esac
echo -n "Mot de passe : "
read PGpassword
#Vérification qu'un mot de passe à bien été renseigné pour la connection à PostgreSQL.
case $PGpassword in
"") echo "Pas de mot de passe PostgreSQL définit\n"
exit 1;; #exit shell script
*) echo "Votre mot de passe à bien été pris en compte\n";;
esac
echo -n "Ip ou non du serveur PostgreSQL [localhost] ?"
read PGhostname
#Vérification qu'une IP ou un nom machine à bien été renseignée pour la connection à PostgreSQL.
case $PGhostname in
"") PGhostname="localhost"
echo "L'hôte ou l'IP PostgreSQL est $PGhostname\n";;
*) echo "L'hôte ou l'IP PostgreSQL est $PGhostname\n";;
esac
echo -n "Port du serveur PostgreSQL [5432] ? "
read port_num
#Vérification qu'un port à bien été renseigné pour la connection à PostgreSQL.
case $port_num in
"") port_num="5432"
echo "Le port par défaut ($port_num) sera utilisé\n";;
*) echo "Le port suivant ($port_num) sera utilisé\n";;
esac
#Récupération du répertoir courant
date_dump=`ls -d /home/user/application/Dump/* | tail -1 | cut -d '/' -f5`
echo $date_dump
echo -n "Nom de la base de donnée pour Appli? \n [1. PROD] \n [2. QUALIF] \n [3. FORMATION] \n [4. Toutes les plateformes]"
read appli_db
#Vérification qu'une base de donnée à bien été renseignée pour travailler dessus.
case $appli_db in
"" ) echo "Pas de base de donnée PostgreSQL définit\n"
exit 1;; #exit shell script
1|PROD|prod|Prod|production|Production|PRODUCTION)
CaseDB='1'
appli_db="prod_app"
echo "La base de donnée PostgreSQL est $appli_db\n"
dump_location=/home/user/application/Dump/$date_dump/dump_application_Eapp_"$date_dump"_000501.dmp;;
2|QUALIF|qualif|Qualif|QUALIFICATION|qualification|QUALIFICATION)
CaseDB='2'
appli_db="qualif_app"
echo "La base de donnée PostgreSQL est $appli_db\n"
dump_location=/home/user/application/Dump/$date_dump/dump_application_Eapp_"$date_dump"_000501.dmp;;
3|FORMATION|formation|Formation|FORM|form|Form)
CaseDB='3'
appli_db"form_app"
echo "La base de donnée PostgreSQL est $appli_db\n"
dump_location=/home/user/application/Dump/$date_dump/dump_application_Eapp_"$date_dump"_000501.dmp;;
4|tous|toutes|"toutes les bases"|"Toutes les bases")
CaseDB='4'
appli_db1="prod_app"
dump_location1=/home/user/application/Dump/$date_dump/dump_application_Eapp_"$date_dump"_000501.dmp
appli_db2="qualif_app"
dump_location2=/home/user/application/Dump/$date_dump/dump_application_Eapp_"$date_dump"_000501.dmp
appli_db3="form_app"
dump_location3=/home/user/application/Dump/$date_dump/dump_application_Eapp_"$date_dump"_000501.dmp;;
esac
if [ $CaseDB = 1 ] || [ $CaseDB = 2 ] || [ $CaseDB = 3 ] ; then
#Création du fichier permettant de s'identifier au serveur PostgreSQL.
touch $HOME/.pgpass
#Attribution des droits sur le fichier, pas d'autre choix que 0600, si non, ca ne fonctionne pas.
chmod 0600 $HOME/.pgpass
#Remplissage du fichier avec les bons parametres
echo "$PGhostname:$port_num:$appli_db:$UserPG:$PGpassword" > $HOME/.pgpass
#
psql -h $PGhostname -p $port_num $appli_db -U $UserPG << EOF
DROP SCHEMA IF EXISTS application CASCADE;
EOF
# Attention, EOF ne doit pas avaoir d'espace avant. si non, cela génèrera une erreur.
sleep 3
cat $dump_location | psql -h $PGhostname -p $port_num $appli_db -U $UserPG
psql -h $PGhostname -p $port_num $appli_db -U $UserPG << EOF
DROP FUNCTION IF EXISTS pg_catalog.text(integer) CASCADE;
CREATE FUNCTION pg_catalog.text(integer) RETURNS text AS 'SELECT textin(int4out($1));' LANGUAGE sql IMMUTABLE STRICT COST 100;
DROP CAST IF EXISTS (integer AS text) CASCADE;
CREATE CAST (integer AS text) WITH FUNCTION pg_catalog.text(integer) AS IMPLICIT;
EOF
#Efface le mot de passe dans la variable et le fichier.
PGpassword=""
rm $HOME/.pgpass
else
#Création du fichier permettant de s'identifier au serveur PostgreSQL.
touch $HOME/.pgpass
#Attribution des droits sur le fichier, pas d'autre choix que 0600, si non, ca ne fonctionne pas.
chmod 0600 $HOME/.pgpass
#Remplissage du fichier avec les bons parametres
echo "$PGhostname:$port_num:$appli_db1:$UserPG:$PGpassword" > $HOME/.pgpass
#
psql -h $PGhostname -p $port_num $appli_db1 -U $UserPG << EOF
DROP SCHEMA IF EXISTS application CASCADE;
EOF
sleep 3
cat $dump_location1 | psql -h $PGhostname -p $port_num $appli_db1 -U $UserPG
psql -h $PGhostname -p $port_num $appli_db -U $UserPG << EOF
DROP FUNCTION IF EXISTS pg_catalog.text(integer) CASCADE;
CREATE FUNCTION pg_catalog.text(integer) RETURNS text AS 'SELECT textin(int4out($1));' LANGUAGE sql IMMUTABLE STRICT COST 100;
DROP CAST IF EXISTS (integer AS text) CASCADE;
CREATE CAST (integer AS text) WITH FUNCTION pg_catalog.text(integer) AS IMPLICIT;
EOF
#Efface le mot de passe dans la variable et le fichier.
rm $HOME/.pgpass
#Création du fichier permettant de s'identifier au serveur PostgreSQL.
touch $HOME/.pgpass
#Attribution des droits sur le fichier, pas d'autre choix que 0600, si non, ca ne fonctionne pas.
chmod 0600 $HOME/.pgpass
#Remplissage du fichier avec les bons parametres
echo "$PGhostname:$port_num:$appli_db2:$UserPG:$PGpassword" > $HOME/.pgpass
#
psql -h $PGhostname -p $port_num $appli_db2 -U $UserPG << EOF
DROP SCHEMA IF EXISTS application CASCADE;
EOF
sleep 3
cat $dump_location2 | psql -h $PGhostname -p $port_num $appli_db2 -U $UserPG
#Efface le mot de passe dans la variable et le fichier.
psql -h $PGhostname -p $port_num $appli_db -U $UserPG << EOF
DROP FUNCTION IF EXISTS pg_catalog.text(integer) CASCADE;
CREATE FUNCTION pg_catalog.text(integer) RETURNS text AS 'SELECT textin(int4out($1));' LANGUAGE sql IMMUTABLE STRICT COST 100;
DROP CAST IF EXISTS (integer AS text) CASCADE;
CREATE CAST (integer AS text) WITH FUNCTION pg_catalog.text(integer) AS IMPLICIT;
EOF
rm $HOME/.pgpass
#Création du fichier permettant de s'identifier au serveur PostgreSQL.
touch $HOME/.pgpass
#Attribution des droits sur le fichier, pas d'autre choix que 0600, si non, ca ne fonctionne pas.
chmod 0600 $HOME/.pgpass
#Remplissage du fichier avec les bons parametres
echo "$PGhostname:$port_num:$appli_db3:$UserPG:$PGpassword" > $HOME/.pgpass
#
psql -h $PGhostname -p $port_num $appli_db3 -U $UserPG << EOF
DROP SCHEMA IF EXISTS application CASCADE;
EOF
sleep 3
cat $dump_location3 | psql -h $PGhostname -p $port_num $appli_db3 -U $UserPG
#Efface le mot de passe dans la variable et le fichier.
psql -h $PGhostname -p $port_num $appli_db -U $UserPG << EOF
DROP FUNCTION IF EXISTS pg_catalog.text(integer) CASCADE;
CREATE FUNCTION pg_catalog.text(integer) RETURNS text AS 'SELECT textin(int4out($1));' LANGUAGE sql IMMUTABLE STRICT COST 100;
DROP CAST IF EXISTS (integer AS text) CASCADE;
CREATE CAST (integer AS text) WITH FUNCTION pg_catalog.text(integer) AS IMPLICIT;
EOF
PGpassword=""
rm $HOME/.pgpass
fi |
Partager