Advertisement
Kwaker

Untitled

Apr 18th, 2017
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # postgresql This is the init script for starting up the PostgreSQL
  4. # server.
  5. #
  6. # chkconfig: - 64 36
  7. # description: PostgreSQL database server.
  8. # processname: postmaster
  9. # pidfile="/var/run/${NAME}.pid"
  10.  
  11. # This script is slightly unusual in that the name of the daemon (postmaster)
  12. # is not the same as the name of the subsystem (postgresql)
  13.  
  14. # Version 9.0 Devrim Gunduz <devrim@gunduz.org>
  15. # Get rid of duplicate PGDATA assignment.
  16. # Ensure pgstartup.log gets the right ownership/permissions during initdb
  17.  
  18. # Version 9.1 Devrim Gunduz <devrim@gunduz.org>
  19. # Update for 9.1
  20. # Add an option to initdb to specify locale (default is $LANG):
  21. # service postgresql initdb tr_TR.UTF-8
  22.  
  23. # Version 9.2 Devrim Gunduz <devrim@gunduz.org>
  24. # Update for 9.2
  25.  
  26. # Version 9.2.1 Devrim Gunduz <devrim@gunduz.org>
  27. # Fix version number in initdb warning message, per Jose Pedro Oliveira.
  28. # Add new functionality: Upgrade from previous version.
  29. # Usage: service postgresql-9.2 upgrade
  30.  
  31. # Version 9.2.3 Devrim Gunduz <devrim@gunduz.org>
  32. # Fix longstanding bug: Enable pidfile and lockfile variables to be defined
  33. # in sysconfig file.
  34. # Use $pidfile in status().
  35.  
  36. # Version 9.2.4 Devrim Gunduz <devrim@gunduz.org>
  37. # Fix pid file name in init script, so that it is more suitable for
  38. # multiple postmasters. Per suggestion from Andrew Dunstan. Fixes #92.
  39.  
  40. # Version 9.3.0 Devrim Gunduz <devrim@gunduz.org>
  41. # Add support for pg_ctl promote. Per suggestion from Magnus Hagander. Fixes #93.
  42. # Remove hardcoded script names in init script. Fixes #102.
  43.  
  44. # Version 9.3.1 Devrim Gunduz <devrim@gunduz.org>
  45. # Fix PGPREVMAJORVERSION parameter, per report from Igor Poteryaev.
  46. # Remove extra whitespace in upgrade() code, per report from Igor Poteryaev.
  47.  
  48. # Version 9.3.2 Devrim Gunduz <devrim@gunduz.org>
  49. # Add process name to the status() call. Patch from Darrin Smart
  50.  
  51. # Version 9.4.0 Devrim Gunduz <devrim@gunduz.org>
  52. # Remove PGPORT variable.
  53.  
  54. # PGVERSION is the full package version, e.g., 9.4.0
  55. # Note: the specfile inserts the correct value during package build
  56. PGVERSION=9.4.11
  57. # PGMAJORVERSION is major version, e.g., 9.4 (this should match PG_VERSION)
  58. PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
  59. PGPREVMAJORVERSION=9.3
  60.  
  61. # Source function library.
  62. INITD=/etc/rc.d/init.d
  63. . $INITD/functions
  64.  
  65. # Get function listing for cross-distribution logic.
  66. TYPESET=`typeset -f|grep "declare"`
  67.  
  68. # Get network config.
  69. . /etc/sysconfig/network
  70.  
  71. # Find the name of the script
  72. NAME=`basename $0`
  73. if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
  74. then
  75. NAME=${NAME:3}
  76. fi
  77.  
  78. # For SELinux we need to use 'runuser' not 'su'
  79. if [ -x /sbin/runuser ]
  80. then
  81. SU=runuser
  82. else
  83. SU=su
  84. fi
  85.  
  86. # Define variable for locale parameter:
  87. LOCALEPARAMETER=$2
  88.  
  89. # Set defaults for configuration variables
  90. PGENGINE=/usr/pgsql-9.4/bin
  91. PGDATA=/var/lib/pgsql/9.4/data
  92. PGLOG=/var/lib/pgsql/9.4/pgstartup.log
  93. # Log file for pg_upgrade
  94. PGUPLOG=/var/lib/pgsql/$PGMAJORVERSION/pgupgrade.log
  95.  
  96. lockfile="/var/lock/subsys/${NAME}"
  97. pidfile="/var/run/${NAME}.pid"
  98.  
  99. # Override defaults from /etc/sysconfig/pgsql if file is present
  100. [ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}
  101.  
  102. export PGDATA
  103.  
  104. [ -f "$PGENGINE/postmaster" ] || exit 1
  105.  
  106. script_result=0
  107.  
  108. start(){
  109. [ -x "$PGENGINE/postmaster" ] || exit 5
  110.  
  111. PSQL_START=$"Starting ${NAME} service: "
  112.  
  113. # Make sure startup-time log file is valid
  114. if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
  115. then
  116. touch "$PGLOG" || exit 1
  117. chown postgres:postgres "$PGLOG"
  118. chmod go-rwx "$PGLOG"
  119. [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
  120. fi
  121.  
  122. # Check for the PGDATA structure
  123. if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
  124. then
  125. # Check version of existing PGDATA
  126.  
  127. if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]
  128. then
  129. SYSDOCDIR="(Your System's documentation directory)"
  130. if [ -d "/usr/doc/postgresql-$PGVERSION" ]
  131. then
  132. SYSDOCDIR=/usr/doc
  133. fi
  134. if [ -d "/usr/share/doc/postgresql-$PGVERSION" ]
  135. then
  136. SYSDOCDIR=/usr/share/doc
  137. fi
  138. if [ -d "/usr/doc/packages/postgresql-$PGVERSION" ]
  139. then
  140. SYSDOCDIR=/usr/doc/packages
  141. fi
  142. if [ -d "/usr/share/doc/packages/postgresql-$PGVERSION" ]
  143. then
  144. SYSDOCDIR=/usr/share/doc/packages
  145. fi
  146. echo
  147. echo $"An old version of the database format was found."
  148. echo $"You need to upgrade the data format before using PostgreSQL."
  149. echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information."
  150. exit 1
  151. fi
  152. else
  153. # No existing PGDATA! Warn the user to initdb it.
  154.  
  155. echo
  156. echo "$PGDATA is missing. Use \"service $NAME initdb\" to initialize the cluster first."
  157. echo_failure
  158. echo
  159. exit 1
  160. fi
  161.  
  162. echo -n "$PSQL_START"
  163. $SU -l postgres -c "$PGENGINE/postmaster -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
  164. sleep 2
  165. pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
  166. if [ "x$pid" != x ]
  167. then
  168. success "$PSQL_START"
  169. touch "$lockfile"
  170. echo $pid > "$pidfile"
  171. echo
  172. else
  173. failure "$PSQL_START"
  174. echo
  175. script_result=1
  176. fi
  177. }
  178.  
  179. stop(){
  180. echo -n $"Stopping ${NAME} service: "
  181. if [ -e "$lockfile" ]
  182. then
  183. $SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
  184. ret=$?
  185. if [ $ret -eq 0 ]
  186. then
  187. echo_success
  188. rm -f "$pidfile"
  189. rm -f "$lockfile"
  190. else
  191. echo_failure
  192. script_result=1
  193. fi
  194. else
  195. # not running; per LSB standards this is "ok"
  196. echo_success
  197. fi
  198. echo
  199. }
  200.  
  201. restart(){
  202. stop
  203. start
  204. }
  205.  
  206. initdb(){
  207. # If the locale name is specified just after the initdb parameter, use it:
  208. if [ -z $LOCALEPARAMETER ]
  209. then
  210. LOCALE=`echo $LANG`
  211. else
  212. LOCALE=`echo $LOCALEPARAMETER`
  213. fi
  214. LOCALESTRING="--locale=$LOCALE"
  215.  
  216. if [ -f "$PGDATA/PG_VERSION" ]
  217. then
  218. echo "Data directory is not empty!"
  219. echo_failure
  220. else
  221. echo -n $"Initializing database: "
  222. if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
  223. then
  224. mkdir -p "$PGDATA" || exit 1
  225. chown postgres:postgres "$PGDATA"
  226. chmod go-rwx "$PGDATA"
  227. fi
  228. # Clean up SELinux tagging for PGDATA
  229. [ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
  230.  
  231. # Make sure the startup-time log file is OK, too
  232. if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
  233. then
  234. touch "$PGLOG" || exit 1
  235. chown postgres:postgres "$PGLOG"
  236. chmod go-rwx "$PGLOG"
  237. [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
  238. fi
  239.  
  240. # Initialize the database
  241. $SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident' $LOCALESTRING" >> "$PGLOG" 2>&1 < /dev/null
  242.  
  243. # Create directory for postmaster log
  244. mkdir "$PGDATA/pg_log"
  245. chown postgres:postgres "$PGDATA/pg_log"
  246. chmod go-rwx "$PGDATA/pg_log"
  247.  
  248. [ -f "$PGDATA/PG_VERSION" ] && echo_success
  249. [ ! -f "$PGDATA/PG_VERSION" ] && echo_failure
  250. echo
  251. fi
  252. }
  253.  
  254. upgrade(){
  255.  
  256. # The second parameter is the new database version, i.e. $PGMAJORVERSION in this case.
  257. # Use "postgresql-$PGMAJORVERSION" service, if not specified.
  258. INIT_SCRIPT="$2"
  259. if [ x"$INIT_SCRIPT" = x ]
  260. then
  261. INIT_SCRIPT=postgresql-$PGMAJORVERSION
  262. fi
  263.  
  264. # The third parameter is the old database version, i.e. $PGPREVMAJORVERSION in this case.
  265. # Use "postgresql-$PGPREVMAJORVERSION" service, if not specified.
  266. OLD_INIT_SCRIPT="$3"
  267. if [ x"$OLD_INIT_SCRIPT" = x ]
  268. then
  269. OLD_INIT_SCRIPT=postgresql-$PGPREVMAJORVERSION
  270. fi
  271.  
  272. # Find the init script of the new version:
  273. if [ ! -f "/etc/init.d/${INIT_SCRIPT}" ]
  274. then
  275. echo "Could not find init script /etc/init.d/${INIT_SCRIPT}"
  276. fi
  277.  
  278. # Find the init script of the old version
  279. if [ ! -f "/etc/init.d/${OLD_INIT_SCRIPT}" ]
  280. then
  281. echo "Could not find init script /etc/init.d/${OLD_INIT_SCRIPT}"
  282. echo "Please install postgresql91-server RPM first."
  283. exit
  284. fi
  285.  
  286. # Get port number and data directory of the old instance from the init script
  287. OLDPGDATA=` sed -n 's/^PGDATA=//p' /etc/init.d/postgresql-$PGPREVMAJORVERSION`
  288. OLDPGPORT=`sed -n 's/^PGPORT=//p' /etc/init.d/postgresql-$PGPREVMAJORVERSION`
  289.  
  290. # Get port number and data directory of the new instance from the init script
  291. NEWPGDATA=` sed -n 's/^PGDATA=//p' /etc/init.d/postgresql-$PGMAJORVERSION`
  292. NEWPGPORT=`sed -n 's/^PGPORT=//p' /etc/init.d/postgresql-$PGMAJORVERSION`
  293.  
  294. if [ ! -x "$PGENGINE/pg_upgrade" ]
  295. then
  296. echo
  297. echo $"Please install the postgresql92-contrib RPM for pg_upgrade command."
  298. echo
  299. exit 5
  300. fi
  301.  
  302. # Perform initdb on the new server
  303. /sbin/service $NAME initdb
  304. RETVAL=$?
  305. if [ $RETVAL -ne 0 ]
  306. then
  307. echo "initdb failed!"
  308. exit 1
  309. fi
  310.  
  311. # Check the clusters first, without changing any data:
  312. su -l postgres -c "$PGENGINE/pg_upgrade -b /usr/pgsql-$PGPREVMAJORVERSION/bin/ -B $PGENGINE/ -d $OLDPGDATA -D $NEWPGDATA -p $OLDPGPORT -P $NEWPGPORT -c"
  313. RETVAL=$?
  314. if [ $RETVAL -eq 0 ]
  315. then
  316. echo "Clusters checked successfully, proceeding with upgrade from $PGPREVMAJORVERSION to $PGMAJORVERSION"
  317. echo "Stopping old cluster"
  318. /sbin/service $OLD_INIT_SCRIPT stop
  319.  
  320. # Set up log file for pg_upgrade
  321. rm -f "$PGUPLOG"
  322. touch "$PGUPLOG" || exit 1
  323. chown postgres:postgres "$PGUPLOG"
  324. chmod go-rwx "$PGUPLOG"
  325. [ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
  326.  
  327. echo "Performing upgrade"
  328. su -l postgres -c "$PGENGINE/pg_upgrade \
  329. -b /usr/pgsql-$PGPREVMAJORVERSION/bin/ -B $PGENGINE/ \
  330. -d $OLDPGDATA -D $NEWPGDATA \
  331. -p $OLDPGPORT -P $NEWPGPORT" >> "$PGUPLOG" 2>&1 < /dev/null
  332. else
  333. echo "Cluster check failed. Please see the output above."
  334. exit 1
  335. fi
  336. echo
  337.  
  338. exit 0
  339. }
  340.  
  341.  
  342. condrestart(){
  343. [ -e "$lockfile" ] && restart || :
  344. }
  345.  
  346. reload(){
  347. $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
  348. }
  349.  
  350. promote(){
  351. $SU -l postgres -c "$PGENGINE/pg_ctl promote -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
  352. }
  353.  
  354. # See how we were called.
  355. case "$1" in
  356. start)
  357. start
  358. ;;
  359. stop)
  360. stop
  361. ;;
  362. status)
  363. status -p $pidfile $NAME
  364. script_result=$?
  365. ;;
  366. restart)
  367. restart
  368. ;;
  369. initdb)
  370. initdb
  371. ;;
  372. promote)
  373. promote
  374. ;;
  375. upgrade)
  376. upgrade
  377. ;;
  378. condrestart|try-restart)
  379. condrestart
  380. ;;
  381. reload)
  382. reload
  383. ;;
  384. force-reload)
  385. restart
  386. ;;
  387. *)
  388. echo $"Usage: $0 {start|stop|status|restart|upgrade|condrestart|try-restart|reload|force-reload|initdb|promote}"
  389. exit 2
  390. esac
  391.  
  392. exit $script_result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement