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

# rsyncs data from one directory to another. By default all subdirectories of the src directory are copied to the dst directory. No checks are done to verify that the arguments are directories because -d doesn't play nice with remote hosts.

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 "$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 $?" >> "$LOG_FILE"
fi