Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Realization of automatic backup and manual backup of database

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Front-end code:

Function createBackUp () {

Var path=$ ("# path") .val ()

$.post ("dataBack", {

'path': path

}, function (data) {

If (data==1) {

Layer.msg ("backup successful!" , {time: 2000})

} else {

Layer.msg ("backup failed, the path does not exist!" , {time: 2000})

}

}, "text")

}

Backend implementation:

Package com.cloudshield.toolbox4.accountmanage.controller

Import java.io.File

Import java.io.IOException

Import java.io.InputStream

Import java.net.UnknownHostException

Import java.sql.Connection

Import java.sql.DriverManager

Import java.sql.PreparedStatement

Import java.text.SimpleDateFormat

Import java.util.Properties

Import java.util.Timer

Import java.util.TimerTask

Import javax.servlet.http.HttpServletRequest

Import javax.servlet.http.HttpServletResponse

Import javax.servlet.http.HttpSession

Import org.springframework.context.annotation.Scope

Import org.springframework.stereotype.Controller

Import org.springframework.web.bind.annotation.RequestMapping

Import org.springframework.web.bind.annotation.RequestMethod

Import org.springframework.web.bind.annotation.ResponseBody

Import com.cloudshield.toolbox4.utils.DateUtils

/ * *

*

* @ XXX

* @ date 2016-10-28 17:24

*

, /

@ Controller

@ RequestMapping ("/")

@ Scope ("prototype")

Public class DataBackpController {

Private static final int BACKUP_SUCCESS=1;/** indicates that backup is successful * /

Private static final int BACKUP_ERROR=0;/** indicates backup failed * /

/ * * to back up manually * /

@ ResponseBody

@ RequestMapping (value= "/ dataBack", method = RequestMethod.POST)

Public Integer dataBack (HttpServletRequest request,HttpSession session,String time) {

Connection conn = null

Try {/ * * read backup path * /

String path = request.getParameter ("path")

/ * * read configuration file * /

InputStream inputStream = this.getClass () .getClassLoader () .getResourceAsStream ("conn.properties")

Properties p = new Properties ()

Try {

P.load (inputStream)

} catch (IOException E1) {

E1.printStackTrace ()

}

/ * * get username, password and path * /

String driverName = p.getProperty ("c3p0.driverClass")

String userName=p.getProperty ("c3p0.user")

String passWord=p.getProperty ("c3p0.password")

String url = p.getProperty ("c3p0.jdbcUrl")

Class.forName (driverName) .newInstance ()

Conn= DriverManager.getConnection (url,userName,passWord)

BackData (path,getDbName (), conn,time)

Return BACKUP_SUCCESS

} catch (Exception e) {

E.printStackTrace ()

} finally {

Try {conn.close ();} catch (Exception e) {}

}

Return BACKUP_ERROR

}

/ * * time setting for automatic backup * /

@ ResponseBody

@ RequestMapping (value= "/ setAutoBackUp", method = RequestMethod.POST)

Public Integer setAutoBackUp (HttpServletRequest request,HttpSession session) {

Try {

Timer timer = new Timer ()

Final String setupstime = request.getParameter ("time")

Final String path = request.getParameter ("path")

TimerTask task = new TimerTask () {

Public void run () {

Connection conn = null

/ * * read configuration file * /

InputStream inputStream = this.getClass () .getClassLoader () .getResourceAsStream ("conn.properties")

Properties p = new Properties ()

Try {

P.load (inputStream)

/ * * get username, password and path * /

String driverName = p.getProperty ("c3p0.driverClass")

String userName=p.getProperty ("c3p0.user")

String passWord=p.getProperty ("c3p0.password")

String url = p.getProperty ("c3p0.jdbcUrl")

Class.forName (driverName) .newInstance ()

Conn= DriverManager.getConnection (url,userName,passWord)

BackData (path,getDbName (), conn,setupstime)

} catch (Exception E1) {

E1.printStackTrace ()

} finally {

Try {conn.close ();} catch (Exception e) {}

}

}

}

If (setupstime.equals (oneday)) {

Timer.scheduleAtFixedRate (task,DateUtils.getOneday (), DateUtils.getOneday ())

}

If (setupstime.equals (week)) {

Timer.scheduleAtFixedRate (task,DateUtils.getWeek (), DateUtils.getWeek ())

}

If (setupstime.equals (halfamonth)) {

Timer.scheduleAtFixedRate (task,DateUtils.getHalfamonth (), DateUtils.getHalfamonth ())

}

If (setupstime.equals (onemonth)) {

Timer.scheduleAtFixedRate (task,DateUtils.getMonth (), DateUtils.getMonth ())

}

If (setupstime.equals (halfayear)) {

Timer.scheduleAtFixedRate (task,DateUtils.getHalfayear (), DateUtils.getHalfayear ())

}

If (setupstime.equals (oneyear)) {

Timer.scheduleAtFixedRate (task,DateUtils.getOneYear (), DateUtils.getOneYear ())

}

Return BACKUP_SUCCESS

} catch (Exception e) {

E.printStackTrace ()

}

Return BACKUP_ERROR

}

/ * * backup method * /

Public String backData (String path,String db_name,Connection conn,String time) throws Exception {

/ * to return the backup name * /

String bk_name = ""

/ * * operate with the database * /

PreparedStatement stmt = null

String sql = ""

Try {

File databasePath = new File (path)

If (! databasePath.exists ()) {

DatabasePath.mkdir ()

DatabasePath.setWritable (true)

}

String file = new SimpleDateFormat ("yyyyMMddHHmmss") .format (new java.util.Date ()) + ".bak"

If (timekeeper invalid invalid timekeeper = "") {

File= "zdbf" + file

}

File newFile=new File (path+File.separator+file)

NewFile.createNewFile ()

NewFile.setWritable (true)

Sql = "backup database" + db_name+ "to disk=N'" + path+File.separator+file+ "'with format,name=N'full backup of" + db_name+ "'"

Stmt = conn.prepareStatement (sql)

Stmt.executeUpdate ()

Bk_name = file

} catch (Exception e) {

E.printStackTrace ()

Throw e

}

Finally

{

Try {stmt.close ();} catch (Exception e) {}

}

Return bk_name

}

/ * * get the name of the database to back up * /

Public String getDbName () {

/ * * read configuration file * /

InputStream inputStream = this.getClass () .getClassLoader () .getResourceAsStream ("other.properties")

Properties p = new Properties ()

Try {

P.load (inputStream)

} catch (IOException E1) {

E1.printStackTrace ()

}

String dbname=p.getProperty ("dbName")

Return dbname

}

/ * * if either post or get is not specified here, either of the two modes is allowed by default * /

@ RequestMapping (value = "/ autoBackupPage", produces= "text/html;charset=UTF-8")

Public String autoBackupPage (HttpServletRequest request, HttpServletResponse response) throws UnknownHostException, IOException {

Return "jsp/accountManage/backupWindow"

}

/ * * jump to the main page of data backup * /

@ RequestMapping (value = "/ toBackupPage", produces= "text/html;charset=UTF-8")

Public String toBackupPage (HttpServletRequest request, HttpServletResponse response) throws UnknownHostException, IOException {

Return "jsp/accountManage/backUp"

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report