-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlbackup.sh
104 lines (90 loc) · 2.32 KB
/
mysqlbackup.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
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
#!/bin/bash
#dump a mysql mount the backup and umount the backup after
DIRNAME=$(dirname $0)
source $DIRNAME/common.sh
source $DIRNAME/mounting.sh
show_help(){
echo "Do not forget to configure your ~/.cnf with your access AND host for mysqldump"
echo "mysqlbackup.sh -u [UUID] "
echo "SAMPLE "
echo " bash mysqlbackup.sh -m \"//valinor.realise.ch/tech_backup -o credentials=/etc/cifsauth\" -p /mnt -f taxiblog -d taxiblog -z"
exit 1
}
UUID=""
MOUNT_PATH=""
MOUNT_SOURCE=""
DEST_FOLDER="mysqldump"
DATABASES=""
FILE_EXPIRATION=15
GZIP=false
while getopts "h?u:m:p:f:d:t:z" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
u) UUID=$OPTARG
;;
p) MOUNT_PATH=$OPTARG
;;
m) MOUNT_SOURCE=$OPTARG
;;
f) DEST_FOLDER=$OPTARG
;;
d) DATABASES=$OPTARG
;;
t) FILE_EXPIRATION=$OPTARG
;;
z) GZIP=true
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
TO_MOUNT=""
if [ ! -z "$UUID" ] ; then
TO_MOUNT=$UUID
MOUNT_DEVICE="/dev/disk/by-uuid/$UUID"
fi
if [ ! -z "$MOUNT_SOURCE" ] ; then
TO_MOUNT=$MOUNT_SOURCE
MOUNT_DEVICE=$MOUNT_SOURCE
fi
if [ -z "$TO_MOUNT" ] ; then
show_help
fi
if [ -z "$MOUNT_PATH" ]; then
#show_help
shutdown "you did not specify -p option. resulting in an empty mount path"
fi
if [ -z "$DATABASES" ]; then
#show_help
shutdown "you have to specify the databases to dump"
fi
if ! isMounted $MOUNT_PATH ; then
echo "mounting $MOUNT_DEVICE"
do_mount "$MOUNT_DEVICE" "$MOUNT_PATH"
log "mounted $MOUNT_DEVICE on $MOUNT_PATH ok"
echo "Launching mysqldump"
log "launching mysqldump"
## do the dump here
#escaping crap
DEST_FOLDER=$(printf '%q' "$DEST_FOLDER")
DESTINATION_FOLDER="$MOUNT_PATH/$DEST_FOLDER"
if [ ! -d "$DESTINATION_FOLDER" ]; then
log "creating destination folder $DESTINATION_FOLDER"
mkdir -p "$DESTINATION_FOLDER" || shutdown "cannot create destination_folder"
fi
DB_STRING=$(printf '%q' "$DATABASES")
D=$(date +%Y-%m-%d_%H-%M-%S)
DUMP_NAME=$(printf '%q_%q.sql' "$DB_STRING" "$D")
mysqldump "$DATABASES" > "$DESTINATION_FOLDER/$DUMP_NAME" || shutdown "failed to dump the database"
if [ $GZIP ]; then
gzip $DESTINATION_FOLDER/$DUMP_NAME
fi
log "finished dumping $DATABASES into $DESTINATION_FOLDER/$DUMP_NAME "
do_umount "$MOUNT_PATH"
log "umounted $MOUNT_DEVICE"
else
shutdown "$MOUNT_PATH is already mounted. skipping"
fi