28
|
1 |
<%- if @kernel == 'Linux' -%>
|
|
2 |
#!/bin/bash
|
|
3 |
<%- else -%>
|
|
4 |
#!/bin/sh
|
|
5 |
<%- end -%>
|
|
6 |
#
|
|
7 |
# MySQL Backup Script
|
|
8 |
# Dumps mysql databases to a file for another backup tool to pick up.
|
|
9 |
#
|
|
10 |
# MySQL code:
|
|
11 |
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
|
|
12 |
# IDENTIFIED BY 'password';
|
|
13 |
# FLUSH PRIVILEGES;
|
|
14 |
#
|
|
15 |
##### START CONFIG ###################################################
|
|
16 |
|
|
17 |
USER=<%= @backupuser %>
|
|
18 |
PASS='<%= @backuppassword %>'
|
|
19 |
DIR=<%= @backupdir %>
|
|
20 |
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
|
|
21 |
|
|
22 |
PREFIX=mysql_backup_
|
|
23 |
<% if @ignore_events %>
|
|
24 |
ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
|
|
25 |
<% else %>
|
|
26 |
ADDITIONAL_OPTIONS="--events"
|
|
27 |
<% end %>
|
|
28 |
<%# Only include routines or triggers if we're doing a file per database -%>
|
|
29 |
<%# backup. This happens if we named databases, or if we explicitly set -%>
|
|
30 |
<%# file per database mode -%>
|
|
31 |
<% if !@backupdatabases.empty? || @file_per_database -%>
|
|
32 |
<% if @include_triggers -%>
|
|
33 |
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --triggers"
|
|
34 |
<% else -%>
|
|
35 |
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-triggers"
|
|
36 |
<% end -%>
|
|
37 |
<% if @include_routines -%>
|
|
38 |
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --routines"
|
|
39 |
<% else -%>
|
|
40 |
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-routines"
|
|
41 |
<% end -%>
|
|
42 |
<% end -%>
|
|
43 |
|
|
44 |
##### STOP CONFIG ####################################################
|
|
45 |
PATH=<%= @execpath %>
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
<%- if @kernel == 'Linux' -%>
|
|
50 |
set -o pipefail
|
|
51 |
<%- end -%>
|
|
52 |
|
|
53 |
cleanup()
|
|
54 |
{
|
|
55 |
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
|
|
56 |
}
|
|
57 |
|
|
58 |
<% if @delete_before_dump -%>
|
|
59 |
cleanup
|
|
60 |
|
|
61 |
<% end -%>
|
|
62 |
<% if @backupdatabases.empty? -%>
|
|
63 |
<% if @file_per_database -%>
|
|
64 |
mysql -u${USER} -p${PASS} -s -r -N -e 'SHOW DATABASES' | while read dbname
|
|
65 |
do
|
|
66 |
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
|
|
67 |
${ADDITIONAL_OPTIONS} \
|
|
68 |
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
|
|
69 |
done
|
|
70 |
<% else -%>
|
|
71 |
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
|
|
72 |
${ADDITIONAL_OPTIONS} \
|
|
73 |
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
|
|
74 |
<% end -%>
|
|
75 |
<% else -%>
|
|
76 |
<% @backupdatabases.each do |db| -%>
|
|
77 |
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
|
|
78 |
${ADDITIONAL_OPTIONS} \
|
|
79 |
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
|
|
80 |
<% end -%>
|
|
81 |
<% end -%>
|
|
82 |
|
|
83 |
<% unless @delete_before_dump -%>
|
|
84 |
if [ $? -eq 0 ] ; then
|
|
85 |
cleanup
|
|
86 |
fi
|
|
87 |
<% end -%>
|
|
88 |
|
|
89 |
<% if @postscript -%>
|
|
90 |
<%- [@postscript].flatten.compact.each do |script|%>
|
|
91 |
<%= script %>
|
|
92 |
<%- end -%>
|
|
93 |
<% end -%>
|