-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_backup.cpp
More file actions
124 lines (103 loc) · 4.37 KB
/
Copy pathsync_backup.cpp
File metadata and controls
124 lines (103 loc) · 4.37 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <string>
// Function to handle the Git synchronization
void sync_to_git() {
// 1. Get current date for the commit message (d/m/y)
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
char date_buffer[20];
std::strftime(date_buffer, sizeof(date_buffer), "%d/%m/%Y", &tm);
std::string commit_msg = "sync " + std::string(date_buffer) + " d/m/y";
// 2. Chain the commands: add -> commit -> push
// We use '&&' so it only proceeds if the previous command succeeded
std::string git_command =
"git add . && git commit -m \"" + commit_msg + "\" && git push";
std::cout << "\n🚀 Starting Git Sync..." << std::endl;
std::cout << "Executing: " << git_command << std::endl;
int result = std::system(git_command.c_str());
if (result == 0) {
std::cout << "✅ Git sync complete!" << std::endl;
} else {
std::cerr << "❌ Git sync failed." << std::endl;
}
}
// Function to safely execute the rsync command
int execute_rsync(const std::string &source, const std::string &destination) {
// -a: Archive mode
// -v: Verbose output
// -z: Compress
const std::string rsync_options = "-avz --filter=':- gitignore'";
// Construct the full command: rsync [options] [source_file]
// [destination_dir/new_filename]
const std::string command =
"rsync " + rsync_options + " " + source + " " + destination;
std::cout << "Executing: " << command << std::endl;
int result = std::system(command.c_str());
if (result == 0) {
std::cout << " ✅ Success." << std::endl;
} else {
std::cerr << " ❌ Failed with exit status: " << result << std::endl;
}
std::cout << "---" << std::endl;
return result;
}
int main() {
// 1. Define the final, single destination directory
// Replace this with your actual destination path!
const std::string final_destination_dir = "/home/munmun06/backup/";
const std::string secure_destination_dir = "/home/munmun06/share/";
// 2. Define the list of source files and their new names in the destination
// Key (string): Full path to the source file.
// Value (string): The desired new filename in the destination directory.
std::map<std::string, std::string> files_to_copy = {
{"/home/munmun06/installed.txt", "installed.txt"},
{"/home/munmun06/explicit-installed.txt", "explicit-installed.txt"},
{"/home/munmun06/SysMainRoutine", "SysMainRoutine"},
{"/home/munmun06/scripts/", "scripts/"},
{"/home/munmun06/passwords.txt.gpg", "passwords.txt.gpg"},
{"/home/munmun06/personal/git-stuff/git_convention", "git_convention"},
{"/home/munmun06/My_programming_stuff/learning/", "learning/"},
{"/home/munmun06/SaveBattery.txt", "SaveBattery.txt"},
{"/home/munmun06/Wifi.txt", "Wifi.txt"},
{"/home/munmun06/.config/hypr/", "hypr/"},
{"/home/munmun06/.config/hyprlock/layout.sh", "hyprlock/layout.sh"},
{"/home/munmun06/.config/kitty/", "kitty/"},
{"/home/munmun06/.config/nvim/", "nvim/"},
{"/home/munmun06/.config/waybar/", "waybar/"},
{"/home/munmun06/.config/fastfetch/", "fastfetch/"},
{"/etc/nftables.conf", "nftables.conf"},
{"/home/munmun06/.tmux.conf", "tmux.conf"}};
int overall_status = 0; // Tracks if any copy failed
// 3. Iterate through the map and execute rsync for each file
for (const auto &pair : files_to_copy) {
const std::string &source_file = pair.first;
const std::string &new_filename = pair.second;
std::string full_destination_path;
// Combine the final destination directory with the new filename
if (source_file == "/home/munmun06/passwords.txt.gpg") {
full_destination_path = secure_destination_dir + new_filename;
std::cout << "🔐 Secure file detected. Redirecting to vault..." << std::endl;
} else {
full_destination_path = final_destination_dir + new_filename;
}
// Execute the copy operation
int result = execute_rsync(source_file, full_destination_path);
// If any single copy fails, update the overall status
if (result != 0) {
overall_status = 1;
}
}
if (overall_status == 0) {
std::cout << "🎉 All sync operations complete."
<< std::endl;
sync_to_git();
} else {
std::cerr
<< "⚠️ One or more rsync operations failed. Check the errors above."
<< std::endl;
}
return overall_status;
}