Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
New MineTRS_java
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kiritoausna
New MineTRS_java
Commits
17ad49ba
Commit
17ad49ba
authored
Mar 19, 2024
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2024-3.19
parent
a0f36d5d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
14 deletions
+78
-14
DataBack.java
.../main/java/me/zhengjie/gemho/controller/tab/DataBack.java
+76
-13
StData.java
...m/src/main/java/me/zhengjie/gemho/entity/data/StData.java
+1
-0
AuthorizationController.java
...nerato/modules/security/rest/AuthorizationController.java
+1
-1
No files found.
eladmin-system/src/main/java/me/zhengjie/gemho/controller/tab/DataBack.java
View file @
17ad49ba
package
me
.
zhengjie
.
gemho
.
controller
.
tab
;
import
me.zhengjie.annotation.Log
;
import
me.zhengjie.gemho.util.PostOrPutResult
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
...
...
@@ -8,9 +10,16 @@ import org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.sql.DataSource
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.InputStreamReader
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* <p>
...
...
@@ -23,15 +32,34 @@ import java.util.Date;
@RestController
@RequestMapping
(
"tab/back"
)
public
class
DataBack
{
@Value
(
value
=
"${
filep
ath}"
)
@Value
(
value
=
"${
backP
ath}"
)
String
filepath
;
@Autowired
private
DataSource
dataSource
;
@Value
(
value
=
"${spring.datasource.druid.password}"
)
String
password
;
@Value
(
value
=
"${environment.path}"
)
private
String
envPath
;
@Log
(
"备份数据库"
)
@PostMapping
public
ResponseEntity
<
Object
>
deal
()
{
System
.
out
.
println
(
"备份数据库开始执行"
);
try
{
Connection
connection
=
dataSource
.
getConnection
();
DatabaseMetaData
metaData
=
connection
.
getMetaData
();
String
name
=
connection
.
getCatalog
();
String
userName
=
metaData
.
getUserName
();
Pattern
pattern
=
Pattern
.
compile
(
"^(.*?)@"
);
Matcher
matcher
=
pattern
.
matcher
(
userName
);
if
(
matcher
.
find
())
{
userName
=
matcher
.
group
(
1
);
System
.
out
.
println
(
userName
);
}
String
url
=
metaData
.
getURL
();
String
ipAddress
=
extractIpAddress
(
url
);
String
backName
=
new
SimpleDateFormat
(
"yyMMddHHmmss"
).
format
(
new
Date
())
+
".sql"
;
dbBackup
(
"root"
,
"jinghe2021//"
,
"mdm_1_db"
,
"d:/file/"
,
backName
);
dbBackup
(
envPath
,
userName
,
password
,
name
,
filepath
,
backName
,
ipAddress
);
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
"备份异常"
);
ex
.
printStackTrace
();
...
...
@@ -63,7 +91,7 @@ public class DataBack {
return
ps
;
}
public
static
void
dbBackup
(
String
username
,
String
authenticate
,
String
dbName
,
String
destination
,
String
backName
)
{
public
static
void
dbBackup
Comd
(
String
username
,
String
authenticate
,
String
dbName
,
String
destination
,
String
backName
,
String
url
)
{
File
backupDir
=
new
File
(
destination
);
if
(!
backupDir
.
exists
())
{
backupDir
.
mkdirs
();
...
...
@@ -76,8 +104,8 @@ public class DataBack {
//mysqldump -h localhost -u root -p db_name > db_name.sql
StringBuffer
buffer
=
new
StringBuffer
();
buffer
.
append
(
"mysqldump"
);
buffer
.
append
(
" -h
8.142.46.126
"
);
buffer
.
append
(
" --column-statistics=0 "
);
buffer
.
append
(
" -h
"
+
url
+
"
"
);
//
buffer.append(" --column-statistics=0 ");
buffer
.
append
(
" -u"
+
username
);
buffer
.
append
(
" -p"
+
authenticate
);
buffer
.
append
(
" "
+
dbName
+
" -r "
);
...
...
@@ -94,17 +122,52 @@ public class DataBack {
ex
.
printStackTrace
();
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"备份数据库开始执行"
);
public
static
void
dbBackup
(
String
envPath
,
String
username
,
String
authenticate
,
String
dbName
,
String
destination
,
String
backName
,
String
url
)
{
File
backupDir
=
new
File
(
destination
);
if
(!
backupDir
.
exists
())
{
backupDir
.
mkdirs
();
}
try
{
String
backName
=
new
SimpleDateFormat
(
"yyMMddHHmmss"
).
format
(
new
Date
())
+
".sql"
;
dbBackup
(
"root"
,
"jinghe2021//"
,
"mdm_1_db"
,
"d:/file/"
,
backName
);
File
sqlFile
=
new
File
(
backupDir
,
backName
);
if
(!
sqlFile
.
exists
())
{
sqlFile
.
createNewFile
();
}
String
cmd
=
envPath
+
"mysqldump -h "
+
url
+
" -u"
+
username
+
" -p"
+
authenticate
+
" "
+
dbName
+
" -r "
+
sqlFile
;
System
.
out
.
println
(
"cmd命令为:"
+
cmd
);
ProcessBuilder
processBuilder
=
new
ProcessBuilder
(
"cmd"
,
"/c"
,
cmd
);
processBuilder
.
redirectError
(
new
File
(
destination
+
"error.log"
));
Process
process
=
processBuilder
.
start
();
// 读取命令执行的输出(包括标准输出和标准错误)
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
System
.
out
.
println
(
line
);
}
int
exitCode
=
process
.
waitFor
();
if
(
exitCode
==
0
)
{
System
.
out
.
println
(
"备份成功!"
);
}
else
{
System
.
out
.
println
(
"备份失败,Exit Code: "
+
exitCode
);
}
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
"备份异常"
);
ex
.
printStackTrace
();
}
System
.
out
.
println
(
"备份数据库结束"
);
}
private
String
extractIpAddress
(
String
jdbcUrl
)
{
String
ipAddress
=
null
;
// 使用正则表达式提取 IP 地址部分
Pattern
pattern
=
Pattern
.
compile
(
"//(.*?):"
);
Matcher
matcher
=
pattern
.
matcher
(
jdbcUrl
);
if
(
matcher
.
find
())
{
ipAddress
=
matcher
.
group
(
1
);
}
return
ipAddress
;
}
}
...
...
eladmin-system/src/main/java/me/zhengjie/gemho/entity/data/StData.java
View file @
17ad49ba
...
...
@@ -102,6 +102,7 @@ public class StData implements Serializable {
@JSONField
(
format
=
"yyyy-MM-dd HH:mm:ss"
)
@TableField
(
exist
=
false
)
public
Date
jctime
;
@TableField
(
exist
=
false
)
public
String
danwei
;
@TableField
(
exist
=
false
)
...
...
eladmin-system/src/main/java/me/zhengjie/gemho/x_generato/modules/security/rest/AuthorizationController.java
View file @
17ad49ba
...
...
@@ -90,7 +90,7 @@ public class AuthorizationController {
networkTime
=
LocalDateTime
.
now
();
}
//和日期比较
String
expirationTimeString
=
"20
23-12
-26 07:00:00"
;
String
expirationTimeString
=
"20
99-03
-26 07:00:00"
;
DateTimeFormatter
dateTimeFormatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
);
LocalDateTime
expirationTime
=
LocalDateTime
.
parse
(
expirationTimeString
,
dateTimeFormatter
);
boolean
after
=
networkTime
.
isAfter
(
expirationTime
);
...
...
Write
Preview
Markdown
is supported
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