aboutsummaryrefslogtreecommitdiff
path: root/docker/auto-backup/daemon.bash
blob: 5dd8cfedd83ba7f65b296b19d8792f80a0177a5e (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
#!/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

/coscli --version

# Check xz and tar
xz --version
tar --version

bucket_yaml=$(yq ".buckets[] | select(.alias == \"crupest-backup\")" ~/.cos.yaml)

# check bucket_yaml is not empty
if [[ -z "$bucket_yaml" ]]; then
    echo "Bucket crupest-backup not found. Please check your coscli config." 1>&2
    exit 1
fi

bucket_name=$(echo "$bucket_yaml" | yq ".name")
bucket_region=$(echo "$bucket_yaml" | yq ".region")

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.SS
    current_time=$(date +%Y-%m-%dT%H.%M.%S)
    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="cos://crupest-backup/$current_time.tar.xz"
    echo "Use coscli to upload data to $destination ..."
    echo "Bucket name: $bucket_name"
    echo "Bucket region: $bucket_region"

    # upload to remote
    /coscli cp /tmp/data.tar.xz "$destination" 

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

    # 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