-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpSeparator.sh
executable file
·98 lines (65 loc) · 2.12 KB
/
dumpSeparator.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
#!/bin/bash
#Copyright (c) 2018 Riccardo Gagliarducci <[email protected]>
#https://github.com/canonex/dumpSeparator
#This is free software. You may redistribute copies of it under the terms of the GNU General Public License.
#There is NO WARRANTY, to the extent permitted by law.
#Courtesy of https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash
#Courtesy of https://stackoverflow.com/questions/4332478/read-the-current-text-color-in-a-xterm/4332530#4332530
NORMAL=$(tput sgr0)
RED=$(tput setaf 1)
LIME_YELLOW=$(tput setaf 190)
CYAN=$(tput setaf 6)
usage() {
echo ""
echo "Usage: $0 list DUMPFILE"
echo " or $0 extract|purge DATABASENAME DUMPFILE"
echo ""
echo "DUMPFILE is the sql dump ex. mydump.sql"
echo "DATABASENAME is the name of the database, use list command to list the available names"
echo "" 1>&2; exit 1; }
case $1 in
list)
if [ -z "$1" ] || [ -z "$2" ]; then
echo "List option expects one database name."
usage
exit 1
fi
echo "Listing db ${LIME_YELLOW}$2${NORMAL}:"
echo "${CYAN}"
#Listing all db
grep 'Dumping events' "$2" | sed 's/-- Dumping events for database //' | sed "s/'//g"
echo "${NORMAL}"
;;
extract)
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "Extract option expects one database name and one mysql dump file."
usage
exit 1
fi
#Making a backup
cp "$3" "$3.old"
echo "Extracting ${LIME_YELLOW}$2${NORMAL} from $3"
#Keeping text until "Dumping", keeping text after "Current"...
sed -i -e "/-- Dumping events for database '$2'/q" -e '/-- Current Database: `'"$2"'`/,$!d' "$3"
fname="${3%.*}"_$2.sql
mv "$3" "$fname"
;;
purge)
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "Purge option expects one database name and one mysql dump file."
usage
exit 1
fi
#Making a backup
cp "$3" "$3.old"
echo "Purging ${RED}$2${NORMAL} from $3"
#Keeping from beginning to end
sed -i "/-- Current Database: \`$2\`/,/-- Dumping events for database '$2'/d" "$3"
fname="${3%.*}"_purged.sql
mv "$3" "$fname"
;;
*)
usage
exit 1
esac
exit 0