aboutsummaryrefslogtreecommitdiffstats
path: root/backup
blob: 597be80810c1612d66d3600829701129153677a5 (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
#!/usr/bin/zsh

# rsyncs data from one directory to another. By default all subdirectories of the src directory are copied to the dst directory. Nothing happens unless both arguments are valid directories.

LOG_DIR="$HOME/rsync_logs"
LOG_FILE="$LOG_DIR/$(date --iso-8601).log"

RSYNC_OPTS=("-aP" "--no-owner" "--delete-during" "--exclude=lost+found" "--log-file=$LOG_FILE")

if [ $# -lt 2 ]; then
    echo "Usage: backup <src> <dst>"
    exit 1
fi

if [ ! -d "$1" ]; then
    echo "$1 does not exist or is not a directory"
    exit 1
fi

if [ ! -d "$2" ]; then
    echo "$2 does not exist or is not a directory"
    exit 1
fi

if [ ! -d "$LOG_DIR" ]; then
    echo "Log directory does not exist, creating"
    mkdir -p "$LOG_DIR"

    if [ $? -ne 0 ]; then
        echo "Failed to create log directory, exiting"
        exit 1
    fi
fi

echo "$(date)" > "$LOG_FILE"
if [ $? -ne 0 ]; then
    echo "Failed to create log file, exiting"
    exit 1
fi

echo "rsync $RSYNC_OPTS ${1%/}/* $2" >> "$LOG_FILE"
rsync $RSYNC_OPTS ${1%/}/* $2

if [ $? -eq 0 ]; then
    echo "Backup successful." >> "$LOG_FILE"
else
    echo "Backup exited with code $?"
fi