summaryrefslogtreecommitdiff
path: root/deps/acore/mysql-tools/mysql-dump
blob: 33e5e35aca4644f0783dad53b7beac3d58cc920b (plain)
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
#
# * Copyright (C) 2007 - 2015 Hyperweb2 All rights reserved.
# * GNU General Public License version 3; see www.hyperweb2.com/terms/

echo "starting dump process.."
# check config from same folder and include only if exists
CONF_FILE=$MT_DIR"/mysql-config"
if [ -f "$CONF_FILE" ]; then
    source "$CONF_FILE"
fi;

#overwrite configs if file exists and variables are defined
if [ ! -z "$4" ]; then
    if [ -e "$4" ]; then
        source "$4"
    else # if 4th parameter is not a file, then try to eval
        eval "$4"
    fi;
fi;

source $MT_DIR"/shared-def"

if [ ! -z "$1" ]; then 
 MYSQL_DB=$1;
fi

#change group instead mod
#group=`ls -l tables | awk '{print $4}'`
#if [ $group != "mysql" ]; then
#    if (($CHMODE != 0)); then
#       sudo chgrp -v mysql $TPATH
#    fi
#fi

#change permissions for other users
if [ ! -z $TPATH ]; then
	if [ ! -d "$TPATH" ]; then
		#create the path recursively
		echo "creating dir: $TPATH.."
		mkdir -p "$TPATH"
	fi
	
	if (($CHMODE != 0)); then
		echo "changing permissions.."
		sudo chmod -v o=rwx $TPATH
	fi

	#clean old tables
	if (($CLEANFOLDER != 0)); then
		rm -rvf $TPATH/*
	fi
	
	if [ ! -z "$2" ]; then 
	#if tables are specified in parameters then..
		arr=$(echo $2 | tr "," "\n")

		for T in $arr
		do
			echo "exporting "$T;
			if (($TEXTDUMPS != 1)); then
				FILE="$TPATH/$T.sql"
				if [ -f $FILE ]; then
					rm -f $FILE
				fi

                                if (($PARSEDUMP != 0)); then
                                    echo "Parsing enabled";
                                    eval "$MYSQLDUMP$OPTS $DUMPOPTS '$MYSQL_DB' '$T' | $DUMPPARSER > $FILE"
                                else
                                    eval "$MYSQLDUMP$OPTS $DUMPOPTS '$MYSQL_DB' '$T' -r $FILE"
                                fi
			else
				echo $(eval "$MYSQLDUMP$OPTS $DUMPOPTS --tab=$TPATH/ '$MYSQL_DB' '$T'")
			fi
		done
		
	else 
	#else get all tables from selected db
			CMD="$MYSQL$OPTS -N -B -e 'show tables from \`$MYSQL_DB\`'"
			echo "command: "$CMD
			for T in $(eval $CMD)
			do 
				if (($TEXTDUMPS != 1)); then
					echo "exporting "$T;
					FILE="$TPATH/$T.sql"
					if [ -f $FILE ]; then
						rm -f $FILE
					fi

                                        if (($PARSEDUMP != 0)); then
                                            echo "Parsing enabled on file "$FILE;
                                            eval "$MYSQLDUMP$OPTS $DUMPOPTS '$MYSQL_DB' '$T' | $DUMPPARSER > $FILE"
                                        else
                                            eval "$MYSQLDUMP$OPTS $DUMPOPTS '$MYSQL_DB' '$T' -r $FILE"
                                        fi
				else
					echo "exporting "$MYSQL_DB" tables: "$T;
					echo $(eval "$MYSQLDUMP$OPTS $DUMPOPTS --tab=$TPATH/ '$MYSQL_DB' '$T'")
				fi
			done;

	fi
fi

if [ ! -z "$3" ]; then FULL=$3; fi
# export full file if option is enabled
if (($FULL != 0)); then   
	echo 'exporting FULL '$MYSQL_DB' in single file';
	rm -f $FPATH

        if (($PARSEDUMP != 0)); then
            echo "Parsing enabled";
            eval "$MYSQLDUMP$OPTS $DUMPOPTS '$MYSQL_DB' | $DUMPPARSER > $FPATH"
        else
            eval "$MYSQLDUMP$OPTS $DUMPOPTS '$MYSQL_DB' -r $FPATH"
        fi
fi