# BackupHelper using C - helps to take your backups easily



## uniquerockrz (Jun 1, 2011)

This is a simple program I wrote using C because I was too lazy to see individual folders to take my backup. Most backup program allow you to sync your hard disk with external rewritable drive, such as pen drives, external hard drive or a upload server. This one will help you to sync your drive (or a part of it) with non-rewriteable media such as DVDs which are cheapest for taking backups.

Using this program, you can:

Check what files you need to add in your backup drive
Check what files have been modified since the last backup
Know if two directory structure are synced or not.
Check what files have been deleted since the last backup

The program works with the help of two input files, an old filelist text file and a new file list text file. This would work in Linux systems (I have tested it individually), sorry I dont know about windows as i dont have that installed.  8) 

Copy and save the program as BackupHelper.c, compile the program using gcc using the following code

```
$ gcc -Wall BackupHelper.c -o BackupHelper
```

Now, first take a backup of your hard disk (or a folder in it, say music) in a DVD. Now cd to that folder and create a filelist of that folder and save it in any name (say a.txt)


```
$ cd music
$ tree -fis > a.txt
```

Copy this file in the folder the program executable is present.
Note: If you dont use the -s option, the program wont check for file modifications or changes, it will only check for content added.

Now after some days (maybe when you have added more content in your music folder) cd to the folder you want to take the backup. Again create a filelist of that folder using the following commands.

```
$ cd music
$ tree -fis > b.txt
```

Copy the b.txt to the program's executable folder. Now you need to compare the both the files to see the changes in music folder. For that, type ./BackupHelper oldfilelist.txt newfilelist.txt and press enter.

```
$ ./BackupHelper a.txt b.txt
```

The program would display all the files and folders that have been added after your last backup. Now you can burn the changed or added files in your backup dvd.

*img10.imageshack.us/img10/2867/screenshot3id.png

Uploaded with ImageShack.us

Delete the old filelist file. Keep the new. Next time you want to take backup, cd to the folder and create the filelist file using tree -fis >filelist.txt command. Compare the older file with new one and you are done.

Please note: This program doesnt takes your backups, but rather helps you to, so the name is BackupHelper. For encryption or more advanced options, you can use deja dup. However you have to write another DVD everytime you backup, thus increasing file-redundancy and costs. This will help you keeping the directories synced, the backup, however, you have to do manually.

If no changes have been made since the last backup, the program would display a message saying so. It also displays the files that were present earlier but have been recently deleted. You can also use it to know if two folders are synced.


```
// Program BackupHelper.c
#include <stdio.h>
char oldfile[10000][200], newfile[10000][200]; // stores the file content
int nold, nnew; // number of files in each file list
int main(int argc, char *argv[]){
	int copytoarray(char filename[100], char arr[10000][200]);
	void comparefiles();
	// copying the file content in an array
	nold=copytoarray(argv[1], oldfile);
	nnew=copytoarray(argv[2], newfile);
	printf("\n");
	// comparing the files and displaying the differences
	comparefiles(newfile, oldfile);
	return 0;
}
int copytoarray(char filename[100], char arr[10000][200]){
	FILE *fp;
	int i,n;
	fp=fopen(filename,"r");
	i=0;
	while(fgets(arr[i],200,fp)!=NULL)
		i++;
	n=i;
	return n;
}
void comparefiles(){
	int i,j,n=0;
	int comparestr(char str1[200], char str2[200]);
	for(i=0,j=0;i<(nnew-1);){
		if(j>(nold-1)){
			n++;
			printf("%d Changed/Added %s\n",n, newfile[i]);
			i++;
			j=0;
		}
		if(comparestr(newfile[i], oldfile[j])){
			i++;
			oldfile[j][0]='\0';
			j=0;
		}
		else j++;
	}
	if(n==0)
		printf("Backup is up to date! No notable difference found.\n");
	for(i=0;i<(nold-2);i++){
		if(oldfile[i][0]!='\0'){
			n++;
			printf("%d Deleted : %s\n", n, oldfile[i]);
		}
	}
}
int comparestr(char str1[200],char str2[200]){
	int i;
	if(str2[0]=='\0'){
		return 0;
	}
	else{
		for(i=0;str1[i]!='\n';i++){
			if(str1[i]!=str2[i])
				return 0;
			}
		}
	return 1;
}
```


----------



## sygeek (Jun 2, 2011)

Seems interesting and simple, I'll try it soon. However I guess there may be other softwares that serves this purpose too..


----------

