summaryrefslogtreecommitdiffstats
path: root/backup/src/main.rs
blob: d2210af8700ac147cf7565656252fa72753447dd (plain) (blame)
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
use chrono::Local;
use duct::cmd;
use regex::Regex;
use serde::Deserialize;
use std::{
    env,
    error::Error,
    fs::{self, File},
    io::{BufRead, BufReader},
    path::{Path, PathBuf},
    process::Command,
};

#[derive(Debug, Deserialize)]
struct BackupConfig {
    backup_dir: String,
    rotate_dir: String,
    timestamp_file: String,
    source_file: String,
    exclude_file: String,
}

#[derive(Debug, Deserialize)]
struct Config {
    backup: BackupConfig,
}

fn read_lines<P: AsRef<Path>>(path: P) -> Result<Vec<String>, Box<dyn Error>> {
    let file = File::open(path)?;
    let reader = BufReader::new(file);
    Ok(reader.lines().filter_map(Result::ok).filter(|l| !l.trim().is_empty()).collect())
}

fn get_backup_number(backup_dir: &Path) -> Result<u32, Box<dyn Error>> {
    let mut highest = 0;
    let pattern = Regex::new(r"backup-(\d{2})\.tar\.zst")?;

    for entry in fs::read_dir(backup_dir)? {
        let path = entry?.path();
        if let Some(fname) = path.file_name().and_then(|n| n.to_str()) {
            if let Some(caps) = pattern.captures(fname) {
                if let Ok(num) = caps[1].parse::<u32>() {
                    if num > highest {
                        highest = num;
                    }
                }
            }
        }
    }

    Ok(highest)
}

fn main() -> Result<(), Box<dyn Error>> {
    let base_dir = PathBuf::from("/root/scripts");
    let config_path = base_dir.join("config.json");
    let config_str = fs::read_to_string(&config_path)?;
    let config: Config = serde_json::from_str(&config_str)?;

    let backup_dir = Path::new(&config.backup.backup_dir);
    let rotate_dir = Path::new(&config.backup.rotate_dir);
    let timestamp_file = Path::new(&config.backup.timestamp_file);
    let source_list = read_lines(&config.backup.source_file)?;
    let exclude_list = read_lines(&config.backup.exclude_file)?;

    fs::create_dir_all(backup_dir)?;

    let mut source_paths: Vec<String> = vec![];
    for s in &source_list {
        if Path::new(s).exists() {
            source_paths.push(s.clone());
        } else {
            eprintln!("Warnung: '{}' existiert nicht", s);
        }
    }

    let mut exclude_args: Vec<String> = vec![];
    for e in &exclude_list {
        if Path::new(e).exists() {
            exclude_args.push(format!("--exclude={}", e));
        } else {
            eprintln!("Warnung: '{}' existiert nicht", e);
        }
    }

    let mut backup_nr = get_backup_number(backup_dir)? + 1;
    if backup_nr > 30 {
        backup_nr = 1;

        let now = Local::now();
        let rotate_subdir = rotate_dir.join(format!("{}-{}", now.format("%Y-%m-%d"), now.format("%H-%M-%S-%3f")));
        fs::create_dir_all(&rotate_subdir)?;

        for entry in fs::read_dir(backup_dir)? {
            let entry = entry?;
            let dest = rotate_subdir.join(entry.file_name());
            fs::rename(entry.path(), dest)?;
        }

        // pg_dumpall via duct (ähnlich wie Bash pipe)
        let dump1 = rotate_subdir.join("backup_mastodon-db-1.sql.zst");
        let dump2 = rotate_subdir.join("backup_db.sql.zst");

        cmd!("docker", "exec", "mastodon-db-1", "pg_dumpall", "-U", "postgres")
            .pipe(cmd!("zstd", "-9"))
            .stdout_file(File::create(dump1)?)
            .run()?;

        cmd!("pg_dumpall", "-U", "postgres")
            .pipe(cmd!("zstd", "-9"))
            .stdout_file(File::create(dump2)?)
            .run()?;
    }

    let backup_filename = format!("backup-{:02}.tar.zst", backup_nr);
    let backup_path = backup_dir.join(&backup_filename);

    let mut tar_cmd = Command::new("tar");
    tar_cmd
        .arg("-cvp")
        .arg("-g")
        .arg(timestamp_file)
        .arg("-f")
        .arg(&backup_path)
        .arg("-I")
        .arg("zstd -9 -T1");

    for excl in &exclude_args {
        tar_cmd.arg(excl);
    }
    for src in &source_paths {
        tar_cmd.arg(src);
    }

    let tar_status = tar_cmd.status()?;
    if !tar_status.success() {
        return Err("Fehler beim Erstellen des Tar-Backups".into());
    }

    // Log-Dateien behandeln
    cmd!("find", "/var/log", "-type", "f", "-name", "*.log", "-exec", "truncate", "-s", "0", "{}", ";")
        .run()?;
    cmd!("find", "/var/log", "-type", "f", "-name", "*.gz", "-exec", "rm", "-f", "{}", ";")
        .run()?;

    println!("Backup erfolgreich als {}", backup_path.display());
    Ok(())
}