summaryrefslogtreecommitdiff
path: root/tests/test_backup.rs
blob: a44c126d48a192cafb8af45e25dce959c2222d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2020 Tyler Neely
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod util;

use pretty_assertions::assert_eq;

use rocksdb::{
    backup::{BackupEngine, BackupEngineOptions, RestoreOptions},
    Env, DB,
};
use util::DBPath;

#[test]
fn restore_from_latest() {
    // create backup
    let path = DBPath::new("restore_from_latest_test");
    let restore_path = DBPath::new("restore_from_latest_path");
    {
        let db = DB::open_default(&path).unwrap();
        assert!(db.put(b"k1", b"v1111").is_ok());
        let value = db.get(b"k1");
        assert_eq!(value.unwrap().unwrap(), b"v1111");
        {
            let env = Env::new().unwrap();
            let backup_opts = BackupEngineOptions::new("backup_path_1").unwrap();

            let mut backup_engine = BackupEngine::open(&backup_opts, &env).unwrap();
            assert!(backup_engine.create_new_backup(&db).is_ok());

            // check backup info
            let info = backup_engine.get_backup_info();
            assert!(!info.is_empty());
            info.iter().for_each(|i| {
                assert!(backup_engine.verify_backup(i.backup_id).is_ok());
                assert!(i.size > 0);
            });

            let mut restore_option = RestoreOptions::default();
            restore_option.set_keep_log_files(false); // true to keep log files
            let restore_status = backup_engine.restore_from_latest_backup(
                &restore_path,
                &restore_path,
                &restore_option,
            );
            assert!(restore_status.is_ok());

            let db_restore = DB::open_default(&restore_path).unwrap();
            let value = db_restore.get(b"k1");
            assert_eq!(value.unwrap().unwrap(), b"v1111");
        }
    }
}

#[test]
fn restore_from_backup() {
    // create backup
    let path = DBPath::new("restore_from_backup_test");
    let restore_path = DBPath::new("restore_from_backup_path");
    {
        let db = DB::open_default(&path).unwrap();
        assert!(db.put(b"k1", b"v1111").is_ok());
        let value = db.get(b"k1");
        assert_eq!(value.unwrap().unwrap(), b"v1111");
        {
            let env = Env::new().unwrap();
            let backup_opts = BackupEngineOptions::new("backup_path_2").unwrap();

            let mut backup_engine = BackupEngine::open(&backup_opts, &env).unwrap();
            assert!(backup_engine.create_new_backup(&db).is_ok());

            // check backup info
            let info = backup_engine.get_backup_info();
            assert!(!info.is_empty());
            info.iter().for_each(|i| {
                assert!(backup_engine.verify_backup(i.backup_id).is_ok());
                assert!(i.size > 0);
            });

            let backup_id = info.get(0).unwrap().backup_id;
            let mut restore_option = RestoreOptions::default();
            restore_option.set_keep_log_files(false); // true to keep log files
            let restore_status = backup_engine.restore_from_backup(
                &restore_path,
                &restore_path,
                &restore_option,
                backup_id,
            );
            assert!(restore_status.is_ok());

            let db_restore = DB::open_default(&restore_path).unwrap();
            let value = db_restore.get(b"k1");
            assert_eq!(value.unwrap().unwrap(), b"v1111");
        }
    }
}