├── LICENSE ├── README.md ├── automysqlbackup_hourly └── test ├── Dockerfile ├── debian.cnf ├── docker-compose.yml └── test.sh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 linksix 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | automysqlbackup_hourly 2 | ====================== 3 | 4 | This script is an extension of [automysqlbackup](https://sourceforge.net/projects/automysqlbackup/), 5 | to support hourly backups of MySQL databases. 6 | 7 | Compatibility 8 | ------------- 9 | 10 | Debian and Ubuntu only. Tested with Debian 10 (Buster) and Ubuntu 20.04 (Focal). 11 | 12 | Installation 13 | ------------ 14 | 15 | First, automysqlbackup *must* be installed : 16 |
sudo apt-get install automysqlbackup
17 | The automysqlbackup_hourly script can be copied or linked into /etc/cron.hourly/ :
18 | sudo ln -s automysqlbackup_hourly /etc/cron.hourly/
19 | For testing it, just launch as root :
20 | ./automysqlbackup_hourly
21 | Then, in your backup's directory, you will see :
22 | hourly daily weekly monthly
23 |
24 | Every hour, you'll have a new dump for each database in hourly directory.
25 |
26 | How it works
27 | ------------
28 |
29 | Every hour, it takes the last dump present in your daily or weekly directory
30 | (depending on the day given by DOWEEKLY parameter), and copy it to the
31 | hourly/yourdb directory. Then, it launches the automysqlbackup script to
32 | create a new dump in daily or weekly directory.
33 | Finally, dumps older than 7 days in hourly directory are deleted. You can
34 | change the number of days by changing ROTATION_DAYS variable, at the beginning
35 | of the script.
36 |
--------------------------------------------------------------------------------
/automysqlbackup_hourly:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #
3 | # automysqlbackup_hourly
4 | # ======================
5 | #
6 | # This script is an extension of [automysqlbackup](https://sourceforge.net/projects/automysqlbackup/),
7 | # to support hourly backups of your MySQL databases.
8 | #
9 | # Compatibility
10 | # -------------
11 | #
12 | # Debian and Ubuntu only. Tested on production servers with Debian 8 (Jessie) and Ubuntu 14.04.
13 | #
14 | # Installation
15 | # ------------
16 | #
17 | # The automysqlbackup_hourly script should be copied into /etc/cron.hourly/, and
18 | # for ensure that is executable :
19 | # chmod +x /etc/cron.hourly/automysqlbackup_hourly
20 | # automysqlbackup *must* be installed.
21 | # For testing it, just launch as root :
22 | # ./automysqlbackup_hourly
23 | # Then, in your backup's directory, you'll see :
24 | # hourly daily weekly monthly
25 | #
26 | # Every hour, you'll have a new dump for each database in hourly directory.
27 | #
28 | # How it works
29 | # ------------
30 | #
31 | # Every hour, it takes the last dump present in your daily or weekly directory
32 | # (depending on the day given by DOWEEKLY parameter), and copy it to the
33 | # hourly/yourdb directory. Then, it launches the automysqlbackup script to
34 | # create a new dump in daily or weekly directory.
35 | # Finally, dumps older than 7 days in hourly directory are deleted. You can
36 | # change the number of days by changing ROTATION_DAYS variable, at the beginning
37 | # of the script.
38 | #
39 |
40 | ##
41 | # Specific configuration variables. You can change it as you like.
42 | ##
43 |
44 | # automysqlbackup executable path
45 | EXEC_PATH=/usr/sbin/automysqlbackup
46 |
47 | # automysqlbackup default config path
48 | CONFIG_FILE=/etc/default/automysqlbackup
49 |
50 | # Number of days for hourly dumps rotation
51 | ROTATION_DAYS=7
52 |
53 | ##
54 | # End of configuration variables.
55 | ##
56 |
57 | # automysqlbackup install check
58 | if [ ! -e ${EXEC_PATH} ];then
59 | echo "[ERROR] automysqlbackup must be installed."
60 | echo "To do this in Debian and Ubuntu : sudo apt-get install automysqlbackup"
61 | exit 1
62 | fi
63 |
64 | # Config file check
65 | if [ ! -f $CONFIG_FILE ]; then
66 | echo "[ERROR] The default configuration file, $CONFIG_FILE, doesnt exists."
67 | fi
68 |
69 | # automysqlbackup configuration import
70 | source $CONFIG_FILE
71 | # Day number of the week 1 to 7 where 1 represents Monday
72 | DNOW=`date +%u`
73 | # The current backup day is daily, or weekly ?
74 | if [ $DNOW -eq $DOWEEKLY ];then
75 | CURRENT_BACKUPDIR="${BACKUPDIR}/weekly"
76 | else
77 | CURRENT_BACKUPDIR="${BACKUPDIR}/daily"
78 | fi
79 |
80 | # Hourly directory's check
81 | if [ ! -e "${BACKUPDIR}/hourly" ]; then
82 | mkdir -p "${BACKUPDIR}/hourly"
83 | fi
84 |
85 | # Hourly backup for each DB
86 | for DB in $DBNAMES
87 | do
88 | # Prepare $DB for using
89 | DB="`echo $DB | sed 's/%/ /g'`"
90 | DB_HOURLY_DIR="${BACKUPDIR}/hourly/${DB}"
91 | DB_CURRENT_DIR="${CURRENT_BACKUPDIR}/${DB}"
92 |
93 | # Create separate directory in "hourly" for each DB
94 | if [ ! -e $DB_HOURLY_DIR ];then
95 | mkdir -p $DB_HOURLY_DIR
96 | fi
97 |
98 | # If we have a dump created for this DB the previous hour, then we copy it
99 | # in the current DB directory. After, automysqlbackup will create the dump
100 | # for this hour itself.
101 | for DBDUMP in `ls "${DB_CURRENT_DIR}"`
102 | do
103 | DBDUMP="${DB_CURRENT_DIR}/${DBDUMP}"
104 | DUMP_DATE=`stat -c %y "${DBDUMP}"`
105 | DUMP_TIMESTAMP=`date -d "${DUMP_DATE}" +%s`
106 | CURRENT_TIMESTAMP=`date +%s`
107 | TIMESTAMPS_DIFF=`expr ${CURRENT_TIMESTAMP} - ${DUMP_TIMESTAMP}`
108 | if [ $TIMESTAMPS_DIFF -gt 1 ] && [ $TIMESTAMPS_DIFF -lt 4000 ];then
109 | cp ${DBDUMP} ${DB_HOURLY_DIR}
110 | fi
111 | done
112 | done
113 |
114 | # For dumps rotation, we delete the ones older than $ROTATION_DAYS days
115 | find "${BACKUPDIR}/hourly" -mtime +${ROTATION_DAYS} -type f -delete
116 |
117 | # Finally, we call automysqlbackup to create the dump of this last hour
118 | # in the "daily" or "weekly" directory.
119 | ${EXEC_PATH}
120 |
--------------------------------------------------------------------------------
/test/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM ubuntu:20.04
2 |
3 | RUN apt update && \
4 | apt install -y automysqlbackup && \
5 | rm -rf /var/apt/lists/*
6 | COPY debian.cnf /etc/mysql/debian.cnf
7 |
8 | ENTRYPOINT ["tail", "-f", "/dev/null"]
9 |
--------------------------------------------------------------------------------
/test/debian.cnf:
--------------------------------------------------------------------------------
1 | [client]
2 | host = db
3 | user = backup_test
4 | password = backup_test
5 |
--------------------------------------------------------------------------------
/test/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3"
2 |
3 | services:
4 | backup:
5 | build: .
6 | volumes:
7 | - ../automysqlbackup_hourly:/root/automysqlbackup_hourly
8 | db:
9 | image: mariadb:10.5
10 | environment:
11 | MYSQL_RANDOM_ROOT_PASSWORD: "yes"
12 | MYSQL_DATABASE: backup_test
13 | MYSQL_USER: backup_test
14 | MYSQL_PASSWORD: backup_test
15 |
--------------------------------------------------------------------------------
/test/test.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | docker-compose exec backup /root/automysqlbackup_hourly
4 |
--------------------------------------------------------------------------------