[Preface]

Enstore database has three components: (1) back-end database server,
which is served by PostgreSQL, (2) python interface to back-end
database server, which is PyGreSQL and (3) enstore servers,
file_clerk.py, volume_clerk.py and info_server.py..

[Software Building and Installation]

[a] To build and install PostgreSQL from scratch:
    [1] get the source from www.postgresql.org
        assuming it is postgresql-8.1.3.tar.gz
    [2] make installation environment, as user "products":
        $ ksu products
        $ mkdir -p /fnal/ups/prd/postgres/v8_1_3
        $ cd /fnal/ups/prd/postgres/v8_1_3
        $ cd postgresql-8.1.3
        $ configure --prefix=/fnal/ups/prd/postgres/v8_1_3
        $ make all; make install
    [3] copy ups directory from previous version
        $ cd /fnal/ups/prd/postgres/v8_1_3
        $ cp -r ../v8_0_3/ups .
    [4] declare it to ups
        $ ups declare -c -r /fnal/ups/prd/postgres/v8_1_3 -f Linux+2.4 -M ups -m postgres.table postgres v8_1_3

[b] To build and install PyGreSQL from scratch:
    [1] get PyGreSQL from www.pygresql.org
        assuming the file is PyGreSQL.tgz (PyGreSQL-3.6.2)
    [2] as user "products":
        $ ksu products
        $ gunzip -c PyGreSQL.tgz | tar xvf -
        $ cd PyGreSQL-3.6.2
        $ setup enstore # to setup python and postgres
        (alternatively you may setup postgres v8_1_3; setup python v2_2_3_E_1)
        $ python setup.py build
        $ python setup.py install
        (it is installed at $PYTHON_DIR/lib/python2.2/site-packages)

[Set up database environment]

[create enstoredb from scratch]
[c] Set up database area, as user "products" on *srv0
    [0] ksu products
    [1] get enstore and postgres
        $ setup enstore
    [2] create directory for database area
        $ mkdir -p /diskb/enstore-db
        (you might need to be root to create that path then change the
         ownership to user "products")
    [3] prepare database area
        $ initdb -D /diskb/enstore-db
    [4] edit permission
        $ vi /diskb/enstore-db/pg_hba.conf
        # let user "enstore" have access from same subnet

[d] Start database server, as user "root" or "products" on *srv0
    [1] using rc script:
        $ /etc/rc.d/init.d/enstore-db start
    [2] or, alternatively, under extreme condition, it may be started by
        hand as user "products":
        $ postmaster -p 8888 -D /diskb/enstore-db -i &
    (/etc/rc.d/init.d/enstore-db would start databse as user "products",
     even if it is invoked by root. PostgreSQL would not allow root to
     start a databse server.)

(this step [e] is NOT part of creating database procedure. It is just to
 show how to stop a running database server.)
[e] Stop database server, as user "root" or "products" on *srv0
    [1] using rc script:
        $ /etc/rc.d/init.d/enstore-db stop
    [2] or, alternatively, under extreme condition, it may be stopped by
        hand:
        find the process id of postmaster and kill it gently

[f] create accounts for database
    [1] as user "products":
        $ ksu products
        $ createuser -p 8888 -d -S -R -P enstore
        (enter passowrd: enstore_user)
    [2] as user "enstore":
        $ ksu enstore
        $ createdb -p 8888 enstoredb
    [3] as user "products":
        $ ksu products
        $ setup enstore
        $ psql -p 8888 enstoredb -f $ENSTORE_DIR/databases/schemas/enstoredb.schema

[g] start file_clerk, volume_clerk and info_server, as user "enstore":
    on *srv0
    $ setup enstore
    $ enstore start
    or, start them individually:
    $ enstore start --just volume_clerk
    $ enstore start --just file_clerk
    $ enstore start --just info_server

[The following sections about accounting and drivestat dbs are OLD.
 They are only true for CDFEN. The NEW configuration is in the next
 section. STKEN and D0EN have already been using new configuration]

[Create accounting db from scratch]

Similar to above procedure, except:
database server: *srv6
db_port: 5432
db_name: enstore
database_area: /diska/enstore-pgdb
rc script: /etc/rc.d/init.d/enstore-pgdb
schema: $ENSTORE_DIR/databases/schemas/accounting.schema

[Create drivestat db from scratch]

Similar to above procedure, except:
database server: *srv6
db_port: 8076
db_name: drivestat
database_area: /diska/drivestat_db
rc script: /etc/rc.d/init.d/enstore-drivestat
schema: $ENSTORE_DIR/databases/schemas/drivestat.schema

[New configuration for accounting and drivestat databases.
 The configuration parameters are in ENSTORE configuration file,
 available through config server]

[Create accounting db from scratch]

Similar to above procedure, except:
database server: *srv0
db_port: 8800
db_name: accounting
database_area: /diska/accounting-db
rc script: /etc/rc.d/init.d/accounting-db
schema: $ENSTORE_DIR/databases/schemas/accounting.schema

[Create drivestat db from scratch]

Similar to above procedure, except:
database server: *srv0
db_port: 8802
db_name: drivestat
database_area: /diska/drivestat-db
rc script: /etc/rc.d/init.d/drivestat-db
schema: $ENSTORE_DIR/databases/schemas/drivestat.schema


[Restore database from backup]

[h] restore enstoredb from backup
    Assuming database area is corrupted and journals are good
    [1] get last backup from *srv3
        the back is stored in *srv3:/diska/enstore-backup/dbase.*.*
        (assuming the backup is stkensrv3:/diska/enstore-backup/dbase.x.y)
        $ rcp "stkensrv3:/diska/enstore-backup/dbase.x.y/*" .
    [2] untar the journals
        $ gunzip -c file.tar.gz | tar xvf -
        $ gunzip -c volume.jou.tar.gz | tar xvf -
    [3] copy the current journals to here
        $ cp /diska/enstore-journal/* .
    [4] to be safe, you might want to save the current database
        as root:
        $ mv /diskb/enstore-db /diskb/enstore-db.saved
    [5] create a new database area as described above in [c], [d],
        and [f][1]-[2]. Do NOT restore the schema. Bring the database
        server up in local mode (no -i) to prevent other clients
        accessing database while is being rebuilt.
    [6] as user "products", restore the database from backup:
        $ ksu products
	$ pg_restore -d enstoredb -Fc enstoredb.dmp
	(this is going to take a while)
    [7] synchronize rebuilt database using journal files:
        $ ksu enstore
        $ $ENSTORE_DIR/src/restoredb.py
        alternatively, you may specify a directory where the journals
        are in $ENSOTRE_DIR/src/restoredb.py command line.
    [8] shut down database server, then bring it up with "-i" option to
        accept remote connections.

    Note, if the database backup is not the last version, you would
    need ALL journal files in all backups taken after that.
    To be very paranoid, you may bring everything in
    *srv3:/diska/enstore-backup/JOURNALS over.

[h2] restore accountingdb and drivestat from backup
    accounting db and drivestat db do not have journal files.
    the backups are at
    *srv3:/diska/pgdb-backup/*.backup
    they are database dumps.
    to restore, create a new database area and use pg_restore to
    restore the databases

[Tools]

The following tools are designed to handle the simple cases.
If they run into complication, an expert needs to be consulted.

They should run as user "root"

[i] create a brand new enstoredb from scratch

    $ENSTORE_DIR/databases/scripts/create_new_enstoredb

    It does the following:
    1. rename current database area, if exists
    2. create directory for database area
    3. initialize database area
    4. copy appropriate pg_hba.conf from $ENSTORE_DIR/databases/conftol_files
    5. start up databse server
    6. create user "enstore"
    7. create database owned by user "enstore"
    8. create schema from $ENSTORE_DIR/databases/schemas/enstoredb.schema
 
    it covers [c]-[f] in above
    it does not restore any data
    it does not install startup scripts in /etc/rc.d/*

[j] restore enstoredb from last backup

    $ENSTORE_DIR/databases/scripts/restore_enstoredb

    It does the following:
    1. create /diskb/DBRESTORE directory
       if there was one, the old one will be renamed first
    2. bring all files from *srv3:/diska/enstore-backup/dbase.x.y/*
       to /diskb/DBRESTORE
    3. untar journal files
    4. copy current journal files from /diska/enstore-journal to
       /diskb/DBRESTORE
    5. create an empty enstoredb using [i]
    6. restore enstoredb using backup
    7. synchronize rebuilt database using journal files
    8. shutdown the database server

[k] create a brand new accounting db from scratch

    $ENSTORE_DIR/databases/scripts/create_new_accountingdb

    similar to [i] except it creates accounting db

[l] create a brand new drivestat db from scratch

    $ENSTORE_DIR/databases/scripts/create_new_drivestatdb

    similar to [i] and [k] except it creates drivestat db

[m] get current schema from running database

    $ENSTORE_DIR/databases/scripts/get_schema enstoredb|accounting|drivestat

    dump database schemas from running stken* databases.
