forked from mediamicroservices/mm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbbackup
executable file
·59 lines (50 loc) · 2.17 KB
/
dbbackup
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
#!/usr/bin/env bash
config_file="$HOME/.$(basename "${0}").conf"
touch "$config_file"
. "$config_file"
usage(){
echo "This script will create backups of the selected database and retain the specified amount. Script can be run manually or set up as a recurring task."
echo "Usage: -e edit configurations, -c create login credentials for backup -h help"
echo "To configure run with the -e option. This will open the config via the nano editor. You will also need to configure a SQL login path. This can be generated by running dbbackup -c."
exit 0
}
_edit_config(){
{
echo "#Configuration Section"
echo "#Select directory for backup"
echo "SQL_BACKUP_DIR=\"$SQL_BACKUP_DIR\""
echo "#Select database for backup"
echo "DATABASE_NAME=\"$DATABASE_NAME\""
echo "#Enter SQL log in path for database. This can be created by running dbbackup -c or manually"
echo "#with the command mysql_config_editor set --login-path=[name your login path here] --host=[enter database host here] --user=[enter user name here] --password"
echo "SQL_LOGIN_PATH=\"$SQL_LOGIN_PATH\""
echo "#Number of backup versions to retain"
echo "COPIES_NUMBER=\"$COPIES_NUMBER\""
} > "${config_file}"
}
_create_login(){
echo "Creating login profile for DB backup. You will be prompted for your SQL root password."
mysql_config_editor set --login-path=mm_db_backup --host=localhost --user=root --password
SQL_LOGIN_PATH="mm_db_backup" || Echo "Could not make login path. Please make sure there are no problems with MySQL."
Echo "Login path created!"
_edit_config
exit 0
}
OPTIND=1
while getopts "hec" opt ; do
case "${opt}" in
h) usage ;;
e) _edit_config && nano "${config_file}" && exit 0 ;;
c) _create_login ;;
*)
esac
done
FILES=$(ls -rt "$SQL_BACKUP_DIR"/"$DATABASE_NAME"_backup_*.sql.gz)
FILE_COUNT=$(echo "$FILES" | wc -l)
OLDEST_FILE=$(echo "$FILES" | head -n 1)
BACKUP_FILE=${SQL_BACKUP_DIR}/"$DATABASE_NAME"_backup_`date +"%m-%d-%Y_%H-%M-%S"`.sql
if [ "$FILE_COUNT" -ge "$COPIES_NUMBER" ] ; then
rm "$OLDEST_FILE"
fi
mysqldump --login-path="$SQL_LOGIN_PATH" "$DATABASE_NAME" > "$BACKUP_FILE"
gzip "$BACKUP_FILE"