Debian Codegraph @

src/main/org/h2/command/dml/BackupCommand.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command.dml;
7
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.util.ArrayList;
11 import java.util.zip.ZipOutputStream;
12 import org.h2.api.ErrorCode;
13 import org.h2.command.CommandInterface;
14 import org.h2.command.Prepared;
15 import org.h2.engine.Constants;
16 import org.h2.engine.Database;
17 import org.h2.engine.SessionLocal;
18 import org.h2.expression.Expression;
19 import org.h2.message.DbException;
20 import org.h2.mvstore.db.Store;
21 import org.h2.result.ResultInterface;
22 import org.h2.store.FileLister;
23 import org.h2.store.fs.FileUtils;
24
25 /**
26 * This class represents the statement
27 * BACKUP
28 */
29 public class BackupCommand extends Prepared {
30
31 private Expression fileNameExpr;
32
33 public BackupCommand(SessionLocal session) {
34 super(session);
35 }
36
37 public void setFileName(Expression fileName) {
38 this.fileNameExpr = fileName;
39 }
40
41 @Override
42 public long update() {
43 String name = fileNameExpr.getValue(session).getString();
44 session.getUser().checkAdmin();
45 backupTo(name);
46 return 0;
47 }
48
49 private void backupTo(String fileName) {
50 Database db = getDatabase();
51 if (!db.isPersistent()) {
52 throw DbException.get(ErrorCode.DATABASE_IS_NOT_PERSISTENT);
53 }
54 try {
55 Store store = db.getStore();
56 store.flush();
57 String name = db.getName();
58 name = FileUtils.getName(name);
59 try (OutputStream zip = FileUtils.newOutputStream(fileName, false)) {
60 ZipOutputStream out = new ZipOutputStream(zip);
61 db.flush();
62 // synchronize on the database, to avoid concurrent temp file
63 // creation / deletion / backup
64 synchronized (db.getLobSyncObject()) {
65 String prefix = db.getDatabasePath();
66 String dir = FileUtils.getParent(prefix);
67 dir = FileLister.getDir(dir);
68 ArrayList<String> fileList = FileLister.getDatabaseFiles(dir, name, true);
69 for (String n : fileList) {
70 if (n.endsWith(Constants.SUFFIX_MV_FILE)) {
71 store.getMvStore().getFileStore().backup(out);
72 }
73 }
74 }
75 out.close();
76 }
77 } catch (IOException e) {
78 throw DbException.convertIOException(e, fileName);
79 }
80 }
81
82 @Override
83 public boolean isTransactional() {
84 return true;
85 }
86
87 /**
88 * Fix the file name, replacing backslash with slash.
89 *
90 * @param f the file name
91 * @return the corrected file name
92 */
93 public static String correctFileName(String f) {
94 f = f.replace('\\', '/');
95 if (f.startsWith("/")) {
96 f = f.substring(1);
97 }
98 return f;
99 }
100
101 @Override
102 public boolean needRecompile() {
103 return false;
104 }
105
106 @Override
107 public ResultInterface queryMeta() {
108 return null;
109 }
110
111 @Override
112 public int getType() {
113 return CommandInterface.BACKUP;
114 }
115
116 }