The database backup periodically removes the development of the program.

Source: Internet
Author: User
Tags db2 filetime

First, the recent customer server database backup is ongoing every day, and as the volume of data continues to increase, the backup is constantly increasing.

Secondly, some problems arise. The client's server has limited disk space, and the regular database backup file is too large to save the problem.

Thirdly, this results in server downtime, some applications in the server, and the inability to use the website.

Iv. so the need to solve this problem requires the development of an application to periodically delete database backups and other files.

Five. In the database backup to remove the need to be aware of the deletion of a set of days before the database backup, the other latest database backup files do not delete.

Sixth: You can apply Windows services to develop a program that periodically deletes a database backup, or you can apply the console to develop a program that periodically deletes a backup of the database of course if the application Console application for development, then it is best to combine the Windows system comes with the task of the program is more convenient. Of course, you can make a choice.

Then if you do delete the specified database backup files such as db2,dbname.0.db2.dbpart000.20161010153053.001 and so on and SQL Server Dbname.bak and so on

As well as backup files for MySQL database, etc., and other file formats are supported.

Seventh. So what I need to do to solve this problem is:

1. First, you need to find the path of the database backup file to read it, of course, the path known here refers to the physical path.

2. What needs to be done is to read the file name of the database as well as the time of the database backup, in order to facilitate the retrieval of the latest and the database backup days ago.

3. Of course you will also need to apply some specific removal methods in the file class. To delete them.

Eighth: Below I will write my own periodic delete database backup Console application posted for your reference.

periodically delete the database backup file code as follows

Class program{static void Main (string[] args) {string strdirfile = Properties.Settings.Default.DB_FILE_PATH        ;        Console.WriteLine ("Strdirfile{0}", Strdirfile); int keepfilecnt = Int.        Parse (Properties.Settings.Default.KEEP_FILE_CNT); try {if (string. IsNullOrEmpty (strdirfile) | | !            Directory.Exists (Strdirfile)) {Console.WriteLine ("The path of the configured file is incorrect please check {0}" + strdirfile);            } if (keepfilecnt <= 0) {Console.WriteLine ("No days configured for deletion");                    if (strdirfile! = null) {if (directory.exists (Strdirfile)) {                    string[] Strdirs = directory.getdirectories (strdirfile);                    string[] Strfiles = Directory.GetFiles (strdirfile);                    Dbnameandtime file = new Dbnameandtime (); if (strfiles! = null) {foreach (String strfile in strFiles) {if (strfile! = null) {                                FileInfo fi = new FileInfo (strfile); string[] Strarr = fi.                                Name.split ('. '); File. Dataname = fi.                                 Name; File.                                 FileTime = strarr[5]; File.                                URL = strfile;                                 DateTime dt1 = DateTime.Now; if (Strarr.length > 0) {IFormatProvider Provider = n                                    EW CultureInfo ("ZH-CN");                                    String tarstr = "Yyyymmddhhmmss"; DateTime DT2 = datetime.parseexact (file.                                    Filetime.tostring (), tarstr, provider);                                    TimeSpan ts = DT1-DT2;                                   Console.WriteLine ("Time days: {0}", TS); if (TS. Totaldays >= keepfilecnt) {if (strfile! = Nu                                            ll) {file.delete (strfile);                                            using (StreamWriter SW = new StreamWriter (strdirfile + DELETE log of database backup. txt ", true)) {SW.                                            WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + "database backup Delete start:" + "deleted path:" + strdirfile + "Delete database backup name" + strfile); } Console.WriteLine ("{0} database backup file deleted successfully!                                        ", DateTime.Now);                                            } else { Console.WriteLine ("Delete file failed!")                                        ");     }                                    }                               else {CONSOLE.WR Iteline ("The other backup files are up-to-date No 3 days ago Backup!                                    ");                    }                                }                            }                        }                    } Save the deleted root folder foreach (String Strdir in Strdirs) {Di Rectory.                    Delete (Strdir, true); }} else {Console.WriteLine ("Only the root directory in this directory!")                "); }} else {Console.WriteLine ("This directory does not exist!            "); }} catch (Exception ex) {//writes the database backup exception to Notepad using (StreamWriter SW = new Stre Amwriter (strdirfile + DELETE log of database backup. txt ", true)) {SW. WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + "backup files are up-to-date No 3 days ago" + ex. Message);           }}}}//the class for declaring a backup database public class dbnameandtime{private string FileName; <summary>///File name///</summary> public string filename {get {return FileName;} set {Filenam e = value;    }} private string FileTime; <summary>////File Last modified time///</summary> public string FileTime {get {return FileTime;} set {fi Letime = value;    }} private string Dataname; <summary>///Database name///</summary> public string Dataname {get {return dataname;} set {Datana me = value;    }} private string URL;     <summary>///file path///</summary> public string URL {get {return Url;} set {Url = value;}} public string Downurl {get {return fileName;}}}

  

The database backup periodically removes the development of the program.

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.