Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Litemall
Commits
3487f5d6
Commit
3487f5d6
authored
Jan 18, 2020
by
Junling Bu
Browse files
feat[litemall-core]: 支持数据库备份操作
parent
1aa9f87d
Changes
3
Hide whitespace changes
Inline
Side-by-side
docker/docker-compose.yml
View file @
3487f5d6
...
...
@@ -26,6 +26,7 @@ services:
volumes
:
-
./litemall/storage:/storage
-
./litemall/logs:/logs
-
./litemall/backup:/backup
-
/etc/localtime:/etc/localtime
depends_on
:
-
mysql57
...
...
litemall-db/src/main/java/org/linlinjava/litemall/db/util/DbUtil.java
0 → 100644
View file @
3487f5d6
package
org.linlinjava.litemall.db.util
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
DbUtil
{
public
static
void
backup
(
File
file
,
String
user
,
String
password
,
String
db
)
{
try
{
Runtime
rt
=
Runtime
.
getRuntime
();
String
command
=
"mysqldump -u"
+
user
+
" -p"
+
password
+
" --set-charset=utf8 "
+
db
;
Process
child
=
rt
.
exec
(
command
);
InputStream
inputStream
=
child
.
getInputStream
();
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
,
StandardCharsets
.
UTF_8
));
OutputStreamWriter
outputStreamWriter
=
new
OutputStreamWriter
(
new
FileOutputStream
(
file
),
StandardCharsets
.
UTF_8
);
String
str
;
while
((
str
=
bufferedReader
.
readLine
())
!=
null
)
{
outputStreamWriter
.
write
(
str
+
"\r\n"
);
}
outputStreamWriter
.
flush
();
inputStream
.
close
();
bufferedReader
.
close
();
outputStreamWriter
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
static
void
load
(
File
file
,
String
user
,
String
password
,
String
db
)
{
try
{
Runtime
rt
=
Runtime
.
getRuntime
();
String
command
=
"mysql -u"
+
user
+
" -p"
+
password
+
" --default-character-set=utf8 "
+
db
;
Process
child
=
rt
.
exec
(
command
);
OutputStream
outputStream
=
child
.
getOutputStream
();
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
file
),
StandardCharsets
.
UTF_8
));
OutputStreamWriter
outputStreamWriter
=
new
OutputStreamWriter
(
outputStream
,
StandardCharsets
.
UTF_8
);
String
str
;
while
((
str
=
bufferedReader
.
readLine
())
!=
null
)
{
outputStreamWriter
.
write
(
str
+
"\r\n"
);
}
outputStreamWriter
.
flush
();
outputStream
.
close
();
bufferedReader
.
close
();
outputStreamWriter
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
litemall-db/src/test/java/org/linlinjava/litemall/db/DbUtilTest.java
0 → 100644
View file @
3487f5d6
package
org.linlinjava.litemall.db
;
import
org.junit.Test
;
import
org.linlinjava.litemall.db.util.DbUtil
;
import
java.io.File
;
public
class
DbUtilTest
{
@Test
public
void
testBackup
()
{
File
file
=
new
File
(
"test.sql"
);
DbUtil
.
backup
(
file
,
"litemall"
,
"litemall123456"
,
"litemall"
);
}
// 这个测试用例会重置litemall数据库,所以比较危险,请开发者注意
// @Test
public
void
testLoad
()
{
File
file
=
new
File
(
"test.sql"
);
DbUtil
.
load
(
file
,
"litemall"
,
"litemall123456"
,
"litemall"
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment