forked from wanderleihuttel/bacula-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_to_postresql.sh
executable file
·103 lines (93 loc) · 3.53 KB
/
mysql_to_postresql.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
#!/bin/bash
#######################################
# Config Parameters
db_username="root"
db_password=""
db_name="bacula"
db_host="localhost"
row_count="RowCountMySQL.log"
echo "--------------------------------------------"
echo " Convert Bacula Catalog MySQL to PostgreSQL"
echo " Author: Wanderlei Hüttel"
echo " Email: [email protected]"
echo " Version: 1.3 - 20/11/2018"
echo
echo "--------------------------------------------"
echo "Please inform your database credentials..."
read -p "MySQL db_name: " -i "${db_name}" -e db_name
read -p "MySQL db_username: " -i "${db_username}" -e db_username
read -p "MySQL db_host: " -i "${db_host}" -e db_host
read -p "MySQL db_password ${db_username}: " -s db_password
echo
echo "--------------------------------------------"
mysql=$(which mysql)
mysqldump=$(which mysqldump)
# List of Bacula Tables
tables="BaseFiles CDImages Client Counters Device File FileSet Filename Job JobHisto JobMedia Location LocationLog Log Media MediaType Path PathHierarchy PathVisibility Pool RestoreObject Snapshot Status Storage UnsavedFiles Version"
#######################################
# Check if the password is not empty
if [ ! -z ${db_password} ]; then
db_password="-p${db_password}"
fi
#######################################
#
echo
echo "Dumping MySQL tables ..."
echo "--------------------------------------------"
echo "Table Time"
echo "--------------------------------------------"
totalstarttime=$(date -u +%s)
for table in ${tables}; do
starttime=$(date -u +%s)
echo "\set AUTOCOMMIT OFF" >> "${table}.sql"
${mysqldump} --no-create-info \
--no-create-db \
--complete-insert \
--compatible=postgresql \
--skip-quote-names \
--disable-keys \
--lock-tables \
--compact \
--skip-opt \
--quick \
-h ${db_host} -u ${db_username} ${db_password} ${db_name} ${table} | \
sed -e 's/0000-00-00 00:00:00/1970-01-01 00:00:00/g' | \
sed -e 's/\\0//g' | \
sed -e 's/\\"/"/g' | \
sed -e 's/\\\\/\\/g' | \
sed -e "s/\\\'/\'\'/g" | \
sed -e "s/_binary//g" \
>> "${table}.sql"
echo "COMMIT;" >> "${table}.sql"
endtime=$(date -u +%s)
elapsedtime=$(expr ${endtime} - ${starttime})
printf "%-15s %8s\n" ${table} $(date -ud @${elapsedtime} +%H:%M:%S)
done
totalendtime=$(date -u +%s)
totalelapsedtime=$(expr ${totalendtime} - ${totalstarttime})
echo "--------------------------------------------"
echo "Elapsed time: $(date -ud @${totalelapsedtime} +%H:%M:%S)"
echo "--------------------------------------------"
echo
echo
echo "" > ${row_count}
#######################################
# Count rows by tables
for table in ${tables} ; do
sql_query="select '$table' as tabela, count(*) as total from $table;"
${mysql} -h ${db_host} -u ${db_username} -D ${db_name} ${db_password} -N -B -e "$sql_query" \
| sed 's/\t/\|/g' | tr '[:upper:]' '[:lower:]' >> ${row_count}
done
sed -i 's/\s//g' ${row_count}
sed -i '/^$/d' ${row_count}
sort ${row_count} -o ${row_count}
echo "Total tables row count"
echo "--------------------------------------------"
echo "Table Row Count"
echo "--------------------------------------------"
output=$(<${row_count})
output=$(echo -e $output | sed 's/[|]/\t/g')
printf "%-15s %8s\n" $output
echo "--------------------------------------------"
echo
echo "Done"