-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackup.cpp
More file actions
104 lines (87 loc) · 2.1 KB
/
backup.cpp
File metadata and controls
104 lines (87 loc) · 2.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <bits/stdc++.h>
#include<unistd.h>
#include<sys/wait.h>
using namespace std;
int runcmd()
{
char* argv[100];
pid_t child_pid,tpid;
int child_status;
child_pid = fork();
if(child_pid == 0) {
execlp("python3", "python3", "backupclient.py", (char*) NULL);
printf("Unknown command\n");
exit(0);
}
else {
do {
tpid = wait(&child_status);
if(tpid != child_pid) WIFEXITED(tpid);
} while(tpid != child_pid);
return child_status;
}
}
string exec(string command,map<string,string> &m) {
char buffer[128];
string result = "";
ofstream mapout;
mapout.open("maplog.txt", std::ios_base::app);
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
return "popen failed!";
}
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
{
string s(buffer);
size_t found = s.find("\t");
string temp=s.substr(found+1);
if(m.find(s)==m.end()){
mapout << s << temp;
char stream[temp.length()+1];
strcpy(stream, temp.c_str());
m[s]=temp;
result += temp;
}
}
}
pclose(pipe);
return result;
}
int main() {
map<string,string> m;
ifstream mapin, config;
config.open("config");
mapin.open("maplog.txt");
string key,val,temp;
while(getline(mapin, temp)){
key=temp;
key.append("\n");
getline(mapin, temp);
val=temp;
m[key]=val;
}
vector<string> backupdir;
while(getline(config,temp)){
backupdir.push_back(temp);
}
string ls;
while(1){
for(int i=0;i<backupdir.size();i++){
temp="find "+backupdir[i]+" "+ "-name '*' -printf '%TY-%Tm-%Td %TT %Tz\t%p\t%k\n'";
ls = exec(temp,m);
cout << ls;
ofstream fout;
fout.open("out.txt");
fout<<ls;
fout.close();
runcmd();
}
sleep(10);
cout<<"Waiting for changes in file to send\n";
}
}