A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::str::FromStr;
use command_runner::CommandRunner;
use symbols::Symbol;
use storage::Storage;
pub struct DatabaseDump<'a, S> where S: Storage {
db_name: Cow<'a, str>,
storage: S,
command_runner: &'a CommandRunner
}
impl<'a, S> DatabaseDump<'a, S> where S: Storage {
pub fn new(db_name: Cow<'a, str>, storage: S, command_runner: &'a CommandRunner) -> Self {
DatabaseDump {
db_name: db_name,
storage: storage,
command_runner: command_runner
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
Ok(try!(String::from_utf8(b)))
}
}
impl<'a, S> fmt::Display for DatabaseDump<'a, S> where S: Storage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Dump MariaDB Database {}", self.db_name)
}
}
impl<'a, S> Symbol for DatabaseDump<'a, S> where S: Storage {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let dump_date = try!(self.storage.recent_date());
let modified_date = try!(self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name)));
if modified_date.trim_right() == "NULL" { return Ok(false); }
Ok(try!(u64::from_str(modified_date.trim_right())) <= dump_date)
}
fn execute(&self) -> Result<(), Box<Error>> {
self.command_runner.run_successfully("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name, self.storage.write_filename())])
}
}
#[cfg(test)]
mod test {
}