8 Replies - 1215 Views - Last Post: 16 February 2013 - 11:40 PM Rate Topic: -----

#1 <utkarsh>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 13

c/c++ programmer to backup whole hard disk

Posted 07 February 2013 - 07:36 PM

hey guys,

was searching net for software to backup hardive and found many of them but i want to code one of my own. will it be similar to copying file?? n i want copy of whole hard drive inculding structure and data like any typical hardrive backup software like GHOST. thanks :helpsmilie:

this code ws ment to copy file but as every thing in linux is treated as file it should work to copy hard drive so i passed argument /dev/sda but didnt seems to work.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
 const int buffsize=200; 
 FILE *infile,*outfile;
 char buffer[buffsize],op='n';
 int size,check=0,i,d,m;
 //inf=argv[1];
 //of=argv[2]; 
 /*
 printf("enter source file name: ");
 scanf("%s",inf);
 printf("enter destination file name: ");
 scanf("%s",of);
 */ 
  
 infile=fopen(argv[1],"r");
 outfile=fopen(argv[2],"r"); 
  
 if(infile==NULL)
 { 
  printf("\nERROR :: input does not exist\n");   
  check=1;
  return 0;
 }//end if 
 
 if(outfile!=NULL)
 {    
  printf("\nERROR :: output file already exist\n do you want to overwrite?? (y/n)");
  fflush(stdin);
  scanf("%c",&op);
  scanf("%c",&op);
  if(op=='n')
   check=1; 
 } 

 if(check==1)
 {
  exit(0);
 }
 
 outfile=fopen(argv[2],"w");
 
 fseek(infile,0,SEEK_END);
 size=ftell(infile);
 printf("size : %d\n",size);   
 rewind(infile);
 
 if(size > buffsize)
 { 
  m=size/buffsize;
  if(m==0) m=1; 
 
  d=size-(m*buffsize);  
  printf("\n");
  for(i=0;i<m;i++)
  {  
   fseek(infile,i*buffsize,SEEK_SET);
   fseek(outfile,i*buffsize,SEEK_SET);
   fread(buffer,buffsize,1,infile);    
   fwrite(buffer,buffsize,1,outfile);    
  }//end for
  fseek(infile,m*buffsize,SEEK_SET);
  fseek(outfile,0,SEEK_END);
  fread(buffer,d,1,infile);    
  fwrite(buffer,d,1,outfile); 
 }//end if
 else
 { 
  fread(buffer,size,1,infile);    
  fwrite(buffer,size,1,outfile);
 }//end else
  
 fclose(infile);
 fclose(outfile); 
}//end main




Is This A Good Question/Topic? 0
  • +

Replies To: c/c++ programmer to backup whole hard disk

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: c/c++ programmer to backup whole hard disk

Posted 07 February 2013 - 09:15 PM

What do you mean that is doesn't seem to work?

What indication do you get to indicate it doesn't work?

Do the files open correctly?

Trying to read this special file in user land will probably not produce your desired results. To make a backup of your hard drive you should be traversing the directory structure and copying the individual files to the new destination. If you are really interested in creating a backup of your drives I recommend you start by looking at some of the existing solutions to give yourself an idea of what it takes to do this job.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: c/c++ programmer to backup whole hard disk

Posted 07 February 2013 - 09:26 PM

You can use Linux commands to do this, with a single command line. I have the command in a file, but can't locate it atm.

Google it though, it's out there. Not sure if it works on all distro's, but it works on all the Debian based distro's, for sure: Ubuntu, Mint, and of course, Debian.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: c/c++ programmer to backup whole hard disk

Posted 07 February 2013 - 09:36 PM

Were you thinking of 'dd'? http://en.wikipedia.org/wiki/Dd_(Unix)
Was This Post Helpful? 1
  • +
  • -

#5 <utkarsh>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 13

Re: c/c++ programmer to backup whole hard disk

Posted 15 February 2013 - 02:06 AM

@jim, i also want to have the same file system n same partition table as the of drive m cloning in addition to the data.

when i try to copy drive. sudo ./a.out /dev/sda1 /home/<username>/ex
it ends witout error but dont copy n it however does work on normal text files.

@skydiver, m looking for a similar solution but want to code it in c/c++.

thanks :)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: c/c++ programmer to backup whole hard disk

Posted 15 February 2013 - 07:24 AM

You could look at the code that implements dd. It's open source, and I believe that it's written in C.
Was This Post Helpful? 0
  • +
  • -

#7 <utkarsh>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 13

Re: c/c++ programmer to backup whole hard disk

Posted 16 February 2013 - 11:43 AM

View PostSkydiver, on 15 February 2013 - 07:24 AM, said:

You could look at the code that implements dd. It's open source, and I believe that it's written in C.


@skydiver i hv downloaded the source code but m getting it. :(

is there anyway to copy the file system and partition table of a hard drive??
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: c/c++ programmer to backup whole hard disk

Posted 16 February 2013 - 11:49 AM

Quote

is there anyway to copy the file system and partition table of a hard drive??

Honestly, if I were you I would start with a calculator first. But, by all means, not knowing anything about a file system, or partition table, trying to create a program without any knowledge.... what could go wrong with the data?
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: c/c++ programmer to backup whole hard disk

Posted 16 February 2013 - 11:40 PM

What you are trying to do is a very low level operation. When you write code as you have done you are ultimately making requests to the operating system, which is in complete control of the hardware resources of your system. Your code as it stands cannot image your disk because it cannot see the file system directly; it is seeing a representation of the file system as presented by your OS.

Seeing the harddrive directly as one huge chunk of data to be cloned is actually quite complicated. You need to perform very low level operations that allow you to read the entire surface of the disk. Generally, operating systems themselves do not read the entire surface for a number of reasons.

I wouldn't attempt this right now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1