aboutsummaryrefslogtreecommitdiff
path: root/docker/auto-backup/daemon.bash
blob: a4dd5dc163f213d1e64286a9bc96d1f5c6abdd4d (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
#!/usr/bin/env bash

set -e

# Check I'm root.
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi


# Check xz, tar and coscmd
xz --version
tar --version

function backup {
    # Output "Begin backup..." in yellow and restore default
    echo -e "\e[0;103m\e[K\e[1mBegin backup..." "\e[0m"

    # Get current time and convert it to YYYY-MM-DDTHH:MM:SSZ
    current_time=$(date +%Y-%m-%dT%H:%M:%SZ)
    echo "Current time: $current_time"

    echo "Create tar.xz for data..."

    # tar and xz /data to tmp
    tar -cJf /tmp/data.tar.xz -C / data

    # Output /tmp/data.tar.xz size
    du -h /tmp/data.tar.xz | cut -f1 | xargs echo "Size of data.tar.xz:"

    destination="${current_time}.tar.xz"

    # upload to remote
    dotnet /AutoBackup/AutoBackup.dll /tmp/data.tar.xz "$destination" 

    echo "Remove tmp file..."
    # remove tmp
    rm /tmp/data.tar.xz

    echo "$destination" >> /data/backup.log

    # echo "Backup finished!" in green and restore default
    echo -e "\e[0;102m\e[K\e[1mFinish backup!\e[0m"
}

echo "Initial delay: $CRUPEST_AUTO_BACKUP_INIT_DELAY"
sleep "$CRUPEST_AUTO_BACKUP_INIT_DELAY"

# forever loop
while true; do
    backup

    # sleep for CRUPEST_AUTO_BACKUP_INTERVAL
    echo "Sleep for $CRUPEST_AUTO_BACKUP_INTERVAL for next backup..."
    sleep "$CRUPEST_AUTO_BACKUP_INTERVAL"
done