fix some bugs of ghttp and modify gbackup and grestore
MODIFY
Main/gbackup.cpp
Main/gbackup.cpp
using namespace std;
#define SYSTEM_PATH "data/system/system.nt"
-#define DEFALUT_BACKUP_PATH "./backups"
+#define DEFALUT_BACKUP_PATH "backups"
int copy(string src_path, string dest_path)
{
+ string sys_cmd;
if(!boost::filesystem::exists(src_path)){
//check the source path
cout << "Source Path Error, Please Check It Again!" << endl;
@@ -28,11 +29,11 @@ int copy(string src_path, string dest_path)
}
if(!boost::filesystem::exists(dest_path)){
//check the destnation path
- cout << "Destnation Path Error, Please Check It Again!" << endl;
- return -1;
+ sys_cmd = "mkdir -p ./" + dest_path;
+ system(sys_cmd.c_str());
}
- string sys_cmd;
- sys_cmd = "cp -r " + src_path + ' ' + dest_path ;
+
+ sys_cmd = "cp -r " + src_path + " ./" + dest_path ;
system(sys_cmd.c_str());
return 0;// success
}
@@ -41,13 +42,12 @@ int
main(int argc, char * argv[])
{
Util util;
- string db_name, db_path, backup_path, build_path;
+ string db_name, backup_path;
if(argc < 2)
{
//output help info here
cout << "the usage of gbackup: " << endl;
- cout << "./bin/gbackup your_database_name your_backup_path(optional) your_database_path(optional)" << endl;
- cout << "defalut database_path = ./ " << endl;
+ cout << "./bin/gbackup your_database_name your_backup_path(optional) "<< endl;
cout << "defalut backup_path = ./backups" << endl;
cout << "the path should not include your database's name!" << endl;
return 0;
@@ -55,13 +55,11 @@ main(int argc, char * argv[])
db_name = string(argv[1]);
if(argc > 2)
backup_path = string(argv[2]);
- if(argc > 3)
- build_path = string(argv[3]);
+
cout << "gbackup..." << endl;
{
cout << "argc: " << argc << "\t";
cout << "DB_name:" << db_name << "\t";
- cout << "Build_path: " << db_path << "\t";
cout << "Backup_path: " << backup_path << "\t";
cout << endl;
}
@@ -85,17 +83,30 @@ main(int argc, char * argv[])
//query database_name build_path
//insert database_name backup_path
+ if(backup_path == "") backup_path = DEFALUT_BACKUP_PATH;
+ if(backup_path == "." || backup_path == "./" ){
+ cout << "Backup Path Can not be root, Backup Failed!" << endl;
+ return 0;
+ }
+ if(backup_path[0] == '/') backup_path = '.' + backup_path;
+ if(backup_path[backup_path.length() - 1] == '/') backup_path = backup_path.substr(0, backup_path.length() - 1);
+
+ backup_path = "./" + backup_path;
+ string db_path = db_name + ".db";
+
//copy
- if(db_path == "")
- db_path = db_name + ".db";
if(backup_path == "") backup_path = DEFALUT_BACKUP_PATH;
int ret = copy(db_path, backup_path);
if(ret == 1){
- cout << "Database Path Error, Backup Failed!" << endl;
- }else if(ret == -1){
- cout << "Backup Path Error, Backup Failed!" << endl;
- }else{
+ cout << "Database Name Error, Backup Failed!" << endl;
+ }
+ else{
string time = Util::get_date_time();
+ string timestamp = Util::get_timestamp();
+ backup_path = backup_path + "/" + db_path;
+ string _backup_path = backup_path + "_" + timestamp;
+ string sys_cmd = "mv " + backup_path + " " + _backup_path;
+ system(sys_cmd.c_str());
cout << "Time:" + time << endl;
cout << "DB:" + db_name + " Backup done!" << endl;
}

MODIFY
Main/ghttp.cpp
Main/ghttp.cpp
@@ -108,6 +108,8 @@ bool backup_handler(const HttpServer& server, const shared_ptr<HttpServer::Respo
bool restore_handler(const HttpServer& server, const shared_ptr<HttpServer::Response>& response, const shared_ptr<HttpServer::Request>& request, string RequestType);
+bool auto_backup_handler(const HttpServer& server, const shared_ptr<HttpServer::Response>& response, const shared_ptr<HttpServer::Request>& request, string RequestType);
+
bool getCoreVersion_handler(const HttpServer& server, const shared_ptr<HttpServer::Response>& response, const shared_ptr<HttpServer::Request>& request, string RequestType);
bool getAPIVersion_handler(const HttpServer& server, const shared_ptr<HttpServer::Response>& response, const shared_ptr<HttpServer::Request>& request, string RequestType);
@@ -3966,7 +3968,7 @@ void user_thread(const shared_ptr<HttpServer::Response>& response, const shared_
pthread_rwlock_unlock(&users_map_lock);
}
- else if(type == "add_query" || type == "add_load" || type == "add_unload" || type == "add_update" || type == "add_backup" || type == "add_restore" || "add_export")
+ else if(type == "add_query" || type == "add_load" || type == "add_unload" || type == "add_update" || type == "add_backup" || type == "add_restore" || type == "add_export")
{
if(username2 == ROOT_USERNAME)
{
@@ -4448,6 +4450,11 @@ void showUser_thread(const shared_ptr<HttpServer::Response>& response, const sha
_restore_db.SetString(restore_db.c_str(), restore_db.length(), allocator);
obj.AddMember("restore privilege", _restore_db, allocator);
+ string export_db = it->second->getexport();
+ Value _export_db;
+ _export_db.SetString(export_db.c_str(), export_db.length(), allocator);
+ obj.AddMember("export privilege", _export_db, allocator);
+
json_array.PushBack(obj, allocator);
}
resDoc.AddMember("ResponseBody", json_array, allocator);
@@ -4805,6 +4812,9 @@ void DB2Map()
}else if(type == "has_backup_priv")
{
user->backup_priv.insert(db_name);
+ }else if(type == "has_export_priv")
+ {
+ user->export_priv.insert(db_name);
}
}
//users.insert(pair<std::string, struct User*>(username, &user));
@@ -5302,7 +5312,9 @@ void restore_thread(const shared_ptr<HttpServer::Response>& response, const shar
return;
}
- //check if database named [db_name] is already build.
+ if(path[0] == '/') path = '.' + path;
+ if(path[path.length() - 1] == '/') path = path.substr(0, path.length()-1);
+
if(!boost::filesystem::exists(path)){
string error = "Backup Path Error, Restore Failed";
string resJson = CreateJson(307, error, 0);
@@ -5325,7 +5337,7 @@ void restore_thread(const shared_ptr<HttpServer::Response>& response, const shar
pthread_rwlock_unlock(&already_build_map_lock);
return;
}
- if(addPrivilege(username, "query", db_name) == 0 || addPrivilege(username, "load", db_name) == 0 || addPrivilege(username, "unload", db_name) == 0 || addPrivilege(username, "backup", db_name) == 0 || addPrivilege(username, "restore", db_name) == 0)
+ if(addPrivilege(username, "query", db_name) == 0 || addPrivilege(username, "load", db_name) == 0 || addPrivilege(username, "unload", db_name) == 0 || addPrivilege(username, "backup", db_name) == 0 || addPrivilege(username, "restore", db_name) == 0 || addPrivilege(username, "export", db_name) == 0)
{
string error = "add query or load or unload or backup or restore privilege failed.";
string resJson = CreateJson(912, error, 0);
@@ -5359,6 +5371,8 @@ void restore_thread(const shared_ptr<HttpServer::Response>& response, const shar
return;
}
//restore
+ string sys_cmd = "rm -rf " + db_name + ".db";
+ system(sys_cmd.c_str());
pthread_rwlock_wrlock(&databases_map_lock);
int ret = copy(path, DB_PATH);
@@ -5387,10 +5401,9 @@ void restore_thread(const shared_ptr<HttpServer::Response>& response, const shar
pthread_rwlock_unlock(&(it_already_build->second->db_lock));
path = Util::get_folder_name(path, db_name);
- string sys_cmd = "mv " + path + " " + db_name + ".db";
+ sys_cmd = "mv " + path + " " + db_name + ".db";
system(sys_cmd.c_str());
-
cout << "database restore done." << endl;
string success = "Database restore successfully.";
string resJson = CreateJson(0, success, 0);
@@ -5405,3 +5418,9 @@ bool restore_handler(const HttpServer& server, const shared_ptr<HttpServer::Resp
t.detach();
return true;
}
+
+
+bool auto_backup_handler(const HttpServer& server, const shared_ptr<HttpServer::Response>& response, const shared_ptr<HttpServer::Request>& request, string RequestType)
+{
+
+}
\ No newline at end of file

MODIFY
Main/grestore.cpp
Main/grestore.cpp
@@ -38,30 +38,25 @@ int
main(int argc, char * argv[])
{
Util util;
- string db_name, backup_path, db_path;
+ string db_name, backup_path, db_path, path;
if(argc < 2)
{
//output help info here
cout << "the usage of grestore: " << endl;
- cout << "./bin/restore your_database_name your_backup_path(optional) your_database_path(optional)" << endl;
- cout << "defalut backup_path = ./backups" << endl;
- cout << "defalut database_path = ./ " << endl;
- cout << "the path should not include your database's name!" << endl;
+ cout << "./bin/restore your_database_name your_backup_path" << endl;
+ cout << "the path should include your database folder name!" << endl;
return 0;
}
db_name = string(argv[1]);
if(argc > 2)
backup_path = string(argv[2]);
- if(argc > 3)
- db_path = string(argv[3]);
cout << "gbackup..." << endl;
{
cout << "argc: " << argc << "\t";
cout << "DB_name:" << db_name << "\t";
- cout << "Build_path: " << db_path << "\t";
cout << "Backup_path: " << backup_path << "\t";
cout << endl;
}
@@ -80,35 +75,65 @@ main(int argc, char * argv[])
return -1;
}
-//TODO1: We need a column in system.db :
-//last restore time;
-//TODO2:
-//We can restore a deleted database
-//if we insert the new database's info into the system.db
-//then we restore the files and we rebuild it
+ if(backup_path[0] == '/') backup_path = '.' + backup_path;
+ if(backup_path[backup_path.length() - 1] == '/') backup_path = backup_path.substr(0, backup_path.length()-1);
- //copy
+ if(!boost::filesystem::exists(backup_path)){
+ cout << "Backup Path Error, Restore Failed" << endl;
+ return 0;
+ }
- if(backup_path == "")
- backup_path = DEFALUT_BACKUP_PATH;
- backup_path = backup_path + "/" + db_name + ".db";
- if(db_path == "") db_path = DEFALUT_BUILD_PATH;
+ //system.db
+ Database system_db("system");
+ system_db.load();
- //delete
- string _db_path = db_path + "/" + db_name + ".db";
- string sys_cmd = "rm -rf " + _db_path;
- system(sys_cmd.c_str());
+ string sparql = "ASK WHERE{<" + db_name + "> <database_status> \"already_built\".}";
+ ResultSet ask_rs;
+ FILE* ask_ofp = stdout;
+ system_db.query(sparql, ask_rs, ask_ofp);
+ if (ask_rs.answer[0][0] == "false")
+ {
+ cout << "The database does not exist. Rebuild" << endl;
+ string time = Util::get_backup_time(backup_path, db_name);
+ if(time.size() == 0){
+ cout << "Backup Path Does not Match DataBase Name, Restore Failed" << endl;
+ return 0;
+ }
+ string sparql = "INSERT DATA {<" + db_name + "> <database_status> \"already_built\"." + "<" + db_name + "> <built_by> <root>."
+ + "<" + db_name + "> <built_time> \"" + time + "\".}";
+ ResultSet _rs;
+ FILE* ofp = stdout;
+ string msg;
+ int ret = system_db.query(sparql, _rs, ofp);
+
+ if (ret >= 0)
+ msg = "update num : " + Util::int2string(ret);
+ else {
+ //update error
+ cout << "Rebuild Error, Restore Failed" << endl;
+ return 0;
+ }
+ cout << msg << endl;
+ }
+
+ int ret = copy(backup_path, DEFALUT_BUILD_PATH);
- int ret = copy(backup_path, db_path);
if(ret == 1){
cout << "Backup Path Error, Restore Failed!" << endl;
- }else if(ret == -1){
- cout << "Database Path Error, Restore Failed!" << endl;
}else{
//TODO update the in system.db
string time = Util::get_date_time();
cout << "Time:" + time << endl;
cout << "DB:" + db_name + " Restore done!" << endl;
}
+
+ db_path = db_name + ".db";
+ string sys_cmd = "rm -rf " + db_path;
+ system(sys_cmd.c_str());
+
+ path = Util::get_folder_name(backup_path, db_name);
+ sys_cmd = "mv " + path + ' ' + db_path;
+ system(sys_cmd.c_str());
+
return 0;
}
\ No newline at end of file

fix some bugs of ghttp and modify gbackup and grestore