-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdatabase.sh
More file actions
608 lines (521 loc) · 18.2 KB
/
database.sh
File metadata and controls
608 lines (521 loc) · 18.2 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/bin/bash
XTMP="/tmp/xtdb"
rm -rf $XTMP
mkdir -p $XTMP
database_menu() {
while true; do
DBM=$(whiptail --backtitle "$( window_title )" --menu "$( menu_title Database\ Menu )" 15 60 8 --cancel-button "Exit" --ok-button "Select" \
"1" "Set database info" \
"2" "Clear database info" \
"3" "Backup Database" \
"4" "List Databases" \
"5" "Rename Database" \
"6" "Drop Database" \
"7" "Carve Pilot From Existing Database" \
"8" "Create Database From File" \
"9" "Download Latest Demo Database" \
"10" "Download Specific Database" \
"11" "Return to main menu" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
do_exit
elif [ $RET -eq 0 ]; then
case "$DBM" in
"1") set_database_info ;;
"2") clear_database_info ;;
"3") backup_database ;;
"4") list_databases ;;
"5") rename_database_menu ;;
"6") drop_database_menu ;;
"7") carve_pilot ;;
"8") create_database_from_file ;;
"9") download_latest_demo ;;
"10") download_demo ;;
"11") main_menu ;;
*) msgbox "How did you get here?" && do_exit ;;
esac || database_menu
fi
done
}
# $1 is mode, auto (no prompt for demo location, delete when done)
# manual, prompt for location, don't delete
download_demo() {
if [ -z $1 ]; then
MODE="manual"
else
MODE="auto"
fi
MENUVER=$(whiptail --backtitle "$( window_title )" --menu "Choose Version" 15 60 7 --cancel-button "Exit" --ok-button "Select" \
"1" "PostBooks 4.7.0 Demo" \
"2" "PostBooks 4.7.0 Empty" \
"3" "PostBooks 4.8.1 Demo" \
"4" "PostBooks 4.8.1 Empty" \
"5" "Return to database menu" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$MENUVER" in
"1") VERSION=4.7.0
DBTYPE="demo"
;;
"2") VERSION=4.7.0
DBTYPE="empty"
;;
"3") VERSION=4.8.1
DBTYPE="demo"
;;
"4") VERSION=4.8.1
DBTYPE="empty"
;;
"5") return 0 ;;
*) msgbox "How did you get here?" && exit 0 ;;
esac || database_menu
fi
if [ $MODE = "manual" ]; then
DEMODEST=$(whiptail --backtitle "$( window_title )" --inputbox "Enter the filename where you would like to save the database" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
elif [ $MODE = "auto" ]; then
DEMODEST=$XTMP/$VERSION-$DBTYPE.backup
fi
DB_URL="http://files.xtuple.org/$VERSION/$DBTYPE.backup"
MD5_URL="http://files.xtuple.org/$VERSION/$DBTYPE.backup.md5sum"
dlf_fast $DB_URL "Downloading Demo Database. Please Wait." "$DEMODEST"
dlf_fast $MD5_URL "Downloading MD5SUM. Please Wait." "$DEMODEST".md5sum
echo "Saving "$DB_URL" as "$DEMODEST"."
VALID=`cat "$DEMODEST".md5sum | awk '{printf $1}'`
CURRENT=`md5sum "$DEMODEST" | awk '{printf $1}'`
if [ "$VALID" != "$CURRENT" ]; then
msgbox "There was an error verifying the downloaded database. Utility will now exit."
do_exit
else
if [ $MODE = "manual" ]; then
if (whiptail --title "Download Successful" --yesno "Download complete. Would you like to deploy this database now?." 10 60) then
DEST=$(whiptail --backtitle "$( window_title )" --inputbox "New database name" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
echo "Creating database $DEST from file $DEMODEST"
restore_database $DEMODEST $DEST
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
msgbox "Database $DEST successfully restored from file $DEMODEST"
return 0
fi
else
echo "Exiting without restoring database."
fi
elif [ $MODE = "auto" ]; then
restore_database $DEMODEST $DEST
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
rm $DEMODEST
do_exit
else
return 0
fi
fi
fi
}
download_latest_demo() {
VERSION=$(latest_version db)
if [ -z $VERSION ]; then
msgbox "Could not determine latest database version"
do_exit
fi
if [ -z $DEMODEST ]; then
DEMODEST=$(whiptail --backtitle "$( window_title )" --inputbox "Enter the filename where you would like to save the database" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
else
export DEMODEST
fi
fi
DB_URL="http://files.xtuple.org/$VERSION/demo.backup"
MD5_URL="http://files.xtuple.org/$VERSION/demo.backup.md5sum"
dlf_fast $DB_URL "Downloading Demo Database. Please Wait." "$DEMODEST"
dlf_fast $MD5_URL "Downloading MD5SUM. Please Wait." "$DEMODEST".md5sum
VALID=`cat "$DEMODEST".md5sum | awk '{printf $1}'`
CURRENT=`md5sum "$DEMODEST" | awk '{printf $1}'`
if [ "$VALID" != "$CURRENT" ]; then
msgbox "There was an error verifying the downloaded database. Utility will now exit."
exit
else
if (whiptail --title "Download Successful" --yesno "Download complete. Would you like to deploy this database now?." 10 60) then
DEST=$(whiptail --backtitle "$( window_title )" --inputbox "New database name" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
restore_database $DEMODEST $DEST
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
msgbox "Database $DEST successfully restored from file $DEMODEST"
return 0
fi
else
echo "Exiting without restoring database."
fi
fi
}
# $1 is database file to backup to
# $2 is name of new database (if not provided, prompt)
backup_database() {
check_database_info
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
if [ -z $1 ]; then
DEST=$(whiptail --backtitle "$( window_title )" --inputbox "Full file name to save backup to" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
else
DEST=$1
fi
if [ -z $2 ]; then
SOURCE=$(whiptail --backtitle "$( window_title )" --inputbox "Database name to back up" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
else
SOURCE=$2
fi
echo "Backing up database "$SOURCE" to file "$DEST"."
pg_dump --username "$PGUSER" --port "$PGPORT" --host "$PGHOST" --format custom --file "$DEST" "$SOURCE"
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
msgbox "Database $SOURCE successfully backed up to $DEST"
return 0
fi
}
# $1 is database file to restore
# $2 is name of new database (if not provided, prompt)
restore_database() {
check_database_info
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
if [ -z $2 ]; then
DEST=$(whiptail --backtitle "$( window_title )" --inputbox "New database name" 8 60 "$CH" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
else
DEST=$2
fi
echo "Creating database $DEST."
psql postgres -q -c "CREATE DATABASE "$DEST" OWNER admin"
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
echo "Restoring database $DEST from file $1 on server $PGHOST:$PGPORT"
pg_restore --username "$PGUSER" --port "$PGPORT" --host "$PGHOST" --dbname "$DEST" "$1"
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
return 0
fi
fi
}
# $1 is source
# $2 is new pilot
# prompt if not provided
carve_pilot() {
check_database_info
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
if [ -z "$1" ]; then
DATABASES=()
while read -r line; do
DATABASES+=("$line" "$line")
done < <( su - postgres -c "psql --tuples-only -P format=unaligned -c \"SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'template0', 'template1');\"" )
if [ -z "$DATABASES" ]; then
msgbox "No databases detected on this system"
return 0
fi
SOURCE=$(whiptail --title "PostgreSQL Databases" --menu "Select database to use as source for pilot" 16 60 5 "${DATABASES[@]}" --notags 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
else
SOURCE="$1"
fi
if [ -z "$2" ]; then
PILOT=$(whiptail --backtitle "$( window_title )" --inputbox "Enter new name of database" 8 60 "" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
else
PILOT="$2"
fi
echo "Creating pilot database $PILOT from database $SOURCE"
su - postgres -c "psql postgres -q -p $PGPORT -c \"CREATE DATABASE \"$PILOT\" TEMPLATE \"$SOURCE\" OWNER admin;\""
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
msgbox "Database "$PILOT" has been created"
fi
}
create_database_from_file() {
check_database_info
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
SOURCE=$(whiptail --backtitle "$( window_title )" --inputbox "Enter source backup filename" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
fi
if [ ! -f $SOURCE ]; then
msgbox "File "$SOURCE" not found!"
return 1
fi
PILOT=$(whiptail --backtitle "$( window_title )" --inputbox "Enter new database name" 8 60 "$CH" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
elif [ $RET -eq 0 ]; then
echo "Creating database $PILOT from file $SOURCE"
restore_database $SOURCE $PILOT
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Something has gone wrong. Check output and correct any issues."
do_exit
else
msgbox "Database "$PILOT" has been created"
fi
fi
}
list_databases() {
check_database_info
DATABASES=()
while read -r line; do
DATABASES+=("$line" "$line")
#done < <( su - postgres -c "psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'" )
done < <( su - postgres -c "psql --tuples-only -P format=unaligned -c \"SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'template0', 'template1');\"" )
if [ -z "$DATABASES" ]; then
msgbox "No databases detected on this system"
return 0
fi
#msgbox "`su - postgres -c "psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'"`"
DATABASE=$(whiptail --title "PostgreSQL Databases" --menu "List of databases on this cluster" 16 60 5 "${DATABASES[@]}" --notags 3>&1 1>&2 2>&3)
}
drop_database_menu() {
check_database_info
DATABASES=()
while read -r line; do
DATABASES+=("$line" "$line")
#done < <( su - postgres -c "psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'" )
done < <( su - postgres -c "psql --tuples-only -P format=unaligned -c \"SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'template0', 'template1');\"" )
if [ -z "$DATABASES" ]; then
msgbox "No databases detected on this system"
return 0
fi
DATABASE=$(whiptail --title "PostgreSQL Databases" --menu "Select database to drop" 16 60 5 "${DATABASES[@]}" --notags 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
drop_database "$DATABASE"
}
# $1 is name
# prompt if not provided
drop_database() {
check_database_info
if [ -z "$1" ]; then
POSTNAME=$(whiptail --backtitle "$( window_title )" --inputbox "Enter name of database to drop" 8 60 "" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
else
POSTNAME="$1"
fi
if (whiptail --title "Are you sure?" --yesno "Completely remove database $POSTNAME?" --yes-button "No" --no-button "Yes" 10 60) then
return 0
fi
su - postgres -c "psql -q -c \"DROP DATABASE $POSTNAME;\""
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Dropping database $POSTNAME failed. Please check the output and correct any issues."
do_exit
else
msgbox "Dropping database $POSTNAME successful"
fi
}
rename_database_menu() {
check_database_info
DATABASES=()
while read -r line; do
DATABASES+=("$line" "$line")
done < <( su - postgres -c "psql --tuples-only -P format=unaligned -c \"SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'template0', 'template1');\"" )
if [ -z "$DATABASES" ]; then
msgbox "No databases detected on this system"
return 0
fi
SOURCE=$(whiptail --title "PostgreSQL Databases" --menu "Select database to rename" 16 60 5 "${DATABASES[@]}" --notags 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
DEST=$(whiptail --backtitle "$( window_title )" --inputbox "Enter new database name" 8 60 "" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
rename_database "$SOURCE" "$DEST"
}
# $1 is source
# $2 is new name
# prompt if not provided
rename_database() {
if [ -z "$1" ]; then
SOURCE=$(whiptail --backtitle "$( window_title )" --inputbox "Enter name of database to rename" 8 60 "" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
else
SOURCE="$1"
fi
if [ -z "$2" ]; then
DEST=$(whiptail --backtitle "$( window_title )" --inputbox "Enter new name of database" 8 60 "" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
else
DEST="$2"
fi
su - postgres -c "psql -q -c \"ALTER DATABASE $SOURCE RENAME TO $DEST;\""
RET=$?
if [ $RET -eq 1 ]; then
msgbox "Renaming database $SOURCE failed. Please check the output and correct any issues."
do_exit
else
msgbox "Successfully renamed database $SOURCE to $DEST"
fi
}
set_database_info() {
if (whiptail --title "xTuple Utility v$_REV" --yes-button "Select Cluster" --no-button "Manually Enter" --yesno "Would you like to choose from installed clusters, or manually enter server information?" 10 60) then
CLUSTERS=()
while read -r line; do
CLUSTERS+=("$line" "$line")
done < <( pg_lsclusters | tail -n +2 )
if [ -z "$CLUSTERS" ]; then
msgbox "No database clusters detected on this system"
return 0
fi
CLUSTER=$(whiptail --title "xTuple Utility v$_REV" --menu "Select cluster to use" 16 120 5 "${CLUSTERS[@]}" --notags 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
fi
if [ -z "$CLUSTER" ]; then
msgbox "No database clusters detected on this system"
return 0
fi
export PGVER=`awk '{print $1}' <<< "$CLUSTER"`
export PGNAME=`awk '{print $2}' <<< "$CLUSTER"`
export PGPORT=`awk '{print $3}' <<< "$CLUSTER"`
export PGHOST=localhost
export PGUSER=postgres
PGPASSWORD=$(whiptail --backtitle "$( window_title )" --passwordbox "Enter postgres user password" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
else
export PGPASSWORD
fi
if [ -z "$PGVER" ] || [ -z "$PGNAME" ] || [ -z "$PGPORT" ]; then
msgbox "Could not determine database version or name"
return 0
fi
else
if [ -z $PGHOST ]; then
PGHOST=$(whiptail --backtitle "$( window_title )" --inputbox "Hostname" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
else
export PGHOST
fi
fi
if [ -z $PGPORT ] ; then
PGPORT=$(whiptail --backtitle "$( window_title )" --inputbox "Port" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
else
export PGPORT
fi
fi
if [ -z $PGUSER ] ; then
PGUSER=$(whiptail --backtitle "$( window_title )" --inputbox "Username" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
else
export PGUSER
fi
fi
if [ -z $PGPASSWORD ] ; then
PGPASSWORD=$(whiptail --backtitle "$( window_title )" --passwordbox "Password" 8 60 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return $RET
else
export PGPASSWORD
fi
fi
fi
}
clear_database_info() {
unset PGHOST
unset PGPASSWORD
unset PGPORT
unset PGUSER
}
check_database_info() {
if [ -z $PGHOST ] || [ -z $PGPORT ] || [ -z $PGUSER ] || [ -z $PGPASSWORD ]; then
set_database_info
RET=$?
return $RET
else
return 0
fi
}