Java Scheduled BACKUP database

Source: Internet
Author: User

/**

manipulating databases

*/

public class BackupDB {
Public String Backup () throws ioexception{
String user = "root"; User name of the database
String Password = "admin";//password for database
String database = "Hrtweb";//the names of the databases to be backed up
Date date = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss");
String filepath = "d:\\" +sdf.format (date) + ". sql";
File File = new file ("d:\\", Sdf.format (date) + ". sql");
if (!file.exists ()) {
File.createnewfile ();
}
String STMT1 = "mysqldump" + Database + "-H 127.0.0.1" + "-u" + user + "-p" +
Password + "--DEFAULT-CHARACTER-SET=GBK--result-file=" + filepath;
try {
Runtime.getruntime (). exec (STMT1);
System.out.println ("Saved to" + filepath + ");
} catch (IOException e) {
E.printstacktrace ();
}
return filepath;
}
}

/**

Creating timers

*/

public class Picktask {
Private Timer timer = new timer ();
Private TimerTask task = new TimerTask () {
public void Run () {
Date date = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String begindate = Sdf.format (date);
String beginTime = begindate.substring (11, 16);
System.out.println ("Start time:" +begindate);

BackupDB bdb = new backupdb ();
Set backup time
if (Begintime.equals ("17:09")) {
try {
Bdb.backup (); Perform a file backup
String dbName = Bdb.backup (). toString (); Remove the backup file name
String Path = "d:\\";
int Nameno = dbname.lastindexof ("\ \");
Determine if the file exists, if it exists, the backup succeeds, and if it does not, the backup does not succeed and needs to be backed up again
File File = new file (path, dbname.substring (Nameno + 1,dbname.length ()));
if (file.exists ()) {
SYSTEM.OUT.PRINTLN ("Backup succeeded");

}else{

System.out.println ("Backup failed, re-backup");
Back up without a successful backup
New Picktask (). Start (1, 1);
}

} catch (FileNotFoundException e) {
System.out.println ("Can not find the file");
} catch (IOException e) {
E.printstacktrace ();
}
}else{
System.out.println ("The time is not yet, do not worry Oh!") ");
}
}
};

The Start method cannot be less, mainly the schedule method
public void Start (int delay, int internal) {
Timer.schedule (Task, delay *, internal * 1000);
}
}



/**

Test class, perform scheduled backup instructions

*/

public class Timeruse {
public static void Main (string[] args) {
Picktask picktask = new Picktask ();
Picktask.start (1, 60); Executes once every 60 seconds
}
}

Java Scheduled BACKUP database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.