Skip to content

Commit

Permalink
Support Rsync sync with password (OpenAtomFoundation#600)
Browse files Browse the repository at this point in the history
* Support Rsync sync with password
  • Loading branch information
whoiami authored and Axlgrep committed May 15, 2019
1 parent 9d586b3 commit 9b78f23
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
2 changes: 2 additions & 0 deletions include/pika_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const int kPortShiftRSync = 1000;
const int kPortShiftReplServer = 2000;

const std::string kPikaPidFile = "pika.pid";
const std::string kPikaSecretFile = "rsync.secret";
const std::string kDefaultRsyncAuth = "default";

struct WorkerCronTask {
int task;
Expand Down
1 change: 1 addition & 0 deletions include/pika_rsync_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PikaRsyncService {
int ListenPort();

private:
int CreateSecretFile();
std::string raw_path_;
std::string rsync_path_;
std::string pid_path_;
Expand Down
49 changes: 47 additions & 2 deletions src/pika_rsync_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
#include "include/pika_rsync_service.h"

#include <glog/logging.h>
#include <fstream>

#include "slash/include/env.h"
#include "slash/include/rsync.h"

#include "include/pika_define.h"
#include "include/pika_conf.h"

extern PikaConf *g_pika_conf;

PikaRsyncService::PikaRsyncService(const std::string& raw_path,
const int port)
Expand All @@ -33,12 +37,21 @@ PikaRsyncService::~PikaRsyncService() {

int PikaRsyncService::StartRsync() {
int ret = 0;
ret = slash::StartRsync(raw_path_, kDBSyncModule, "0.0.0.0", port_);
std::string auth;
if (g_pika_conf->masterauth().empty()) {
auth = kDefaultRsyncAuth;
} else {
auth = g_pika_conf->masterauth();
}
ret = slash::StartRsync(raw_path_, kDBSyncModule, "0.0.0.0", port_, auth);
if (ret != 0) {
LOG(WARNING) << "Failed to start rsync, path:" << raw_path_ << " error : " << ret;
return -1;
}

ret = CreateSecretFile();
if (ret != 0) {
LOG(WARNING) << "Failed to create secret file";
}
// Make sure the listening addr of rsyncd is accessible, avoid the corner case
// that rsync --daemon process is started but not finished listening on the socket
sleep(1);
Expand All @@ -50,6 +63,38 @@ int PikaRsyncService::StartRsync() {
return 0;
}

int PikaRsyncService::CreateSecretFile() {
std::string secret_file_path = g_pika_conf->db_sync_path();
if (g_pika_conf->db_sync_path().back() != '/') {
secret_file_path += "/";
}
secret_file_path += slash::kRsyncSubDir + "/";
slash::CreatePath(secret_file_path);
secret_file_path += kPikaSecretFile;

std::string auth;
if (g_pika_conf->requirepass().empty()) {
auth = kDefaultRsyncAuth;
} else {
auth = g_pika_conf->requirepass();
}

std::ofstream secret_stream(secret_file_path.c_str());
if (!secret_stream) {
return -1;
}
secret_stream << auth;
secret_stream.close();

// secret file cant be other-accessible
std::string cmd = "chmod 600 " + secret_file_path;
int ret = system(cmd.c_str());
if (ret == 0 || (WIFEXITED(ret) && !WEXITSTATUS(ret))) {
return 0;
}
return ret;
}

bool PikaRsyncService::CheckRsyncAlive() {
return slash::FileExists(pid_path_);
}
Expand Down
25 changes: 14 additions & 11 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,12 @@ void PikaServer::DbSyncSendFile(const std::string& ip, int port,
std::string remote_path = g_pika_conf->classic_mode() ? table_name : table_name + "/" + std::to_string(partition_id);
std::vector<std::string>::const_iterator iter = descendant.begin();
slash::RsyncRemote remote(ip, port, kDBSyncModule, g_pika_conf->db_sync_speed() * 1024);
std::string secret_file_path = g_pika_conf->db_sync_path();
if (g_pika_conf->db_sync_path().back() != '/') {
secret_file_path += "/";
}
secret_file_path += slash::kRsyncSubDir + "/" + kPikaSecretFile;

for (; iter != descendant.end(); ++iter) {
local_path = bg_path + "/" + *iter;
target_path = remote_path + "/" + *iter;
Expand All @@ -925,8 +931,7 @@ void PikaServer::DbSyncSendFile(const std::string& ip, int port,
}

// We need specify the speed limit for every single file
ret = slash::RsyncSendFile(local_path, target_path, remote);

ret = slash::RsyncSendFile(local_path, target_path, secret_file_path, remote);
if (0 != ret) {
LOG(WARNING) << "Partition: " << partition->GetPartitionName()
<< " RSync send file failed! From: " << *iter
Expand All @@ -936,13 +941,12 @@ void PikaServer::DbSyncSendFile(const std::string& ip, int port,
break;
}
}

// Clear target path
slash::RsyncSendClearTarget(bg_path + "/strings", remote_path + "/strings", remote);
slash::RsyncSendClearTarget(bg_path + "/hashes", remote_path + "/hashes", remote);
slash::RsyncSendClearTarget(bg_path + "/lists", remote_path + "/lists", remote);
slash::RsyncSendClearTarget(bg_path + "/sets", remote_path + "/sets", remote);
slash::RsyncSendClearTarget(bg_path + "/zsets", remote_path + "/zsets", remote);
slash::RsyncSendClearTarget(bg_path + "/strings", remote_path + "/strings", secret_file_path, remote);
slash::RsyncSendClearTarget(bg_path + "/hashes", remote_path + "/hashes", secret_file_path, remote);
slash::RsyncSendClearTarget(bg_path + "/lists", remote_path + "/lists", secret_file_path, remote);
slash::RsyncSendClearTarget(bg_path + "/sets", remote_path + "/sets", secret_file_path, remote);
slash::RsyncSendClearTarget(bg_path + "/zsets", remote_path + "/zsets", secret_file_path, remote);

pink::PinkCli* cli = pink::NewRedisCli();
std::string lip(host_);
Expand Down Expand Up @@ -970,16 +974,15 @@ void PikaServer::DbSyncSendFile(const std::string& ip, int port,
fix << "0s\n" << lip << "\n" << port_ << "\n" << binlog_filenum << "\n" << binlog_offset << "\n";
fix.close();
}
ret = slash::RsyncSendFile(fn, remote_path + "/" + kBgsaveInfoFile, remote);
ret = slash::RsyncSendFile(fn, remote_path + "/" + kBgsaveInfoFile, secret_file_path, remote);
slash::DeleteFile(fn);
if (ret != 0) {
LOG(WARNING) << "Partition: " << partition->GetPartitionName() << " Send Modified Info File Failed";
}
} else if (0 != (ret = slash::RsyncSendFile(bg_path + "/" + kBgsaveInfoFile, remote_path + "/" + kBgsaveInfoFile, remote))) {
} else if (0 != (ret = slash::RsyncSendFile(bg_path + "/" + kBgsaveInfoFile, remote_path + "/" + kBgsaveInfoFile, secret_file_path, remote))) {
LOG(WARNING) << "Partition: " << partition->GetPartitionName() << " Send Info File Failed";
}
}

// remove slave
{
std::string task_index =
Expand Down
2 changes: 1 addition & 1 deletion third/slash

0 comments on commit 9b78f23

Please sign in to comment.