aboutsummaryrefslogtreecommitdiff
path: root/script/usb.sh
blob: 73c946c4dbc590cbc65cc6c052ae42c81dda5997 (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
#!/bin/bash

#Function to check if the time is during working hours
function workingHours()
{
    now=$(date +%H:%M)
    if [[ "$now" < "15:00" ]] && [[ "$now" > "13:00" ]]; then
            return 0;
    else
            return 1;
    fi
}

# Mount locations
primary="/mnt/primary"
backup="/mnt/backup"

# Create directories
if ! [ -d "$primary" ]; then
    mkdir -p "$primary"
fi

if ! [ -d "$backup" ]; then
    mkdir -p "$backup"
fi

# If primary is not mounted, mount it
if ! mount | grep $primary > /dev/null 2>&1; then
    mount LABEL=PRIMARY "$primary" > /dev/null 2>&1
fi

# If primary is mounted, sync
if mount | grep $primary > /dev/null 2>&1; then
    rsync -rltDqzPcO --no-perms --backup-dir=old /home/pi/data "$primary"
fi

# If is not working hours and backup is not mounted, mount it
if ! workingHours && ! mount | grep $backup > /dev/null 2>&1; then
    mount LABEL=BACKUP "$backup" > /dev/null 2>&1
fi

# If the backup is mounted, sync
if  mount | grep $backup > /dev/null 2>&1; then
    rsync -rltDqzPcO --no-perms --backup-dir=old /home/pi/data "$backup"
    umount LABEL=BACKUP "$backup" > /dev/null 2>&1
fi