How to create Backup using C,Best Practices while creating CSV or txt file using C

How to create   Backup  using c?

Backup   is  the  process  of  taking  the  backup of  Data  and  saving it on another location  for future Reference. This process includes

  1. Creating   CSV files or txt files  of   data on
    1. Daily basis
    2. Hourly basis.
    3. By particular Date.
  2. Making zip or creating   tar for all created the files.
  3. Uploading created tar file on server or on ftp or on our drive
  4. Again we have to download that .tar file in our application.
  5. Unzip that file to get all CSV/TXT file.
  6. Read all the Data from CSV/TXT file.

 

How to create CSV/TXT  File?

To create CSV/TXT File by using C –language, first we have to decide

  1. What  is  the  content  of  CSV  file.(Table Values)
  2. From where  we can  get those content.(In our case Database)
  3. Where we are going to store these files.(Output file Location)
  4. What is the Name of Our CSV File?(CSV file Name)

So  in  our Case to get the data from Database we have to do,

  1. Open  the Database.

int open = openDB(&db, (char *)conf->dbPath, conf->dbConnectionTimeout);

  1. Check  or validate  that  Db is  opened  or  not?

If( open != 0){   closeDb();           }

  1. Create your  SQL Query to get the data from Tables.

sprintf(query,”select * from <table-name> where <condition>”);

  1. Do  File Handling , create  the file and variable.

createFile(filePath)

char *writeData=NULL;

writeData = calloc(1024,sizeof(char));

  1. Fire the SQL Query on Specific Tables and get the value in variable.

result = executeQuery((char *)query, &db, &stmt);

strcat(writeData,sqlite3_column_name(*stmt,col));

 

  1. Do the Formatting while Data is appending in variable.

if(col >= cols -1)

{

       strcat(writeData,”\n

   }

  else

  {

        strcat(writeData,”,”);

   }

  1. Then check file is present or not , if not then create it .

if(!isFilePresent(filePath))

 {

    if(createFile(filePath))

      {

             printf(“File (%s) created!\n”,filePath);

       }

 }

  1. Lastly write the value of variable in File.

status = appendFile(filePath, &writeData);

  1. Release the memory of variable.

free(writeData);

  1. Finalize your Statement.

sqlite3_finalize(stmt);

  1. Close the Database.

closeDB(&db);

 

Flow of  creation.

  1. Read the value of DB Path, Opfilepath, log path from pushagentconfig file.
  2. Read   the value   from backlogconfig  file  ,Date and Time, FTP related information.
  3. Append both into single variable.
  4. According to that variable  fire query on database to get the timestamp
  5. After that according to that Starting timestamp   and ending timestamp.
  6. Then we are reading the type ,
  7. According to  given type we assign the value of interval variable day(86400),Hour(3600),

(last-start) timestamp

  1. After getting all the detail run the  for loop from start to end timestamp. by   assigning the interval.
  2. Append the   opFilepath, date and filename.
  3. Prepare the query and call  the  function Creating CSV  by passing Filename, Cols, stmt and length
  4. Then CreatingCSV   function called, in that function we declare one variable with some memory and appended all the data’s in that variable.
  5. Write the formatted content   of variable in file.
  6. Releasing the memory. By   this way one table data come into one csv   file with header.
  7. Again   for next table new filename, query will be created and the same process is   going on.
  8. After every single File creation, we are finalizing the statement making the query-variable content to null to reuse it.
  9. Lastly closing the Database.