-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathght-restore-mysql-projects
executable file
·106 lines (89 loc) · 2.6 KB
/
ght-restore-mysql-projects
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
#!/usr/bin/env bash
# Modified version of ght-restore-mysql which only loads the projects table
# defaults
user="ghtorrent"
passwd=""
host="localhost"
db="ghtorrent"
engine="MyISAM"
usage()
{
echo "Usage: $0 [-u dbuser ] [-p dbpasswd ] [-h dbhost] [-d database ] dump_dir"
echo
echo "Restore a database from CSV and SQL files in dump_dir"
echo " -u database user (default: $user)"
echo " -p database passwd (default: $passwd)"
echo " -h database host (default: $host)"
echo " -d database to restore to. Must exist. (default: $db)"
echo " -e db engine: MyISAM for fast import and querying speed"
echo " InnoDB for normal operations (default: $engine)"
}
if [ -z $1 ]
then
usage
exit 1
fi
while getopts "u:p:h:d:e:" o
do
case $o in
u) user=$OPTARG ;;
p) passwd=$OPTARG ;;
h) host=$OPTARG ;;
d) db=$OPTARG ;;
e) engine=$OPTARG ;;
\?) echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
esac
done
# Setup MySQL command line
if [ -z $passwd ]; then
mysql="mysql -u $user -s -h $host -D $db"
else
mysql="mysql -u $user --password=$passwd -s -h $host -D $db"
fi
shift $(expr $OPTIND - 1)
dumpDir=$1
if [ ! -e $dumpDir ]; then
echo "Cannot find directory to restore from"
exit 1
fi
# Convert to full path
dumpDir="`pwd`/$dumpDir"
if [ ! -e $dumpDir/schema.sql ]; then
echo "Cannot find $dumpDir/schema.sql to create DB schema"
exit 1
fi
# 1. Create db schema
echo "`date` Creating the DB schema"
cat $dumpDir/schema.sql |
sed -e "s/\`ghtorrent\`/\`$db\`/" |
sed -e "s/InnoDB/$engine/"|
grep -v "^--" |
$mysql
# 2. Restore CSV files with disabled FK checks
#for f in $dumpDir/*.csv ; do
# table=`basename $f|cut -f1 -d'.'`
# echo "`date` Restoring table $table"
# echo "SET foreign_key_checks = 0; LOAD DATA LOCAL INFILE '$f' INTO TABLE $table CHARACTER SET UTF8 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' " |$mysql || exit 1
#done
# 2. Restore only the projects data
file_project="projects.csv"
table=`basename $file_project|cut -f1 -d'.'`
echo "`date` Restoring table $table"
echo "SET foreign_key_checks = 0; LOAD DATA LOCAL INFILE '$file_project' INTO TABLE $table CHARACTER SET UTF8 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' " |$mysql || exit 1
# 3. Create indexes
if [ ! -e $dumpDir/indexes.sql ]; then
echo "Cannot find $dumpDir/indexes.sql to create DB indexes"
exit 1
fi
echo "`date` Creating indexes"
cat $dumpDir/indexes.sql |
sed -e "s/\`ghtorrent\`/\`$db\`/" |
grep -v "^--" |
while read idx; do
echo "`date` $idx"
echo $idx | $mysql || exit 1
done
#: ft=bash