-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource3.cpp
More file actions
161 lines (139 loc) · 5.93 KB
/
source3.cpp
File metadata and controls
161 lines (139 loc) · 5.93 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <filesystem>
#include <chrono>
#include <thread>
#include <string>
#include <vector>
#include <atomic>
#include <csignal>
#include <iomanip>
#include <sstream>
// Use the filesystem namespace for brevity
namespace fs = std::filesystem;
// --- Globals for Graceful Shutdown ---
std::atomic<bool> keepRunning{ true };
void signalHandler(int) {
keepRunning = false;
}
// --- Function Prototypes ---
// Prints a message to the console with a timestamp.
void logMessage(const std::string& message);
// Main backup logic function.
void runBackup(const fs::path& sourceDir, const fs::path& backupDir, const fs::path& deletedDir);
// --- Main Program ---
int main() {
// Define the paths for your source, backup, and deleted directories.
// Make sure to use forward slashes or double backslashes for paths.
fs::path sourcePath = "E:/Misc";
fs::path backupPath = "D:/Backup/misc";
fs::path deletedPath = "D:/Backup/deleted";
// Register signal handler for graceful shutdown
std::signal(SIGINT, signalHandler);
// --- Initial Setup ---
// Create the backup and deleted directories if they don't already exist.
try {
if (!fs::exists(backupPath)) {
fs::create_directories(backupPath);
logMessage("Created backup directory: " + backupPath.string());
}
if (!fs::exists(deletedPath)) {
fs::create_directories(deletedPath);
logMessage("Created deleted items directory: " + deletedPath.string());
}
}
catch (const fs::filesystem_error& e) {
logMessage("Error creating directories: " + std::string(e.what()));
return 1; // Exit if we can't create directories
}
// --- Main Loop ---
// This loop runs indefinitely, triggering the backup process every hour.
while (keepRunning) {
logMessage("Starting file check...");
runBackup(sourcePath, backupPath, deletedPath);
logMessage("Check complete. Waiting for the next trigger in 1 hour...");
// Wait for one hour before the next cycle, checking for shutdown signal.
for (int i = 0; i < 3600 && keepRunning; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
logMessage("Backup utility terminated by user.");
return 0;
}
// --- Function Definitions ---
/**
* @brief Prints a message to the console with a timestamp.
* @param message The message to display.
*/
void logMessage(const std::string& message) {
// Get the current time for logging purposes
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
std::tm tm;
#ifdef _WIN32
localtime_s(&tm, ¤tTime);
#else
localtime_r(¤tTime, &tm);
#endif
std::ostringstream oss;
oss << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "] " << message;
std::cout << oss.str() << std::endl;
}
/**
* @brief Runs the backup process by comparing files between source and backup directories.
* @param sourceDir The directory to back up from.
* @param backupDir The directory to back up to.
* @param deletedDir The directory to move old backups to.
*/
void runBackup(const fs::path& sourceDir, const fs::path& backupDir, const fs::path& deletedDir) {
try {
// Iterate over each item in the source directory recursively.
for (const auto& entry : fs::recursive_directory_iterator(sourceDir)) {
if (!fs::is_regular_file(entry.path())) continue;
// Get the relative path of the file within the source directory.
fs::path relPath = fs::relative(entry.path(), sourceDir);
// Construct the corresponding paths in the backup and deleted directories.
fs::path backupFile = backupDir / relPath;
fs::path deletedFile = deletedDir / relPath;
// Ensure parent directories exist
fs::create_directories(backupFile.parent_path());
fs::create_directories(deletedFile.parent_path());
if (!fs::exists(backupFile)) {
// --- Case 1: File is new ---
// The file does not exist in the backup, so copy it.
fs::copy_file(entry.path(), backupFile, fs::copy_options::overwrite_existing);
logMessage("Copied new file: " + relPath.string());
}
else {
// --- Case 2: File already exists ---
// Check if the file sizes are different.
if (fs::file_size(entry.path()) != fs::file_size(backupFile)) {
// The file has been modified.
// Append timestamp to avoid overwriting in deletedDir
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::ostringstream ts;
std::tm tm;
#ifdef _WIN32
localtime_s(&tm, &t);
#else
std::tm* tmPtr = std::localtime(&t);
tm = *tmPtr;
#endif
ts << std::put_time(&tm, "_%Y%m%d%H%M%S");
fs::path deletedFileWithTimestamp = deletedFile;
deletedFileWithTimestamp += ts.str();
// Move the old backup to the deleted folder with a timestamp.
fs::rename(backupFile, deletedFileWithTimestamp);
logMessage("Moved outdated backup: " + backupFile.string() + " -> " + deletedFileWithTimestamp.string());
// Copy the new file to the backup directory.
fs::copy_file(entry.path(), backupFile, fs::copy_options::overwrite_existing);
logMessage("Copied updated file: " + relPath.string());
}
// If sizes are the same, do nothing.
}
}
}
catch (const fs::filesystem_error& e) {
logMessage("An error occurred during backup: " + std::string(e.what()));
}
}