Backup script with date and other options

Hi all…
I try to create a script that will back-up my bck_folder
This is how the code looks like in .sh
( i use zsh. just to point )

#!/bin/zsh
today="$(/bin/date +%x-%R)-$(/usr/bin/whoami)-$(/bin/hostname)"
echo
echo $today
echo
tar -cvzf backup-$today.tar.tgz $HOME/Documents/bck_folder

and the result is this

./tared_all.sh
    
    26.10.2018-16:17-ralexander-lateralus
    
    tar (child): Cannot connect to backup-26.10.2018-16: resolve failed
    tar: Removing leading `/' from member names
    /home/ralexander/Documents/bck_folder/
    .
    .
    .
tar (child): Cannot connect to backup-26.10.2018-16: resolve failed
tar: Child returned status 128
tar: Error is not recoverable: exiting now

Any words of wisdom how to make it work ?!

1 Like

Hi @RobertAlexander

The issue is with the colon. From the GNU tar man page:

An archive name that has a colon in it specifies a file or
device on a remote machine. The part before the colon is taken
as the machine name or IP address, and the part after it as the
file or device pathname

I would suggest using an explicit format to avoid unwanted characters to leak into the date string (for example, on my system, “%x” returns a slash-separated date):

today="$(/bin/date +date +"%Y-%m-%dT%H%m")-$(/usr/bin/whoami)-$(/bin/hostname)"

I put the format with years on left, so most recent backups will appear last when sorted in lexicographic order (the default for ls for example).

5 Likes