-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2-database-backup.bash
135 lines (110 loc) · 3.84 KB
/
q2-database-backup.bash
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
source utils.bash
USAGE="
./q2-database-backup.bash dbname [--backup] | [--max MAX_NUMBER_OF_BACKUPS]
"
# ARGS
AUTOMATIC_BACKUP=false
BACKUP_DIR=""
MAX_NUMBER_OF_BACKUPS=0
# VARS LOCAL TO THIS SCRIPT
DATABASE_FILENAME=""
function manually_backup_database() {
read -r -p "Enter the backup name (with file extension .zip or .tar.gz): " BACKUP_FILENAME
if [[ "$BACKUP_FILENAME" =~ .*\.zip ]]; then
echo "Using zip for backup (file has .zip extension)..."
zip "$BACKUP_FILENAME" "$DATABASE_FILENAME"
elif [[ "$BACKUP_FILENAME" =~ .*\.tar.gz ]]; then
echo "Using gz for backup (file has .gz extension)..."
tar -czf "$BACKUP_FILENAME" "$DATABASE_FILENAME"
fi
echo "Backup done ($BACKUP_FILENAME)"
}
function automatically_backup_database() {
BACKUPS_COUNT=$(ls /opt/backups | wc -l)
if [ "$BACKUPS_COUNT" -ge "$MAX_NUMBER_OF_BACKUPS" ]
then
# Remove file with oldest modified date
rm /opt/backups/"$(ls /opt/backups -t | tail -1)"
fi
# https://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script
DATE=$(date '+%Y-%m-%d_%H:%M:%S')
sudo zip "$BACKUP_DIR/${DATE}_${DATABASE_FILENAME}.zip" "$DATABASE_FILENAME"
}
function restore_database_from_path() {
read -r -p "Enter the backup name (with file extension .zip or .tar.gz): " FILEPATH
# The target directory after uncompressing the file will be the current working directory (i.e. '.')
# Therefore, the `unzip` or `tar` commands will prompt you if you want to replace the db file after uncompressing
if [[ "$FILEPATH" =~ .*\.zip ]]; then
unzip "$FILEPATH"
elif [[ "$FILEPATH" =~ .*\.tar\.gz ]]; then
tar -zxf "$FILEPATH"
fi
}
function enable_automatic_backups() {
read -r -p "Please enter your backup schedule (daily, weekly or monthly): " BACKUP_SCHEDULE
read -r -p "Max number of backup files to keep: " MAX_NUMBER_OF_BACKUPS
CRONJOB_SCHEDULE=""
if [[ "$BACKUP_SCHEDULE" == "daily" ]]; then CRONJOB_SCHEDULE="0 0 * * *"
elif [[ "$BACKUP_SCHEDULE" == "weekly" ]]; then CRONJOB_SCHEDULE="0 0 * * 0"
elif [[ "$BACKUP_SCHEDULE" == "monthly" ]]; then CRONJOB_SCHEDULE="0 0 1 * *"
fi
SCRIPT_PATH=$(__get_script_fullpath)
HARDCODED_DB_PATH=$(__get_file_fullpath "$DATABASE_FILENAME")
CRONJOB="$CRONJOB_SCHEDULE $SCRIPT_PATH $HARDCODED_DB_PATH --backup /opt/backups --max $MAX_NUMBER_OF_BACKUPS"
__register_cronjob "$CRONJOB"
}
function parse_commandline_arguments() {
while [ $# -gt 0 ]; do
case "$1" in
*db)
DATABASE_FILENAME=$1
shift
;;
--backup)
AUTOMATIC_BACKUP=true
BACKUP_DIR="$2"
shift 2
;;
--max)
MAX_NUMBER_OF_BACKUPS=$2
shift 2 # Because we will read '--max' and the argument directly after it, which is the actual number ($2)
;;
*)
echo "Invalid argument: $1"
exit
;;
esac
done
if [ -z "$DATABASE_FILENAME" ]
then echo "Database name is required."
exit
fi
}
function main() {
SCRIPT_PROMPT="
Q2 backup / restore ($DATABASE_FILENAME):
1) Backup the database
2) Restore a database
3) Enable automatic backups
4) Exit
Please select one of the options above: "
if [ $AUTOMATIC_BACKUP = true ]; then automatically_backup_database
else
while true; do
read -r -p "$SCRIPT_PROMPT" OPTION
case $OPTION in
1) manually_backup_database ;;
2) restore_database_from_path ;;
3) enable_automatic_backups ;;
4)
echo "Exiting program."
exit 0
;;
*) echo "Unknown key." ;;
esac
done
fi
}
parse_commandline_arguments "$@"
main "$@"