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

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

	local status

    # save world to a temporary archive
    local archname="/tmp/${BACKUP_NAME}_`date +%F_%H-%M-%S`.tar.gz"
    tar -czf "$archname" "./$WORLD_NAME"
	status=$?
    if [ $status -ne 0 ]; then
        log_error "tar: failed to save the world"
        rm "$archname" #remove (probably faulty) archive
        return 1
    fi
    log_debug "tar: world saved to $archname, pushing it to backup directories..."

	# 0 if could save to at least one backup dir
	# TODO: make more strict?
    local retcode=1
    for backup_dir in ${BACKUP_DIRS[*]}
    do
        log_info "tar: backing up to \"$backup_dir\""
        # scp acts as cp for local destination directories
        scp "$archname" "$backup_dir/"
		status=$?
        if [ $status -ne 0 ]; then
            log_error "tar: failed pushing to \"$backup_dir\", moving on"
        else
            retcode=0
        fi
     done

    rm "$archname"

    log_debug "tar: backup finished"

    return $retcode
}

# server_restore relies on output format of this function
function tar_ls() {
    local backup_dir="$1"

    if [[ "$backup_dir" == *:* ]]; then
        local remote="$(echo "$backup_dir" | cut -d: -f1)"
        local remote_dir="$(echo "$backup_dir" | cut -d: -f2)"
        ssh "$remote" "ls -1 $remote_dir" | grep "tar.gz" | sort -r
	else
        ls -1 "$backup_dir" | grep "tar.gz" | sort -r
	fi
}

function tar_restore() {
    local remote="$1"
    local snapshot="$2"
	local dest="$3"
	local status

    scp "$remote/$snapshot" "/tmp/"
	status=$?
    if [ $status -ne 0 ]; then
        log_error "tar: failed to get archive from \"$remote/$snapshot\""
        return 1
    fi

    tar -xzf "/tmp/$snapshot" -C "$dest"
}