| 5802 | } |
| 5803 | |
| 5804 | static int clusterManagerCommandBackup(int argc, char **argv) { |
| 5805 | UNUSED(argc); |
| 5806 | int success = 1, port = 0; |
| 5807 | char *ip = NULL; |
| 5808 | if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args; |
| 5809 | clusterManagerNode *refnode = clusterManagerNewNode(ip, port); |
| 5810 | if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0; |
| 5811 | int no_issues = clusterManagerCheckCluster(0); |
| 5812 | int cluster_errors_count = (no_issues ? 0 : |
| 5813 | listLength(cluster_manager.errors)); |
| 5814 | config.cluster_manager_command.backup_dir = argv[1]; |
| 5815 | /* TODO: check if backup_dir is a valid directory. */ |
| 5816 | sds json = sdsnew("[\n"); |
| 5817 | int first_node = 0; |
| 5818 | listIter li; |
| 5819 | listNode *ln; |
| 5820 | listRewind(cluster_manager.nodes, &li); |
| 5821 | while ((ln = listNext(&li)) != NULL) { |
| 5822 | if (!first_node) first_node = 1; |
| 5823 | else json = sdscat(json, ",\n"); |
| 5824 | clusterManagerNode *node = ln->value; |
| 5825 | sds node_json = clusterManagerNodeGetJSON(node, cluster_errors_count); |
| 5826 | json = sdscat(json, node_json); |
| 5827 | sdsfree(node_json); |
| 5828 | if (node->replicate) |
| 5829 | continue; |
| 5830 | clusterManagerLogInfo(">>> Node %s:%d -> Saving RDB...\n", |
| 5831 | node->ip, node->port); |
| 5832 | fflush(stdout); |
| 5833 | getRDB(node); |
| 5834 | } |
| 5835 | json = sdscat(json, "\n]"); |
| 5836 | sds jsonpath = sdsnew(config.cluster_manager_command.backup_dir); |
| 5837 | if (jsonpath[sdslen(jsonpath) - 1] != '/') |
| 5838 | jsonpath = sdscat(jsonpath, "/"); |
| 5839 | jsonpath = sdscat(jsonpath, "nodes.json"); |
| 5840 | fflush(stdout); |
| 5841 | clusterManagerLogInfo("Saving cluster configuration to: %s\n", jsonpath); |
| 5842 | FILE *out = fopen(jsonpath, "w+"); |
| 5843 | if (!out) { |
| 5844 | clusterManagerLogErr("Could not save nodes to: %s\n", jsonpath); |
| 5845 | success = 0; |
| 5846 | goto cleanup; |
| 5847 | } |
| 5848 | fputs(json, out); |
| 5849 | fclose(out); |
| 5850 | cleanup: |
| 5851 | sdsfree(json); |
| 5852 | sdsfree(jsonpath); |
| 5853 | if (success) { |
| 5854 | if (!no_issues) { |
| 5855 | clusterManagerLogWarn("*** Cluster seems to have some problems, " |
| 5856 | "please be aware of it if you're going " |
| 5857 | "to restore this backup.\n"); |
| 5858 | } |
| 5859 | clusterManagerLogOk("[OK] Backup created into: %s\n", |
| 5860 | config.cluster_manager_command.backup_dir); |
| 5861 | } else clusterManagerLogOk("[ERR] Failed to back cluster!\n"); |
nothing calls this directly
no test coverage detected