This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.sh
executable file
·119 lines (103 loc) · 2.34 KB
/
functions.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# Return the active LVM snapshot.
active() {
echo `readlink /var/lib/mysql`
}
# Return the device node that is mounted on a given path.
device() {
df "$1" | tail -1 | awk '{ print $1 }'
}
# Make sure we're running as root.
check_user() {
if [ $EUID -gt 0 ]
then
echo "Linux MySQL Manager must be run as root."
exit 1
fi
}
# Branch a new database from the master database.
branch() {
DEVICE=`device "$(active)"`
service mysql stop
lvcreate -s -n $1 "$DEVICE"
mkdir -p "$VG_PATH/$1"
fstab_add $1
mount -a
service mysql start
}
# Destroy a database snapshot.
delete() {
umount "$VG_PATH/$1"
rmdir "$VG_PATH/$1"
fstab_rm $1
lvremove -f "$VG/$1"
}
# Display the amount of free space in the thin pool.
free() {
PCT=`lvs mysql/thinpool -odata_percent --noheadings --rows | cut -c 3-`
echo ""
echo "$PCT% used by MySQL databases."
WARN=`echo "$PCT > 80" | bc`
if [ $WARN -eq 1 ]
then
echo ""
echo "WARNING: If all free space is used, the system may hang."
echo "Delete snapshots or run fstrim-all to free space."
echo ""
fi
}
fstab_definition() {
echo -e "/dev/$VG/$1\t/$VG/$1\text4\tdefaults\t0\t0" "# MySQL database added by LMM"
}
fstab_add() {
echo "$(fstab_definition $1)" >> /etc/fstab
}
fstab_rm() {
grep -v "$(fstab_definition $1)" /etc/fstab > /tmp/lmm_fstab
mv /tmp/lmm_fstab /etc/fstab
}
# Determine if a database snapshot exists.
snapshot_exists() {
if [[ ! -a "$1" ]]
then
echo "Snapshot $1 does not appear to exist."
exit 1
fi
}
# Merge a snapshot into the currently active snapshot.
merge() {
echo "Merging $1 into $(active)"
service mysql stop
rsync -a --progress --delete-after "$1/" $(active)
service mysql start
fstrim $(active)
}
# Determine if a database snapshot can be made.
snapshot_available() {
if [[ -a "$1" ]]
then
echo "Snapshot $1 already exists."
exit 1
fi
}
# Determine if a snapshot is active.
snapshot_active() {
if [[ "$(active)" == "$VG_PATH/$1" ]]
then
return 0
fi
return 1
}
# Change the currently active database.
checkout() {
echo `active` "is the currently active database."
service mysql stop
echo "Setting $VG_PATH/$1 as the active database."
rm /var/lib/mysql
ln -s $VG_PATH/$1 /var/lib/mysql
if [[ -x "/etc/lmm/post-checkout" ]]
then
/etc/lmm/post-checkout
fi
service mysql start
}