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
- Creating CSV files or txt files of data on
- Daily basis
- Hourly basis.
- By particular Date.
- Making zip or creating tar for all created the files.
- Uploading created tar file on server or on ftp or on our drive
- Again we have to download that .tar file in our application.
- Unzip that file to get all CSV/TXT file.
- 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
- What is the content of CSV file.(Table Values)
- From where we can get those content.(In our case Database)
- Where we are going to store these files.(Output file Location)
- 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,
- Open the Database.
int open = openDB(&db, (char *)conf->dbPath, conf->dbConnectionTimeout);
- Check or validate that Db is opened or not?
If( open != 0){ closeDb(); }
- Create your SQL Query to get the data from Tables.
sprintf(query,”select * from <table-name> where <condition>”);
- Do File Handling , create the file and variable.
createFile(filePath)
char *writeData=NULL;
writeData = calloc(1024,sizeof(char));
- 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));
- Do the Formatting while Data is appending in variable.
if(col >= cols -1)
{
strcat(writeData,”\n
}
else
{
strcat(writeData,”,”);
}
- Then check file is present or not , if not then create it .
if(!isFilePresent(filePath))
{
if(createFile(filePath))
{
printf(“File (%s) created!\n”,filePath);
}
}
- Lastly write the value of variable in File.
status = appendFile(filePath, &writeData);
- Release the memory of variable.
free(writeData);
- Finalize your Statement.
sqlite3_finalize(stmt);
- Close the Database.
closeDB(&db);
Flow of creation.
- Read the value of DB Path, Opfilepath, log path from pushagentconfig file.
- Read the value from backlogconfig file ,Date and Time, FTP related information.
- Append both into single variable.
- According to that variable fire query on database to get the timestamp
- After that according to that Starting timestamp and ending timestamp.
- Then we are reading the type ,
- According to given type we assign the value of interval variable day(86400),Hour(3600),
(last-start) timestamp
- After getting all the detail run the for loop from start to end timestamp. by assigning the interval.
- Append the opFilepath, date and filename.
- Prepare the query and call the function Creating CSV by passing Filename, Cols, stmt and length
- Then CreatingCSV function called, in that function we declare one variable with some memory and appended all the data’s in that variable.
- Write the formatted content of variable in file.
- Releasing the memory. By this way one table data come into one csv file with header.
- Again for next table new filename, query will be created and the same process is going on.
- After every single File creation, we are finalizing the statement making the query-variable content to null to reuse it.
- Lastly closing the Database.
Best Open Source Business Intelligence Software Helical Insight is Here