aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 86c8a23321fa286ae318bf31da7434e6dd7b69f3 (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
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
use std::{
    env::var,
    fs::{File, remove_file},
    io::Read,
    os::unix::fs::FileTypeExt,
    time::{Duration, UNIX_EPOCH},
};

use anyhow::Result;
use clokwerk::{AsyncScheduler, Interval};
use log::{debug, error, info};
use rpgpie::{
    certificate::Certificate,
    message::{SignatureMode, encrypt},
    policy::Seipd,
};
use tar::Builder;
use teloxide::{Bot, prelude::Requester, types::InputFile};
use tokio::time::sleep;

const MAX_FILE_SIZE: usize = 50000000;
const BACKUP: &str = "backup";
const BACKUP_ENCRYPTED: &str = "backup_encrypted";

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init();
    let mut scheduler = AsyncScheduler::new();
    let interval = var("INTERVAL")?.parse::<u32>()?;

    scheduler.every(Interval::Hours(interval)).run(|| async {
        backup().await.unwrap();
    });

    info!("Program set to backup every {} hours", interval);

    info!("Running first backup out of schedule");
    backup().await?;

    loop {
        scheduler.run_pending().await;
        sleep(Duration::from_secs(30)).await;
    }
}

async fn backup() -> Result<()> {
    info!("Running a backup");
    let chat_id = var("CHAT_ID").unwrap();
    let locations: Vec<String> = var("LOCATIONS")
        .unwrap()
        .trim()
        .split(':')
        .map(|s| s.to_string())
        .collect();

    archive(&locations)?;
    encrypt_data("pub.txt")?;
    remove_file(BACKUP)?;

    let bot = Bot::from_env();
    let total = std::fs::metadata(BACKUP_ENCRYPTED)?.len() as usize;
    let mut data_to_send = File::open(BACKUP_ENCRYPTED)?;

    bot.send_message(
        chat_id.clone(),
        format!("{}", UNIX_EPOCH.elapsed()?.as_secs().to_string()),
    )
    .await?;

    let mut rr;
    let mut part = 0;
    let mut read = 0;
    loop {
        let mut buf = vec![
            0;
            if total - read < MAX_FILE_SIZE {
                total - read
            } else {
                MAX_FILE_SIZE
            }
        ];

        rr = data_to_send.read(&mut buf)?;
        read += rr;
        debug!("Read {} bytes", rr);

        let data = InputFile::memory(buf).file_name(format!("part_{:06}", part));

        loop {
            match bot.send_document(chat_id.clone(), data.clone()).await {
                Ok(_) => break,
                Err(teloxide::RequestError::RetryAfter(n)) => {
                    error!("Awaiting {} seconds", n.seconds());
                    sleep(Duration::from_secs(n.seconds() as u64)).await
                }
                Err(err) => {
                    error!("{}\nAwaiting 8 seconds", err);
                    sleep(Duration::from_secs(8)).await
                }
            }
        }
        info!("Sent part {}", part);
        part += 1;

        if read >= total {
            break;
        }
    }

    drop(data_to_send);
    remove_file(BACKUP_ENCRYPTED)?;

    info!("Backup terminated");
    Ok(())
}

fn list_leafs(acc: &mut Vec<String>, path: &str) -> Result<()> {
    for entry in std::fs::read_dir(path)? {
        match entry {
            Ok(e) => {
                if !e.file_type().unwrap().is_dir() {
                    acc.push(format!("{}/{}", path, e.file_name().to_str().unwrap()));
                } else {
                    list_leafs(
                        acc,
                        &format!("{}/{}", path, e.file_name().to_str().unwrap()),
                    )?;
                }
            }
            Err(_) => (),
        }
    }
    Ok(())
}

fn archive(locations: &[String]) -> Result<()> {
    info!("Creating archive");
    let mut paths = Vec::new();
    let mut b = Builder::new(File::create(BACKUP)?);

    for loc in locations {
        list_leafs(&mut paths, loc)?;
    }

    for p in paths
        .iter()
        .filter(|item| !std::fs::metadata(item).unwrap().file_type().is_socket())
    {
        b.append_path(p)?;
    }

    b.finish()?;
    info!("Archive created");
    Ok(())
}

fn encrypt_data(key_file: &str) -> Result<()> {
    info!("Encrypting archive");
    let rec = Certificate::load(&mut File::open(key_file)?)?;
    let mut plaintext = File::open(BACKUP)?;
    let mut output = File::create(BACKUP_ENCRYPTED)?;
    encrypt(
        Some(Seipd::SEIPD2),
        rec,
        Vec::new(),
        Vec::new(),
        &Vec::new(),
        &mut plaintext,
        SignatureMode::Binary,
        &mut output,
        false,
    )?;
    info!("Archive encrypted");
    Ok(())
}