21289694217f3cac2786177e524bf208fbfbe1e0
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_backup_remote.sh
1 #!/bin/bash 
2 ###
3 # ============LICENSE_START=======================================================
4 # ONAP Policy Engine
5 # ================================================================================
6 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #      http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20
21 ###
22 #
23 # db_backup_remote.sh: Perform database backup from remote 
24
25 # Usage  : db_backup_remote.sh db_user     db_user_password database  db_hostname
26 # Example: db_backup_remote.sh policy_user password         onap_sdk localhost.com
27 #
28 # Note: 1. mysqldump utility must be available in the env where this script intend to run
29 #       2. db_user requires at least the SELECT privilege for dumped tables
30 #
31 #
32
33 . $HOME/.profile
34
35 DB_USER=""
36 DB_PASSWORD=""
37 DATABASE=""
38 DB_HOSTNAME=""
39 DATE=`date +"%Y%m%d"`
40 DATETIME=`date +"%Y%m%d%H%M%S"`
41 DAILY_BACKUP_DIR=""
42 LOG=""
43 ERR=""
44
45 function create_backup_dir
46 {
47   if [ ! -d $DAILY_BACKUP_DIR ]; then 
48     echo "Create DAILY_BACKUP_DIR [$DAILY_BACKUP_DIR] ..." 
49     mkdir -p $DAILY_BACKUP_DIR 2>&1 
50   fi
51 }
52
53 function backup_database
54 {
55   echo "backup database [$DATABASE]@[${DB_HOSTNAME}] started ...@`date`" | tee -a $LOG
56
57   BACKUP_FILE=$DAILY_BACKUP_DIR/backup_${DATABASE}_${DB_HOSTNAME}_${DATETIME}.sql
58   #echo $BACKUP_FILE
59   mysqldump --user=${DB_USER} --password=${DB_PASSWORD} --databases ${DATABASE} -h ${DB_HOSTNAME} > $BACKUP_FILE 
60   echo "" | tee -a $LOG
61   echo "database backup file --> $BACKUP_FILE" | tee -a $LOG
62   echo "" | tee -a $LOG
63   echo "backup database [$DATABASE]@[${DB_HOSTNAME}] completed ...@`date`" | tee -a $LOG
64 }
65
66
67 # MAIN
68 if [ $# -eq 4 ]; then 
69   DB_USER="${1}"
70   DB_PASSWORD="${2}"
71   DATABASE="${3}"
72   DB_HOSTNAME="${4}"
73   echo "db_backup_remote.sh for [$DATABASE]@[${DB_HOSTNAME}] started ... `date`" | tee -a $LOG
74   echo "DB_USER    : $DB_USER"     | tee -a $LOG
75   echo "DATABASE   : $DATABASE"    | tee -a $LOG
76   echo "DB_HOSTNAME: $DB_HOSTNAME" | tee -a $LOG
77     
78   DAILY_BACKUP_DIR=$POLICY_HOME/data/mysql/$DATE
79   LOG=$POLICY_HOME/logs/db_backup_remote_$DATE.log
80   ERR=$POLICY_HOME/logs/db_backup_remote_$DATE.err
81   create_backup_dir 
82
83   backup_database
84   echo "db_backup_remote.sh for [$DATABASE]@[${DB_HOSTNAME}] completed ... `date`" | tee -a $LOG
85 else
86   echo "Usage  : db_backup_remote.sh db_user_id  db_user_password database  db_hostname" 
87   echo "Example: db_backup_remote.sh policy_user password         onap_sdk localhost.com" 
88 fi