-
Notifications
You must be signed in to change notification settings - Fork 794
/
Copy pathmysqlbackup.sh.erb
executable file
·125 lines (109 loc) · 3.67 KB
/
mysqlbackup.sh.erb
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
<%- if @kernel == 'Linux' -%>
#!/bin/bash
<%- else -%>
#!/bin/sh
<%- end -%>
#
# MySQL Backup Script
# Dumps mysql databases to a file for another backup tool to pick up.
#
# MySQL code:
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
# IDENTIFIED BY 'password';
# FLUSH PRIVILEGES;
#
##### START CONFIG ###################################################
USER=<%= @backupuser %>
PASS='<%= @backuppassword %>'
MAX_ALLOWED_PACKET=<%= @maxallowedpacket %>
DIR=<%= @backupdir %>
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
# Create temporary mysql cnf file.
TMPFILE=`mktemp /tmp/backup.XXXXXX` || exit 1
<%- if @kernel == 'SunOS' -%>
echo "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE
<%- else -%>
echo -e "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE
<%- end -%>
<% if @prescript -%>
<%- [@prescript].flatten.compact.each do |script|%>
<%= script %>
<%- end -%>
<% end -%>
# Ensure backup directory exist.
mkdir -p $DIR
PREFIX=mysql_backup_
<% if @ignore_events %>
ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
<% else %>
ADDITIONAL_OPTIONS="--events"
<% end %>
<%# Only include routines or triggers if we're doing a file per database -%>
<%# backup. This happens if we named databases, or if we explicitly set -%>
<%# file per database mode -%>
<% if [email protected]? || @file_per_database -%>
<% if @include_triggers -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --triggers"
<% else -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-triggers"
<% end -%>
<% if @include_routines -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --routines"
<% else -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-routines"
<% end -%>
<% end -%>
<%- if @optional_args and @optional_args.is_a?(Array) -%>
<%- @optional_args.each do |arg| -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS <%= arg %>"
<%- end -%>
<%- end -%>
##### STOP CONFIG ####################################################
PATH=<%= @execpath %>
<%- if @kernel == 'Linux' -%>
set -o pipefail
<%- end -%>
cleanup()
{
<%- if @kernel == 'SunOS' -%>
gfind "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | gxargs -0 -r rm -f
<%- else -%>
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
<%- end -%>
}
<% if @delete_before_dump -%>
cleanup
<% end -%>
<% if @backupdatabases.empty? -%>
<% if @file_per_database -%>
mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | while read dbname
do
<%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
done
<% else -%>
<%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<% end -%>
<% else -%>
<% @backupdatabases.each do |db| -%>
<%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<% end -%>
<% end -%>
<% unless @delete_before_dump -%>
if [ $? -eq 0 ] ; then
cleanup
touch <%= @backup_success_file_path %>
fi
<% end -%>
<% if @postscript -%>
<%- [@postscript].flatten.compact.each do |script|%>
<%= script %>
<%- end -%>
<% end -%>
# Remove temporary file
rm -f $TMPFILE