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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::{
    ffi::OsStr,
    io,
    path::{Path, PathBuf},
    process::ExitStatus,
};

use either::{Left, Right};
use tempfile::TempDir;
use tokio::{fs, task::block_in_place};
use tokio_stream::{wrappers::ReadDirStream, StreamExt};

use super::{config, resource::StartupResource};
use crate::{cluster, coordinate, lock};

// ----------------------------------------------------------------------------

#[derive(Debug)]
pub struct Backup {
    pub backup_dir: PathBuf,
    pub backup_wal_dir: PathBuf,
}

impl Backup {
    /// Creates the destination directory and the WAL archive directory if these
    /// do not exist, and allocates a temporary location for the base backup.
    pub async fn prepare<D: AsRef<Path>>(backup_dir: D) -> Result<Self, BackupError> {
        fs::create_dir_all(&backup_dir).await?;
        let backup_dir = backup_dir.as_ref().canonicalize()?;
        let backup_wal_dir = backup_dir.join("wal");
        fs::create_dir_all(&backup_wal_dir).await?;
        Ok(Self { backup_dir, backup_wal_dir })
    }

    /// Configures the cluster for continuous archiving.
    ///
    /// Returns a flag indicating if the cluster must be restarted for changes
    /// to take effect. If the cluster is already configured appropriately, this
    /// does nothing (and returns `false`).
    pub async fn do_configure_archiving<'a>(
        &self,
        resource: &'a StartupResource<'a>,
        archive_command: &str,
    ) -> Result<bool, BackupError> {
        let pool = match resource {
            Left(resource) => resource.facet().pool(None),
            Right(resource) => resource.facet().pool(None),
        }?;
        let mut restart: bool = false;

        // Ensure that `wal_level` is set to `replica` or `logical`. If not,
        // set it to `replica`.
        match WAL_LEVEL.get(&pool).await? {
            Some(config::Value::String(level)) if level == "replica" || level == "logical" => {
                log::debug!("{WAL_LEVEL:?} already set to {level:?}");
            }
            Some(_) => {
                log::info!("Setting {WAL_LEVEL:?} to 'replica'");
                WAL_LEVEL.set(&pool, "replica").await?;
                restart = true;
            }
            None => {
                return Err(BackupError::ConfigError(
                    "WAL is not supported; cannot proceed".into(),
                ))
            }
        }

        // Ensure that `archive_mode` is set to `on` or `always`. If not,
        // set it to `on`.
        match ARCHIVE_MODE.get(&pool).await? {
            Some(config::Value::String(level)) if level == "on" || level == "always" => {
                log::debug!("{ARCHIVE_MODE:?} already set to {level:?}");
            }
            Some(_) => {
                log::info!("Setting {ARCHIVE_MODE:?} to 'on'");
                ARCHIVE_MODE.set(&pool, "on").await?;
                restart = true;
            }
            None => {
                return Err(BackupError::ConfigError(
                    "Archiving is not supported; cannot proceed".into(),
                ))
            }
        }

        // We can't set `archive_command` if `archive_library` is already set.
        match ARCHIVE_LIBRARY.get(&pool).await? {
            Some(config::Value::String(library)) if library.is_empty() => {
                log::debug!("{ARCHIVE_LIBRARY:?} not set (good for us)");
            }
            Some(archive_library) => {
                return Err(BackupError::ConfigError(format!(
                    "{ARCHIVE_LIBRARY:?} is already set to {archive_library:?}; cannot proceed"
                )))
            }
            None => {
                log::debug!("{ARCHIVE_LIBRARY:?} is not supported (good for us)");
            }
        }

        match ARCHIVE_COMMAND.get(&pool).await? {
            Some(config::Value::String(command)) if command == archive_command => {
                log::debug!("{ARCHIVE_COMMAND:?} already set to {archive_command:?}");
            }
            // Re. "(disabled)", see `show_archive_command` in xlog.c.
            Some(config::Value::String(command))
                if command.is_empty() || command == "(disabled)" =>
            {
                log::info!("Setting {ARCHIVE_COMMAND:?} to {archive_command:?}");
                ARCHIVE_COMMAND.set(&pool, archive_command).await?;
            }
            Some(archive_command) => {
                return Err(BackupError::ConfigError(format!(
                    "{ARCHIVE_COMMAND:?} is already set to {archive_command:?}; cannot proceed"
                )))
            }
            None => {
                return Err(BackupError::ConfigError(
                    "Archiving is not supported; cannot proceed".into(),
                ))
            }
        }

        Ok(restart)
    }

    /// Performs a "base backup" of the cluster.
    ///
    /// Returns the directory into which the backup has been created. This is
    /// always a subdirectory of [`self.backup_dir`].
    ///
    /// This must be performed _after_ configuring continuous archiving (see
    /// [`Backup::do_configure_archiving`]).
    pub async fn do_base_backup<'a>(
        &self,
        resource: &'a StartupResource<'a>,
    ) -> Result<PathBuf, BackupError> {
        // Temporary location into which we'll make the base backup.
        let backup_tmp_dir =
            block_in_place(|| TempDir::with_prefix_in(BACKUP_DATA_PREFIX_TMP, &self.backup_dir))?;

        let args: &[&OsStr] = &[
            "--pgdata".as_ref(),
            backup_tmp_dir.path().as_ref(),
            "--format".as_ref(),
            "plain".as_ref(),
            "--progress".as_ref(),
        ];
        let status = block_in_place(|| match resource {
            Left(resource) => resource.facet().exec(None, "pg_basebackup".as_ref(), args),
            Right(resource) => resource.facet().exec(None, "pg_basebackup".as_ref(), args),
        })?;
        if !status.success() {
            Err(status)?;
        }
        // Before calculating the target directory name or doing the actual
        // rename, take out a coordinating lock in `backup_dir`.
        let backup_lock = block_in_place(|| {
            lock::UnlockedFile::try_from(&self.backup_dir.join(BACKUP_LOCK_NAME))?
                .lock_exclusive()
                .map_err(coordinate::CoordinateError::UnixError)
        })?;

        // Where we're going to move the new backup to. This is always a
        // directory named `{BACKUP_DATA_PREFIX}.NNNNNNNNNN` where NNNNNNNNNN is
        // a zero-padded integer, the next available in `destination`.
        let backup_data_dir = self.backup_dir.join(format!(
            "{BACKUP_DATA_PREFIX}{:010}",
            ReadDirStream::new(fs::read_dir(&self.backup_dir).await?)
                .filter_map(Result::ok)
                .filter_map(|entry| match entry.file_name().to_str() {
                    Some(name) if name.starts_with(BACKUP_DATA_PREFIX) =>
                        name[BACKUP_DATA_PREFIX.len()..].parse::<u32>().ok(),
                    Some(_) | None => None,
                })
                .fold(0, Ord::max)
                .await
                + 1
        ));

        // Do the rename.
        fs::rename(&backup_tmp_dir, &backup_data_dir).await?;
        drop(backup_lock);

        Ok(backup_data_dir)
    }
}

// ----------------------------------------------------------------------------

static ARCHIVE_MODE: config::Parameter = config::Parameter("archive_mode");
static ARCHIVE_COMMAND: config::Parameter = config::Parameter("archive_command");
static ARCHIVE_LIBRARY: config::Parameter = config::Parameter("archive_library");
static WAL_LEVEL: config::Parameter = config::Parameter("wal_level");

// Successful backups have this directory name prefix.
pub static BACKUP_DATA_PREFIX: &str = "data.";

// In-progress backups have this directory name prefix.
static BACKUP_DATA_PREFIX_TMP: &str = ".tmp.data.";

// Coordinating lock for working in the backup directory.
static BACKUP_LOCK_NAME: &str = ".lock";

// ----------------------------------------------------------------------------

#[derive(thiserror::Error, miette::Diagnostic, Debug)]
pub enum BackupError {
    #[error("Input/output error")]
    IoError(#[from] io::Error),
    #[error("Shell error: {0}")]
    GeneralError(String),
    #[error("Configuration error: {0}")]
    ConfigError(String),
    #[error(transparent)]
    CoordinateError(#[from] coordinate::CoordinateError<cluster::ClusterError>),
    #[error(transparent)]
    ClusterError(#[from] cluster::ClusterError),
    #[error("External command failed: {0:?}")]
    CommandError(ExitStatus),
    #[error("Database error")]
    SqlxError(#[from] cluster::sqlx::Error),
}

impl From<ExitStatus> for BackupError {
    fn from(error: ExitStatus) -> BackupError {
        Self::CommandError(error)
    }
}