2015-03-30 21:56:31 +02:00
|
|
|
<%- if @kernel == 'Linux' -%>
|
2013-04-18 08:51:50 +02:00
|
|
|
#!/bin/bash
|
2015-03-30 21:56:31 +02:00
|
|
|
<%- else -%>
|
|
|
|
#!/bin/sh
|
|
|
|
<%- end -%>
|
2012-04-24 07:53:59 +02:00
|
|
|
#
|
|
|
|
# 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 ###################################################
|
|
|
|
|
2013-05-24 16:01:52 +02:00
|
|
|
USER=<%= @backupuser %>
|
2014-04-01 10:30:45 +02:00
|
|
|
PASS='<%= @backuppassword %>'
|
2016-06-14 14:49:50 +02:00
|
|
|
MAX_ALLOWED_PACKET=<%= @maxallowedpacket %>
|
2013-05-24 16:01:52 +02:00
|
|
|
DIR=<%= @backupdir %>
|
2013-04-18 08:51:50 +02:00
|
|
|
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
|
|
|
|
|
2015-11-13 16:41:56 +01:00
|
|
|
# Create temporary mysql cnf file.
|
|
|
|
TMPFILE=`mktemp /tmp/backup.XXXXXX` || exit 1
|
2016-06-14 14:49:50 +02:00
|
|
|
echo -e "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE
|
2015-11-06 19:28:56 +01:00
|
|
|
|
2016-03-14 16:44:43 +01:00
|
|
|
# Ensure backup directory exist.
|
|
|
|
mkdir -p $DIR
|
|
|
|
|
2013-04-18 08:51:50 +02:00
|
|
|
PREFIX=mysql_backup_
|
2014-01-29 13:22:12 +01:00
|
|
|
<% if @ignore_events %>
|
2015-04-29 15:52:37 +02:00
|
|
|
ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
|
2014-01-29 13:22:12 +01:00
|
|
|
<% else %>
|
2015-04-29 15:52:37 +02:00
|
|
|
ADDITIONAL_OPTIONS="--events"
|
2014-01-29 13:22:12 +01:00
|
|
|
<% end %>
|
2015-04-29 15:52:37 +02:00
|
|
|
<%# 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 !@backupdatabases.empty? || @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 -%>
|
2012-04-24 07:53:59 +02:00
|
|
|
|
|
|
|
##### STOP CONFIG ####################################################
|
2014-02-20 17:17:41 +01:00
|
|
|
PATH=<%= @execpath %>
|
2012-04-24 07:53:59 +02:00
|
|
|
|
2014-01-29 13:22:12 +01:00
|
|
|
|
|
|
|
|
2015-03-30 21:56:31 +02:00
|
|
|
<%- if @kernel == 'Linux' -%>
|
2013-04-18 08:51:50 +02:00
|
|
|
set -o pipefail
|
2015-03-30 21:56:31 +02:00
|
|
|
<%- end -%>
|
2013-04-18 08:51:50 +02:00
|
|
|
|
2013-07-03 14:31:31 +02:00
|
|
|
cleanup()
|
|
|
|
{
|
|
|
|
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
|
|
|
|
}
|
|
|
|
|
|
|
|
<% if @delete_before_dump -%>
|
|
|
|
cleanup
|
|
|
|
|
|
|
|
<% end -%>
|
2013-08-15 15:04:14 +02:00
|
|
|
<% if @backupdatabases.empty? -%>
|
2013-08-27 22:15:29 +02:00
|
|
|
<% if @file_per_database -%>
|
2016-05-20 10:11:25 +02:00
|
|
|
mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | while read dbname
|
2013-08-27 22:15:29 +02:00
|
|
|
do
|
2016-03-17 17:32:39 +01:00
|
|
|
mysqldump --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
|
2015-04-29 15:52:37 +02:00
|
|
|
${ADDITIONAL_OPTIONS} \
|
2013-08-27 22:15:29 +02:00
|
|
|
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
|
|
|
|
done
|
|
|
|
<% else -%>
|
2016-03-17 17:32:39 +01:00
|
|
|
mysqldump --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
|
2015-04-29 15:52:37 +02:00
|
|
|
${ADDITIONAL_OPTIONS} \
|
2013-08-15 15:04:14 +02:00
|
|
|
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
|
2013-08-27 22:15:29 +02:00
|
|
|
<% end -%>
|
2013-08-15 15:04:14 +02:00
|
|
|
<% else -%>
|
2013-08-13 23:44:41 +02:00
|
|
|
<% @backupdatabases.each do |db| -%>
|
2016-03-17 17:32:39 +01:00
|
|
|
mysqldump --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
|
2015-04-29 15:52:37 +02:00
|
|
|
${ADDITIONAL_OPTIONS} \
|
2013-08-13 23:44:41 +02:00
|
|
|
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
|
|
|
|
<% end -%>
|
|
|
|
<% end -%>
|
2013-04-18 08:51:50 +02:00
|
|
|
|
2013-07-10 05:12:38 +02:00
|
|
|
<% unless @delete_before_dump -%>
|
2013-04-18 08:51:50 +02:00
|
|
|
if [ $? -eq 0 ] ; then
|
2013-07-03 14:31:31 +02:00
|
|
|
cleanup
|
2013-04-18 08:51:50 +02:00
|
|
|
fi
|
2013-07-03 14:31:31 +02:00
|
|
|
<% end -%>
|
2012-04-24 07:53:59 +02:00
|
|
|
|
2014-02-06 16:44:07 +01:00
|
|
|
<% if @postscript -%>
|
|
|
|
<%- [@postscript].flatten.compact.each do |script|%>
|
|
|
|
<%= script %>
|
|
|
|
<%- end -%>
|
|
|
|
<% end -%>
|
2015-11-06 19:28:56 +01:00
|
|
|
|
2015-11-13 16:41:56 +01:00
|
|
|
# Remove temporary file
|
|
|
|
rm -f $TMPFILE
|