| 6712 | } |
| 6713 | |
| 6714 | static int clusterManagerCommandBackup(int argc, char **argv) { |
| 6715 | UNUSED(argc); |
| 6716 | int success = 1, port = 0; |
| 6717 | char *ip = NULL; |
| 6718 | if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args; |
| 6719 | clusterManagerNode *refnode = clusterManagerNewNode(ip, port); |
| 6720 | if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0; |
| 6721 | int no_issues = clusterManagerCheckCluster(0); |
| 6722 | int cluster_errors_count = (no_issues ? 0 : |
| 6723 | listLength(cluster_manager.errors)); |
| 6724 | config.cluster_manager_command.backup_dir = argv[1]; |
| 6725 | /* TODO: check if backup_dir is a valid directory. */ |
| 6726 | sds json = sdsnew("[\n"); |
| 6727 | int first_node = 0; |
| 6728 | listIter li; |
| 6729 | listNode *ln; |
| 6730 | listRewind(cluster_manager.nodes, &li); |
| 6731 | while ((ln = listNext(&li)) != NULL) { |
| 6732 | if (!first_node) first_node = 1; |
| 6733 | else json = sdscat(json, ",\n"); |
| 6734 | clusterManagerNode *node = ln->value; |
| 6735 | sds node_json = clusterManagerNodeGetJSON(node, cluster_errors_count); |
| 6736 | json = sdscat(json, node_json); |
| 6737 | sdsfree(node_json); |
| 6738 | if (node->replicate) |
| 6739 | continue; |
| 6740 | clusterManagerLogInfo(">>> Node %s:%d -> Saving RDB...\n", |
| 6741 | node->ip, node->port); |
| 6742 | fflush(stdout); |
| 6743 | getRDB(node); |
| 6744 | } |
| 6745 | json = sdscat(json, "\n]"); |
| 6746 | sds jsonpath = sdsnew(config.cluster_manager_command.backup_dir); |
| 6747 | if (jsonpath[sdslen(jsonpath) - 1] != '/') |
| 6748 | jsonpath = sdscat(jsonpath, "/"); |
| 6749 | jsonpath = sdscat(jsonpath, "nodes.json"); |
| 6750 | fflush(stdout); |
| 6751 | clusterManagerLogInfo("Saving cluster configuration to: %s\n", jsonpath); |
| 6752 | FILE *out = fopen(jsonpath, "w+"); |
| 6753 | if (!out) { |
| 6754 | clusterManagerLogErr("Could not save nodes to: %s\n", jsonpath); |
| 6755 | success = 0; |
| 6756 | goto cleanup; |
| 6757 | } |
| 6758 | fputs(json, out); |
| 6759 | fclose(out); |
| 6760 | cleanup: |
| 6761 | sdsfree(json); |
| 6762 | sdsfree(jsonpath); |
| 6763 | if (success) { |
| 6764 | if (!no_issues) { |
| 6765 | clusterManagerLogWarn("*** Cluster seems to have some problems, " |
| 6766 | "please be aware of it if you're going " |
| 6767 | "to restore this backup.\n"); |
| 6768 | } |
| 6769 | clusterManagerLogOk("[OK] Backup created into: %s\n", |
| 6770 | config.cluster_manager_command.backup_dir); |
| 6771 | } else clusterManagerLogOk("[ERR] Failed to back cluster!\n"); |
nothing calls this directly
no test coverage detected