Repository: borgbackup/borg

Issue:
{hostname} isn't always the short hostname, but FQDN
## Have you checked borgbackup docs, FAQ, and open Github issues?
yes

## Is this a BUG / ISSUE report or a QUESTION?

BUG

## System information. For client/server mode post info for both machines.

#### Your borg version (borg -V).

1.1.8

#### Full borg commandline that lead to the problem (leave away excludes and passwords)

```
borg create "::{hostname}_test" /tmp/
```
## Describe the problem you're observing.

```
borg list
```
some backup sets have short hostname and some FQDN. e.g.:
```
tmp1_test
tmp2.example.com_test
```

using shell variables/commands work as a workaround:
```
borg create "::${hostname --short}_test" /tmp/
```
```
tmp1_test
tmp2_test
```



Discussion:
What does the `hostname` shell command output on the hosts where the fqdn archive names are created?

it wrongly reports the same, but that's not the point. according to documentation ```{hostname}``` should return the short hostname. ```hostname --short``` and ```hostname --fqdn``` behave correctly.
@pille can you print out `/etc/hostname` here?



for reasons it's:
```
tmp1
```
```
tmp2.example.com
```
`/etc/hostname` must have only 1 record `tmp1` so i not see any problem at borg side.

You can set FQDN name as the first column in /etc/hosts for the server's IP, followed by the short name.
```
/etc/hosts:

127.0.0.1   localhost 
::1         localhost 

10.2.17.15   tmp2.example.com tmp1
```

@infectormp: as i said, this setup has it's reasons.
the whole point of this issue was to create awareness that documentation is not in sync with implementation. hostnames may contain multiple words seperated by dots.
either fix the code or the docs. i'll live with my workaround.
relevant borg 1.1.8 code: https://github.com/borgbackup/borg/blob/master/src/borg/platform/base.py#L250

https://docs.python.org/3/library/socket.html#socket.gethostname

https://www.debian.org/doc/manuals/debian-reference/ch03.en.html#_the_hostname
Considering the last link, this might be rather a bad system configuration than a borg issue?
https://docs.fedoraproject.org/en-US/Fedora/13/html/Installation_Guide/sn-networkconfig-fedora.html

> **Valid Hostnames**
You may give your system any name provided that the full hostname is unique. The hostname may include letters, numbers and hyphens.
@infectormp hah, that phrasing can easily mislead. Considering they don't mention dots, they mean the short name. But also speaking about the uniqueness of the full name here might mislead people to think that the full name is (completely) defined here.
https://jblevins.org/log/hostname
I've looked up in the C source how `hostname --short` does it:

it will just truncate at the first dot, if there is any.

Resolving patch:
diff --git a/src/borg/platform/base.py b/src/borg/platform/base.py
--- a/src/borg/platform/base.py
+++ b/src/borg/platform/base.py
@@ -210,6 +210,9 @@ def getfqdn(name=''):
 # XXX this sometimes requires live internet access for issuing a DNS query in the background.
 hostname = socket.gethostname()
 fqdn = getfqdn(hostname)
+# some people put the fqdn into /etc/hostname (which is wrong, should be the short hostname)
+# fix this (do the same as "hostname --short" cli command does internally):
+hostname = hostname.split('.')[0]
 
 # uuid.getnode() is problematic in some environments (e.g. OpenVZ, see #3968) where the virtual MAC address
 # is all-zero. uuid.getnode falls back to returning a random value in that case, which is not what we want.


Test patch: