-
Notifications
You must be signed in to change notification settings - Fork 9
/
atlassian_db_backup
executable file
·119 lines (108 loc) · 3.51 KB
/
atlassian_db_backup
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
#!/bin/bash
# Creates a MySQL DB backup of the Confluence or JIRA MySQL database
# Get the database name from $APP's $DBXML_CFG file
# Get the mysql hostname, username, and password from the .my.cnf file
## NOTE: Due to a bug with the mysql and mysqldump commands, it is problematic to
## to specify the database name in the .my.cnf file - they share the option
## --database which means something different to each command.
## This is why we get the databse name from the $DBXML_CFG file.
# Must be root to run this script
if [ "`/usr/bin/id -urn`" != "root" ] ; then
echo -e "\nYou must be root to execute this script \n"
exit 1
fi
# Common directory between Confluence and JIRA
INSTALL_DIR="/usr/local/atlassian"
HOME_DIR="/var/atlassian/application-data"
USAGE() {
echo -e "\nUsage: `basename $0` [ -c | -j ] [ -s ]"
echo -e "-c --> Backup Confluence MySQL DB"
echo -e "-j --> Backup JIRA MySQL DB"
echo -e "-s --> Indicate this script should run in silent mode\n"
}
BACKUP_DIR=/atlassianbackup
SILENT=0
APP="null"
DB_NAME="null"
while getopts ":cjs" OPT ; do
case $OPT in
c)
APP="confluence"
DBXML_CFG="$HOME_DIR/confluence/confluence.cfg.xml"
[ -f $DBXML_CFG ] && DB_NAME=`grep url $DBXML_CFG | grep -o "conf[a-zA-Z]*[0-9]*"`
ENV_FILE="$INSTALL_DIR/confluence/bin/user.sh"
BACKUP_FILE=confluence_sqldump_`date "+%Y%m%d"`_$$.sql
PORT=8090
;;
j)
APP="jira"
DBXML_CFG="$HOME_DIR/jira/dbconfig.xml"
[ -f $DBXML_CFG ] && DB_NAME=`grep url $DBXML_CFG | grep -o "jira[a-zA-Z]*[0-9]*"`
ENV_FILE="$INSTALL_DIR/jira/bin/user.sh"
BACKUP_FILE=jira_sqldump_`date "+%Y%m%d"`_$$.sql
PORT=8080
;;
s)
SILENT=1
;;
\?)
echo -e "\nInvalid option: -$OPTARG" >&2
USAGE
exit 1
;;
:)
echo -e "\nOption -$OPTARG requires an argument" >&2
USAGE
exit 1
;;
esac
done
if [ "$APP" = "null" ] ; then
USAGE
exit 1
elif [ "$DB_NAME" = "null" ] ; then
echo -e "\n$DBXML_CFG does not exist - exiting \n"
exit 1
fi
# Perform checks to determine if this script can run on this server
if [ -s $ENV_FILE ] ; then
# If script was invoked with the -s flag - skip check if $APP is running
if [ $SILENT -eq 0 ] ; then
# Test to see if $APP is running
if [ "`netstat -nlt | grep $PORT`" != "" ] ; then
echo -e "\n$APP is running!"
echo -e "It is not required, but recommended, that you stop $APP before proceeding with a back-up."
echo -n "Do you want me to stop $APP ? (y/n): "
read ANSWER
case "$ANSWER" in
y|Y)
echo -e "\nStopping $APP now... \n"
/sbin/service $APP stop
;;
n|N)
echo -e "\nOK - moving on without stopping $APP \n"
;;
*)
echo -e "\nDid not understand your answer - exiting \n"
exit 1
;;
esac
fi
fi
else
echo -e "\nCould not locate a $APP installation on this system \n"
exit 1
fi
# See https://docs.library.ucla.edu/x/GoA4Bw for details regarding the mysqldump options
echo -e "Starting back-up of the $DB_NAME database"
echo -e "For a large database this can be a lenghtly process"
mysqldump $DB_NAME > $BACKUP_DIR/$BACKUP_FILE
if [ $? -ne 0 ] ; then
echo "mysqldump encountered an error and the back-up failed."
if [ -f $BACKUP_DIR/$BACKUP_FILE ]; then
rm -f $BACKUP_DIR/$BACKUP_FILE
fi
exit 1
fi
echo -e "\nBackup is complete and is available at $BACKUP_DIR/$BACKUP_FILE"
echo -e "If you stopped $APP remember to start it up again \n"