dca2017ff7f08737e1719b829aef7d3eaef6cd4a
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_backup.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.sh: Run this script to back database to a file
24
25 # Usage  : db_backup.sh db_user     db_user_password database
26 # Example: db_backup.sh policy_user password         onap_sdk
27 #
28 # Note: mysqldump requires at least the SELECT privilege for dumped tables
29 #
30 #
31
32 . $HOME/.profile
33
34 DB_USER=""
35 DB_PASSWORD=""
36 DATABASE=""
37 DATE=`date +"%Y%m%d"`
38 DATETIME=`date +"%Y%m%d%H%M%S"`
39 DAILY_BACKUP_DIR=""
40 LOG=""
41 ERR=""
42
43 function create_backup_dir
44 {
45   if [ ! -d $DAILY_BACKUP_DIR ]; then 
46     echo "Create DAILY_BACKUP_DIR [$DAILY_BACKUP_DIR] ..." 
47     mkdir -p $DAILY_BACKUP_DIR 2>&1 
48   fi
49 }
50
51 function backup_database
52 {
53   echo "backup_database [$DATABASE] started ...@`date`" | tee -a $LOG
54
55   BACKUP_FILE=$DAILY_BACKUP_DIR/backup_${DATABASE}_${DATETIME}.sql
56   echo $BACKUP_FILE
57   mysqldump --user=${DB_USER} --password=${DB_PASSWORD} --databases ${DATABASE} > $BACKUP_FILE 
58   echo "" | tee -a $LOG
59   echo "database backup file --> $BACKUP_FILE" | tee -a $LOG
60   echo "" | tee -a $LOG
61   echo "backup_database [$DATABASE] completed ...@`date`" | tee -a $LOG
62 }
63
64
65 # MAIN
66 echo "db_backup.sh started ... `date`" | tee -a $LOG
67 if [ $# -eq 3 ]; then 
68   DB_USER="${1}"
69   DB_PASSWORD="${2}"
70   DATABASE="${3}"
71   echo "DB_USER: $DB_USER" | tee -a $LOG
72     
73   DAILY_BACKUP_DIR=$POLICY_HOME/data/mysql/$DATE
74   LOG=$POLICY_HOME/logs/db_backup_$DATE.log
75   ERR=$POLICY_HOME/logs/db_backup_$DATE.err
76   create_backup_dir 
77
78   backup_database
79   echo "db_backup.sh completed ... `date`" | tee -a $LOG
80 else
81   echo "Usage  : db_backup.sh db_user_id  db_user_password database" 
82   echo "Example: db_backup.sh policy_user password         onap_sdk" 
83 fi
84