-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup_db.sh
executable file
·68 lines (59 loc) · 1.99 KB
/
backup_db.sh
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
#!/bin/bash
# Backup Database
#
# Backup the local database in to a compressed .sql.gz archive
#
# @author Connor Bär
# @copyright Copyright (c) 2017 Connor Bär
# @link https://madebyconnor.co/
# @package wordpress-scripts
# @since 1.0.0
# @license MIT
# Get the directory of the currently executing script
DIR="$(dirname "${BASH_SOURCE[0]}")"
# Include files
INCLUDE_FILES=(
"common/defaults.sh"
".env.sh"
"common/common_env.sh"
"common/common_db.sh"
)
for INCLUDE_FILE in "${INCLUDE_FILES[@]}"
do
if [ -f "${DIR}/${INCLUDE_FILE}" ]
then
source "${DIR}/${INCLUDE_FILE}"
else
echo "File ${DIR}/${INCLUDE_FILE} is missing, aborting."
exit 1
fi
done
# Set the backup db file name, parent directory path, and full path
BACKUP_DB_NAME="${LOCAL_DB_NAME}-db-backup-$(date '+%Y%m%d-%H%M%S').sql"
BACKUP_DB_DIR_PATH="${LOCAL_BACKUPS_PATH}${LOCAL_DB_NAME}/${DB_BACKUP_SUBDIR}/"
BACKUP_DB_PATH="${BACKUP_DB_DIR_PATH}${BACKUP_DB_NAME}"
# Make sure the directory exists
if [[ ! -d "${BACKUP_DB_DIR_PATH}" ]] ; then
echo "Creating backup directory ${BACKUP_DB_DIR_PATH}"
mkdir -p "${BACKUP_DB_DIR_PATH}"
fi
# Backup the local db
$LOCAL_MYSQLDUMP_CMD $LOCAL_DB_CREDS $MYSQLDUMP_SCHEMA_ARGS > "$BACKUP_DB_PATH"
$LOCAL_MYSQLDUMP_CMD $LOCAL_DB_CREDS $MYSQLDUMP_DATA_ARGS >> "$BACKUP_DB_PATH"
gzip -f "$BACKUP_DB_PATH"
echo "*** Backed up local database to ${BACKUP_DB_PATH}.gz"
# Remove backups older than LOCAL_BACKUPS_MAX_AGE
TMP_LOG_PATH="/tmp/${REMOTE_DB_NAME}-db-backups.log"
find "${BACKUP_DB_DIR_PATH}" -name "*.sql.gz" -mtime +${GLOBAL_DB_BACKUPS_MAX_AGE} -exec rm -fv "{}" \; &> $TMP_LOG_PATH
# Report on what we did
FILE_COUNT=`cat $TMP_LOG_PATH | wc -l`
if [ $FILE_COUNT == 1 ] ; then
PLURAL_CHAR=""
fi
DETAILS_MSG="; details logged to ${TMP_LOG_PATH}"
if [ $FILE_COUNT == 0 ] ; then
DETAILS_MSG=""
fi
echo "*** ${FILE_COUNT} old database backup${PLURAL_CHAR} removed${DETAILS_MSG}"
# Normal exit
exit 0