How I Created A C Sharp Module For Automatic Data Back Up

INTRODUCTION

One of the great things about programming code is the fact that it can be harnessed to automate small, but important chores that are often overlooked. This is especially critical in a business where one typically has too much to do with insufficient time to do it. Here, I will discuss C# code I used for an actual customer of mine. It selectively performs an automated data back up operation upon entering the inventory application I designed for them.

HAS A BACK UP OPERATION OCCURRED IN THE LAST 7 DAYS?

This automated data back up code is fired upon entering the Winform’s “Load Event”. After the initial chores are taken care of, the back up log data file, “bulog.txt”, will be accessed. The last appended record to the back up log will be read since it represents the last time an automated back up was performed. Next, the month/day/year components from the last back up date will be retrieved to construct a DateTime object I call last_auto_backup_date. The last back up date will be compared to the date 7 days prior to the current date. If the date from 7 days ago is greater than or equal to the last automated back up date, then a new automated back up operation will commence. The programming below illustrates how the date manipulation is done.

  1. // this is a "load" module of a C# application for inventory  
  2.   
  3. // management. upon firing the "load" event from the  
  4.   
  5. // main menu screen, this module will test to see if an automatic  
  6.   
  7. // back up of the master inventory data file, "inventory.txt" and the  
  8.   
  9. // detail transaction file associated with each master inventory item  
  10.   
  11. // have been performed in the last 7 days. if not, then it will  
  12.   
  13. // automatically be performed before the main menu is accessible  
  14.   
  15. // to the user.  
  16.   
  17. private void FormGeneric_Load(object sender, System.EventArgs e)  
  18.   
  19. {  
  20.   
  21. // declare variables used in the "load" event of the main menu.  
  22.   
  23. int screen_width, screen_height, yearvar, monthvar, dayvar, a;  
  24.   
  25. string stringVal, currdir, item_file_name, bkupDir, strmonthvar, strdayvar, string_file_name_backedup;  
  26.   
  27. string string_file_name;  
  28.   
  29. string string_auto_backup_date, string_auto_backup_description, stringError;  
  30.   
  31. long sizeofdatafile, sizeofdatafile2, datafileoffset, datafileoffset2, filelengthvar;  
  32.   
  33. // size of each record in the back up log data file.  
  34.   
  35. const long BULEN = 32;  
  36.   
  37. // size of each record in the master inventory data file.  
  38.   
  39. const long MASTERLEN = 60;  
  40.   
  41. // size of each record in the transaction detail data file.  
  42.   
  43. const long DETAILEN = 75;  
  44.   
  45. // character arrays used to manipulate the data.  
  46.   
  47. char[] recordatavar = new char[MASTERLEN - 2];  
  48.   
  49. char[] recordatavar_transactions = new char[DETAILEN - 2];  
  50.   
  51. char[] backup_log_data = new char[BULEN - 2];  
  52.   
  53. char[] char_auto_backup_date, char_auto_backup_description;  
  54.   
  55. // set the boundaries of the menu screen.  
  56.   
  57. screen_width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;  
  58.   
  59. screen_height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;  
  60.   
  61. this.SetBounds(1, 1, screen_width, screen_height);  
  62.   
  63. // set the start position of the menu screen to the center of the viewport.  
  64.   
  65. this.StartPosition = FormStartPosition.CenterScreen;  
  66.   
  67. // get the current working directory of the installation.  
  68.   
  69. currdir = Directory.GetCurrentDirectory();  
  70.   
  71. currdir = currdir.Substring(0, currdir.Length - 9);  
  72.   
  73. // get the date of the last auto back up event by opening  
  74.   
  75. // the back up log file, "bulog.txt".  
  76.   
  77. FileInfo datafileinfo = new FileInfo(currdir + "bulog.txt");  
  78.   
  79. sizeofdatafile = datafileinfo.Length;  
  80.   
  81. StreamReader streamobj = new StreamReader(currdir + "bulog.txt");  
  82.   
  83. streamobj.BaseStream.Seek(sizeofdatafile - BULEN, SeekOrigin.Begin);  
  84.   
  85. stringVal = streamobj.ReadLine();  
  86.   
  87. streamobj.Close();  
  88.   
  89. // use the TryParse method to split the date portion of the  
  90.   
  91. // string "stringVal" into month, day and year components.  
  92.   
  93. // adjust the parsed year component for the 21st century.  
  94.   
  95. int.TryParse(stringVal.Substring(0, 2), out monthvar);  
  96.   
  97. int.TryParse(stringVal.Substring(3, 2), out dayvar);  
  98.   
  99. int.TryParse(stringVal.Substring(6, 2), out yearvar);  
  100.   
  101. yearvar = yearvar + 2000;  
  102.   
  103. // put the month, day and year components into a DateTime  
  104.   
  105. // object called "last_auto_backup_date".  
  106.   
  107. DateTime last_auto_backup_date = new DateTime(yearvar, monthvar, dayvar, 0, 0, 0);  
  108.   
  109. // subtract 7 days from today's date then see if it is greater than  
  110.   
  111. // or equal to the date of the last auto back up; if true then do another  
  112.   
  113. // one for today.  
  114.   
  115. System.DateTime today = System.DateTime.Now;  
  116.   
  117. System.TimeSpan duration = new System.TimeSpan(7, 0, 0, 0);  
  118.   
  119. System.DateTime seven_days_prior_date = today.Subtract(duration);  
  120.   
  121. if (seven_days_prior_date >= last_auto_backup_date)  
  122.   
  123. {  
  124.   
  125. .  
  126. .  
  127. .  
  128.   

 
PREP WORK FOR FILE NAMING CONVENTION IS NEEDED TO PREVENT OVERWRITES ON EXISTING BACKED UP DATA FILES

Before the actual data back up operation can begin, a little preparation work is needed. The backed up file name for the master inventory data file and each of its detail inventory data files must be constructed. This must be done in a way that makes them unique so they won’t overwrite any other master inventory data file or detail inventory data files in the target back up directory. For the master inventory data file, I concatenate the string “inventory_” to a 2 character current month string followed by a 2 character current day string and then a 4 character current year string and then the string “.txt”. For example, if today’s date is 02/06/2013 and an automatic back up operation is performed, then the master inventory file name in the target back up directory would be “inventory_02062013.txt”. A similar methodology is used to name the detail inventory data files that are being copied. Let’s say the detail inventory data file name as found in a given master inventory record is “item0012”. Using our example, its counterpart back up data file in the target back up directory would be “item0012_02062013.txt”. Incorporating the current date into the back up data file’s naming convention ensures that it will always be unique and no previously backed up data files will ever be overwritten.

LOOP AROUND THE MASTER INVENTORY DATA FILE TO PROCESS EACH DETAIL INVENTORY DATA FILE

The first phase of the back up operation will be to back up the master inventory data file itself to the target directory. The second phase will sequentially loop around the source master inventory data file to retrieve each of the file names of the detail inventory data files. Each detail inventory data file contains the inventory transactions that occurred for a particular part number. Before this data file will be copied to the target destination, the program will verify that it does exist on the source drive. If this is not true, the program will advance to the next sequential detail inventory data file that is contained in the master inventory data file.

After verification, the source detail inventory data file will be written to its counterpart back up detail inventory data file in the target back up directory. After the source detail inventory data file has been written, its file stream will be closed. Then the program will repeat this for each successive inventory detail transaction file. Lastly, the source master inventory data file’s stream will then be closed after the last detail transaction data file has been backed up.

HANDLING CORRUPTED DETAIL INVENTORY DATA FILES

Each detail inventory data file size will be measured to ensure that each of its records are of "DETAILEN" size. This is a methodology I use to make sure no extra/errant characters have been inserted into a given data file. This would throw a wrench into the back up process and make the back up operation abort. If the code does detect an erroneous record, the file name is then concatenated to a variable I call "stringError". If there is a problem with one or more detail transaction data files, then this variable will be displayed as a message at the end of processing. Also, the back up log data file will not be updated with a successful back up operation record. Please click here to see the entire code readout.

UPDATE THE BACK UP LOG DATA FILE

After the automated back up has been completed, the program will note this in the back up log data file on the assumption no corrupted detail inventory data files were detected during the back up operation. If even one is detected, the back up log will not be updated and instead a message will appear on screen noting the detail inventory data file(s) that were not processed due to the presence of non-homogenous or errant data records that were encountered during processing.

The back up log is updated so future date comparisons between the date of the last back up operation and 7 days prior to the current date will be accurate. This is actually a pretty simple task. The string "AUTO-BACKUP__PERFORMED" will be concatenated to the end of the string equivalent of the current date. This information will then be appended to the end of the back up log data file, “bulog.txt”.

CONCLUSION

Software development is a vehicle to increased efficiency in the workplace. Given today’s demands on staff and their limited time, manual methods of getting tasks done even on a computer may not be the best choice. My automated back up code for the inventory application I created is just one example of how programming can be used to liberate employees from mundane tasks so they can focus on more important issues at hand.

Up Next
    Ebook Download
    View all
    Learn
    View all