forked from selim13/docker-automysqlbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomysqlbackup_hourly
118 lines (104 loc) · 3.7 KB
/
automysqlbackup_hourly
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
#!/bin/bash
# https://github.com/dm0reau/automysqlbackup_hourly
# automysqlbackup_hourly
# ======================
#
# This script is an extension of [automysqlbackup](https://sourceforge.net/projects/automysqlbackup/),
# to support hourly backups of your MySQL databases.
#
# Compatibility
# -------------
#
# Debian and Ubuntu only. Tested on production servers with Debian 8 (Jessie) and Ubuntu 14.04.
#
# Installation
# ------------
#
# The automysqlbackup_hourly script should be copied into /etc/cron.hourly/, and
# for ensure that is executable :
# <pre><code>chmod +x /etc/cron.hourly/automysqlbackup_hourly</code></pre>
# automysqlbackup *must* be installed.
# For testing it, just launch as root :
# <pre><code>./automysqlbackup_hourly</code></pre>
# Then, in your backup's directory, you'll see :
# <pre><code>hourly daily weekly monthly</code></pre>
#
# Every hour, you'll have a new dump for each database in hourly directory.
#
# How it works
# ------------
#
# Every hour, it takes the last dump present in your daily or weekly directory
# (depending on the day given by DOWEEKLY parameter), and copy it to the
# hourly/yourdb directory. Then, it launches the automysqlbackup script to
# create a new dump in daily or weekly directory.
# Finally, dumps older than 7 days in hourly directory are deleted. You can
# change the number of days by changing ROTATION_HOURLY_DAYS variable, at the beginning
# of the script.
#
##
# Specific configuration variables. You can change it as you like.
##
# automysqlbackup executable path
EXEC_PATH=/usr/local/bin/automysqlbackup
# automysqlbackup default config path
CONFIG_FILE=/etc/automysqlbackup/automysqlbackup.conf
# Number of days for hourly dumps rotation
ROTATION_HOURLY_DAYS=${ROTATION_HOURLY_DAYS}
##
# End of configuration variables.
##
# automysqlbackup install check
if [ ! -e ${EXEC_PATH} ];then
echo "[ERROR] automysqlbackup must be installed."
echo "To do this in Debian and Ubuntu : sudo apt-get install automysqlbackup"
exit 1
fi
# Config file check
if [ ! -f $CONFIG_FILE ]; then
echo "[ERROR] The default configuration file, $CONFIG_FILE, doesnt exists."
fi
# automysqlbackup configuration import
source $CONFIG_FILE
# Day number of the week 1 to 7 where 1 represents Monday
DNOW=`date +%u`
# The current backup day is daily, or weekly ?
if [ $DNOW -eq $DOWEEKLY ];then
CURRENT_BACKUPDIR="${BACKUPDIR}/weekly"
else
CURRENT_BACKUPDIR="${BACKUPDIR}/daily"
fi
# Hourly directory's check
if [ ! -e "${BACKUPDIR}/hourly" ]; then
mkdir -p "${BACKUPDIR}/hourly"
fi
# Hourly backup for each DB
for DB in $DBNAMES
do
# Prepare $DB for using
DB="`echo $DB | sed 's/%/ /g'`"
DB_HOURLY_DIR="${BACKUPDIR}/hourly/${DB}"
DB_CURRENT_DIR="${CURRENT_BACKUPDIR}/${DB}"
# Create separate directory in "hourly" for each DB
if [ ! -e $DB_HOURLY_DIR ];then
mkdir -p $DB_HOURLY_DIR
fi
# If we have a dump created for this DB the previous hour, then we copy it
# in the current DB directory. After, automysqlbackup will create the dump
# for this hour itself.
for DBDUMP in `ls "${DB_CURRENT_DIR}"`
do
DBDUMP="${DB_CURRENT_DIR}/${DBDUMP}"
DUMP_TIMESTAMP=`stat -c %Y "${DBDUMP}"`
CURRENT_TIMESTAMP=`date +%s`
TIMESTAMPS_DIFF=`expr ${CURRENT_TIMESTAMP} - ${DUMP_TIMESTAMP}`
if [[ $TIMESTAMPS_DIFF -gt 1 ]] && [[ $TIMESTAMPS_DIFF -lt 4000 ]];then
cp ${DBDUMP} ${DB_HOURLY_DIR}
fi
done
done
# For dumps rotation, we delete the ones older than $ROTATION_HOURLY_DAYS days
find "${BACKUPDIR}/hourly" -mtime +${ROTATION_HOURLY_DAYS} -type f -delete
# Finally, we call automysqlbackup to create the dump of this last hour
# in the "daily" or "weekly" directory.
${EXEC_PATH}