-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScripts.hpp
90 lines (72 loc) · 3.25 KB
/
Scripts.hpp
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
#include <fstream>
#include <iostream>
static bool defaultPath = true;
static char inputW[64] = "/путь/до/";
// Создание процесса для автоматического бекапа
static void createProcess() {
system("cp /usr/share/backup_app/config/hello.service /lib/systemd/system/hello.service");
system("systemctl stop hello.service");
system("systemctl enable hello.service");
system("systemctl start hello.service");
}
// Записывание переменных в конфиге
static void writeToConfig(int dur_s) {
std::ofstream dir_w("/usr/share/backup_app/config/dir_w.txt", std::ios::in | std::ios::trunc);
std::ofstream dur("/usr/share/backup_app/config/dur.txt", std::ios::in | std::ios::trunc);
std::ofstream last("/usr/share/backup_app/config/last.txt", std::ios::in | std::ios::trunc);
if(defaultPath == true) { dir_w << "/usr/share/backup_app"; }
else { dir_w << inputW; }
dur << dur_s;
last << time(NULL);
}
void startNow() {
std::ifstream dir_w("/usr/share/backup_app/config/dir_w.txt", std::ios::out);
std::ifstream name("/usr/share/backup_app/config/name.txt", std::ios::out);
std::string n, w;
std::getline(dir_w, w);
std::string ww = "-w " + w + " ";
std::getline(name, n);
std::string nn = "-n " + n + " ";
std::cout << w << "/" << n << ".tar.zst" << std::endl;
system((std::string("/usr/share/backup_app/copy.sh ") + ww + nn).c_str());
std::ofstream last_re("/usr/share/backup_app/config/last.txt", std::ios::in | std::ios::trunc);
if(!last_re) {std::cout << "Error\n";}
last_re << time(NULL);
last_re.close();
std::cout << w << n << std::endl;
std::ifstream file(w + "/" + n + ".tar.zst",std::ios::binary | std::ios::ate);
if (!file) {
std::cout << "Ошибка открытия файла!" << std::endl;
}
std::streampos filesize = file.tellg();
file.close();
filesize = filesize / (1024.0);
std::cout << "Размер файла " << " : " << filesize << " Кбайт" << std::endl;
char notification[120];
sprintf(notification, "notify-send Успешно 'Размер копии %d кбайт'", filesize);
system(notification);
}
void startWithDur() {
while (true) {
std::ifstream dur("/usr/share/backup_app/config/dur.txt", std::ios::out);
std::ifstream last("/usr/share/backup_app/config/last.txt", std::ios::out);
std::ifstream dir_w("/usr/share/backup_app/config/dir_w.txt", std::ios::out);
std::ifstream name("/usr/share/backup_app/config/name.txt", std::ios::out);
std::string w, d, l, n;
std::getline(dir_w, w);
std::string ww = "-w " + w + " ";
std::getline(dur, d);
std::getline(last, l);
std::getline(name, n);
std::string nn = "-n " + n + " ";
// Настало ли время для бекап
if(stoi(d) + stoi(l) <= time(NULL)) {
system((std::string("/usr/share/backup_app/copy.sh ") + ww + nn).c_str());
std::ofstream last_re("/usr/share/backup_app/config/last.txt", std::ios::in | std::ios::trunc);
if(!last_re) {std::cout << "Error\n";}
last_re << time(NULL);
last_re.close();
system("notify-send hello '123'");
}
}
}