1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+
20+ package org .apache .zookeeper .cli ;
21+
22+ import org .apache .commons .cli .CommandLine ;
23+ import org .apache .commons .cli .Option ;
24+ import org .apache .commons .cli .Options ;
25+ import org .apache .commons .cli .ParseException ;
26+ import org .apache .commons .cli .Parser ;
27+ import org .apache .commons .cli .PosixParser ;
28+ import org .apache .zookeeper .server .backup .RestoreFromBackupTool ;
29+
30+ /**
31+ * Restore command for ZkCli.
32+ */
33+ public class RestoreCommand extends CliCommand {
34+
35+ private RestoreFromBackupTool tool ;
36+ private CommandLine cl ;
37+ private static Options options = new Options ();
38+ private static final String RESTORE_ZXID_STR = "restore_zxid" ;
39+ private static final String RESTORE_TIMESTAMP_STR = "restore_timestamp" ;
40+ private static final String BACKUP_STORE_STR = "backup_store" ;
41+ private static final String DATA_DESTINATION_STR = "data_destination" ;
42+ private static final String LOG_DESTINATION_STR = "log_destination" ;
43+ private static final String TIMETABLE_STORAGE_PATH_STR = "timetable_storage_path" ;
44+ private static final String LOCAL_RESTORE_TEMP_DIR_PATH_STR = "local_restore_temp_dir_path" ;
45+ private static final String DRY_RUN_STR = "dry_run" ;
46+ public static final String RESTORE_ZXID_OPTION = "z" ;
47+ public static final String RESTORE_TIMESTAMP_OPTION = "t" ;
48+ public static final String BACKUP_STORE_OPTION = "b" ;
49+ public static final String DATA_DESTINATION_OPTION = "d" ;
50+ public static final String LOG_DESTINATION_OPTION = "l" ;
51+ public static final String TIMETABLE_STORAGE_PATH_OPTION = "s" ;
52+ public static final String LOCAL_RESTORE_TEMP_DIR_PATH_OPTION = "r" ;
53+ public static final String DRY_RUN_OPTION = "n" ;
54+ private static final String RESTORE_ZXID_CMD = "-" + RESTORE_ZXID_OPTION + " " + RESTORE_ZXID_STR ;
55+ private static final String RESTORE_TIMESTAMP_CMD =
56+ "-" + RESTORE_TIMESTAMP_OPTION + " " + RESTORE_TIMESTAMP_STR ;
57+ private static final String BACKUP_STORE_CMD = "-" + BACKUP_STORE_OPTION + " " + BACKUP_STORE_STR ;
58+ private static final String DATA_DESTINATION_CMD =
59+ "-" + DATA_DESTINATION_OPTION + " " + DATA_DESTINATION_STR ;
60+ private static final String LOG_DESTINATION_CMD =
61+ "-" + LOG_DESTINATION_OPTION + " " + LOG_DESTINATION_STR ;
62+ private static final String TIMETABLE_STORAGE_PATH_CMD =
63+ "-" + TIMETABLE_STORAGE_PATH_OPTION + " " + TIMETABLE_STORAGE_PATH_STR ;
64+ private static final String LOCAL_RESTORE_TEMP_DIR_PATH_CMD =
65+ "-" + LOCAL_RESTORE_TEMP_DIR_PATH_OPTION + " " + LOCAL_RESTORE_TEMP_DIR_PATH_STR ;
66+ private static final String DRY_RUN_CMD = "-" + DRY_RUN_OPTION ;
67+ private static final String RESTORE_CMD_STR = "restore" ;
68+ private static final String OPTION_STR =
69+ "[" + RESTORE_ZXID_CMD + "]/[" + RESTORE_TIMESTAMP_CMD + "] [" + BACKUP_STORE_CMD + "] ["
70+ + DATA_DESTINATION_CMD + "] [" + LOG_DESTINATION_CMD + "] [" + TIMETABLE_STORAGE_PATH_CMD
71+ + "](needed if restore to a timestamp) [" + LOCAL_RESTORE_TEMP_DIR_PATH_CMD
72+ + "](optional) [" + DRY_RUN_CMD + "](optional)" ;
73+
74+ static {
75+ options .addOption (new Option (RESTORE_ZXID_OPTION , true , RESTORE_ZXID_STR ));
76+ options .addOption (new Option (RESTORE_TIMESTAMP_OPTION , true , RESTORE_TIMESTAMP_STR ));
77+ options .addOption (new Option (BACKUP_STORE_OPTION , true , BACKUP_STORE_STR ));
78+ options .addOption (new Option (DATA_DESTINATION_OPTION , true , DATA_DESTINATION_STR ));
79+ options .addOption (new Option (LOG_DESTINATION_OPTION , true , LOG_DESTINATION_STR ));
80+ options .addOption (new Option (TIMETABLE_STORAGE_PATH_OPTION , true , TIMETABLE_STORAGE_PATH_STR ));
81+ options .addOption (
82+ new Option (LOCAL_RESTORE_TEMP_DIR_PATH_OPTION , true , LOCAL_RESTORE_TEMP_DIR_PATH_STR ));
83+ options .addOption (new Option (DRY_RUN_OPTION , false , DRY_RUN_STR ));
84+ }
85+
86+ public RestoreCommand () {
87+ super (RESTORE_CMD_STR , OPTION_STR );
88+ tool = new RestoreFromBackupTool ();
89+ }
90+
91+ @ Override
92+ public String getUsageStr () {
93+ return "Usage: RestoreFromBackupTool " + RESTORE_CMD_STR + " " + OPTION_STR + "\n "
94+ + RESTORE_ZXID_CMD
95+ + ": the point to restore to, either the string 'latest' or a zxid in hex format. Choose one between this option or "
96+ + RESTORE_TIMESTAMP_CMD + ", if both are specified, this option will be prioritized\n "
97+ + RESTORE_TIMESTAMP_CMD
98+ + ": the point to restore to, a timestamp in long format. Choose one between this option or "
99+ + RESTORE_ZXID_CMD + ".\n " + BACKUP_STORE_CMD
100+ + ": the connection information for the backup store\n For GPFS the format is: gpfs:<config_path>:<backup_path>:<namespace>\n "
101+ + DATA_DESTINATION_CMD + ": local destination path for restored snapshots\n "
102+ + LOG_DESTINATION_CMD + ": local destination path for restored txlogs\n "
103+ + TIMETABLE_STORAGE_PATH_CMD
104+ + ": Needed if restore to a timestamp. Backup storage path for timetable files, for GPFS the format is: gpfs:<config_path>:<backup_path>:<namespace>, if not set, default to be same as backup storage path\n "
105+ + LOCAL_RESTORE_TEMP_DIR_PATH_CMD
106+ + ": Optional, local path for creating a temporary intermediate directory for restoration, the directory will be deleted after restoration is done\n "
107+ + DRY_RUN_CMD + " " + DRY_RUN_STR
108+ + ": Optional, no files will be actually copied in a dry run" ;
109+ }
110+
111+ @ Override
112+ public CliCommand parse (String [] cmdArgs ) throws CliParseException {
113+ Parser parser = new PosixParser ();
114+ try {
115+ cl = parser .parse (options , cmdArgs );
116+ } catch (ParseException ex ) {
117+ throw new CliParseException (getUsageStr (), ex );
118+ }
119+ if ((!cl .hasOption (RESTORE_ZXID_OPTION ) && !cl .hasOption (RESTORE_TIMESTAMP_OPTION )) || !cl
120+ .hasOption (BACKUP_STORE_OPTION ) || !cl .hasOption (DATA_DESTINATION_OPTION ) || !cl
121+ .hasOption (LOG_DESTINATION_OPTION )) {
122+ throw new CliParseException ("Missing required argument(s).\n " + getUsageStr ());
123+ }
124+ return this ;
125+ }
126+
127+ @ Override
128+ public boolean exec () throws CliException {
129+ return tool .runWithRetries (cl );
130+ }
131+ }
0 commit comments