Skip to content

Commit c12c7ee

Browse files
committed
server.sh functional finished
1 parent f401a06 commit c12c7ee

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

src/borg/server.sh

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

33
# === vars ===
4-
54
# borg repo
65
if [[ -v BORG_REPO && -v BORG_PASSPHRASE ]]; then
76
export BORG_REPO
@@ -10,27 +9,25 @@ else
109
echo "both vars *BORG_REPO* & *BORG_PASSPHRASE* have to be set"
1110
exit 1
1211
fi
13-
1412
# ssh auth
1513
if [[ -v BORG_RSH ]]; then
1614
if [[ -f "$SSH_KEYFILE" ]]; then
1715
export BORG_RSH
1816
else
19-
echo "*$SSH_KEYFILE* not found"
17+
echo "'$SSH_KEYFILE' not found"
2018
exit 1
2119
fi
2220
fi
23-
2421
# mounting location; creates folder if not existing
2522
if [[ -v MOUNT_DIR && -n "$MOUNT_DIR" ]]; then
26-
prefix="mounting location *$MOUNT_DIR* ... "
23+
prefix="mounting location '$MOUNT_DIR' ..."
2724
if [[ -d "$MOUNT_DIR" ]]; then
28-
echo "$prefix" "exists"
25+
echo "$prefix exists"
2926
else
3027
if mkdir -p "$MOUNT_DIR"; then
31-
echo "$prefix" "created"
28+
echo "$prefix created"
3229
else
33-
echo "$prefix" "can't be created"
30+
echo "$prefix can't be created"
3431
exit 1
3532
fi
3633
fi
@@ -45,45 +42,55 @@ validate_borg_conn() {
4542
borg info 1> /dev/null && return 0
4643
return 1
4744
}
48-
4945
# checks if archives are there to recover from
5046
validate_borg_archive() {
5147
borg list | grep -q . && return 0
5248
return 1
5349
}
54-
55-
# === post func ===
56-
# get archive (for now latest one)
57-
latest_borg_archive() {
58-
latest=$(borg list --short | tail -n 1)
59-
[[ -n "$latest" ]] && return 0
50+
# unmount previous archive from mount location
51+
do_unmount() {
52+
if mountpoint -q "$MOUNT_DIR"; then
53+
borg umount "$MOUNT_DIR" && return 0
54+
return 1
55+
fi
56+
return 0
57+
}
58+
# latest archive mount to mounting location
59+
do_mount() {
60+
# unmounting previous one before mounting new archive
61+
do_unmount
62+
borg mount "::$latest" "$MOUNT_DIR" && mountpoint -q "$MOUNT_DIR" && return 0
6063
return 1
6164
}
6265

6366
# === main ===
64-
prefix="repo: *$BORG_REPO* ... "
67+
prefix="connection '$BORG_REPO' ..."
6568
# checks if connection is working
6669
if validate_borg_conn; then
67-
echo "$prefix" "connection successful"
70+
echo "$prefix successful"
6871
else
69-
echo "$prefix" "connection unsuccessful"
72+
echo "$prefix unsuccessful"
7073
exit 1
7174
fi
72-
7375
# checks if at least one archive is there
7476
if ! validate_borg_archive; then
7577
echo "no archives can be found"
7678
exit 1
7779
fi
78-
7980
# gets latest archive
80-
prefix="archive: "
81+
prefix="archive "
8182
latest=$(borg list --short | tail -n 1)
8283
if [[ -n "$latest" ]]; then
83-
echo "$prefix" "$latest"
84+
echo "$prefix 'latest::$latest'"
8485
else
85-
echo "$prefix" "not found"
86+
echo "$prefix not found"
87+
exit 1
88+
fi
89+
# mounting archive
90+
prefix="mounting 'latest::$latest' at '$MOUNT_DIR' ..."
91+
if do_mount; then
92+
echo "$prefix successful"
93+
else
94+
echo "$prefix unsuccessful"
8695
exit 1
8796
fi
88-
89-
# makes

0 commit comments

Comments
 (0)