-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
81 lines (72 loc) · 2.15 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* long ass file paths : now covered !*/
#define BUFFER_SIZE 2048
#ifndef BACKUP_INTERVAL
#define BACKUP_INTERVAL 1800
#endif
char** read_backupfile(size_t *num_lines) {
FILE *file;
char buffer[BUFFER_SIZE];
char **lines = NULL;
size_t lines_count = 0;
file = fopen("backupfile.txt", "r");
if (file == NULL) {
perror("backupfile.txt not found!");
return NULL;
}
while(fgets(buffer, BUFFER_SIZE, file) != NULL) {
buffer[strcspn(buffer, "\n")] = '\0';
char *line = malloc(strlen(buffer) + 1);
if(line == NULL) {
perror("mem alloc failed!");
fclose(file);
return NULL;
}
strcpy(line, buffer);
char **new_lines = realloc(lines, (lines_count + 1) * sizeof(char *));
if (new_lines == NULL) {
perror("mem alloc failed!");
free(line);
fclose(file);
return NULL;
}
lines = new_lines;
lines[lines_count] = line;
lines_count++;
}
fclose(file);
*num_lines =lines_count;
return lines;
}
int main(int argc, char* argv[]) {
while (1) {
size_t num_lines = 0;
char **file_content = read_backupfile(&num_lines);
if (argc < 2) {
printf("no backup path provided!\nusage: ./shittybkp /path/to/backup_directory\n");
return 1;
}
printf("destination: %s\n", argv[1]);
if (file_content != NULL) {
for (size_t i = 0; i < num_lines; i++) {
size_t command_length = snprintf(NULL, 0, "rsync -azP %s %s", file_content[i] , argv[1]) + 1;
char* _command = malloc(command_length);
snprintf(_command, command_length, "rsync --delete -azP %s %s", file_content[i], argv[1]);
printf("command: %s\n", _command);
int result = system(_command);
if (result != 0) {
fprintf(stderr, "Error: rsync failed with status %d\n", result);
} else {
printf("Backup completed successfully.\n");
}
free(file_content[i]);
}
free(file_content);
}
sleep(BACKUP_INTERVAL);
}
return 0;
}