aboutsummaryrefslogtreecommitdiff
path: root/backends/tar.sh
blob: 1265992ea4cdf21728f096cb90d8eca6704e6809 (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
function tar_init() {
    # nothing to do for tar?
    :
}

# TODO: Make default .tar with optional bup
function tar_create_backup() {
    echo "tar: backing up..."

    # save world to a temporary archive
    ARCHNAME="/tmp/${BACKUP_NAME}_`date +%FT%H%M%S%z`.tar.gz"
    tar -czf "$ARCHNAME" "./$WORLD_NAME"

    if [ ! $? -eq 0 ]
    then
        echo "tar: failed to save the world"
        rm "$ARCHNAME" #remove (probably faulty) archive
        return 1
    else
        echo "tar: world saved to $ARCHNAME, pushing it to backup directories..."
    fi

    RETCODE=2
    for BACKUP_DIR in ${BACKUP_DIRS[*]}
    do
        echo "tar: pushing to \"$BACKUP_DIR\""
        # scp acts as cp for local destination directories
        scp "$ARCHNAME" "$BACKUP_DIR/"
        if [ ! $? -eq 0 ]; then
            echo "tar: failed pushing to \"$BACKUP_DIR\", moving on"
        else
            RETCODE=0
        fi
     done

    rm "$ARCHNAME"

    echo "tar: backup finished"

    return $RETCODE
}

# server_restore relies on output format of this function
function tar_ls_dir() {
    BACKUP_DIR="$1"
    if [[ $BACKUP_DIR == *:* ]]; then
        REMOTE="$(echo "$BACKUP_DIR" | cut -d: -f1)"
        REMOTE_DIR="$(echo "$BACKUP_DIR" | cut -d: -f2)"
        ssh "$REMOTE" "ls -1 $REMOTE_DIR" | grep "tar.gz"
    else
        ls -1 "$BACKUP_DIR" | grep "tar.gz"
    fi
}

function tar_ls_all() {
    for BACKUP_DIR in ${BACKUP_DIRS[*]}
    do
        echo "Backups in $BACKUP_DIR:"
        tar_ls_remote "$BACKUP_DIR"
    done
}

function tar_restore() {
    REMOTE="$1"
    SNAPSHOT="$2"

    scp "$REMOTE/$SNAPSHOT" "/tmp/"
    if [ ! $? -eq 0 ]; then
        echo "Failed to get archive from \"$REMOTE/$SNAPSHOT\""
        return 1
    fi

    tar -xzf "/tmp/$SNAPSHOT"
}