Create a Log file for Rsync if not exists Create a Log file for Rsync if not exists shell shell

Create a Log file for Rsync if not exists


Okay so after good guidance by the coolest people who answered below I was able to solve this problem. Actually the problem was that if you use --log-file with rsync and provide the logfile a directory which does not exist it will give you an error that the log file does not exist.

But if the provided directory exists then the logfile is automatically created within the provided directory. Here is the updated syntax I am using

mkdir -p -v $HOME/public_html/rsynclogs/rsync -azP --stats source-directory user@host.com:destination-directory --log-file=$HOME/public_html/rsynclogs/rsync-backup-log-$(date +"%Y-%m-%d").log

Now mkdir creates a directory. As suggested by @janek I added -p just so it checks if the directory exists and only creates if it does not exist.

Other Updates in Syntax:

Replaced ~ with $HOME. Suggested by @l'L'l

Replaced backticks(``) with $( ... ). Suggested by @l'L'l


The problem is the tilde (~) in your path. You either have to remove the = or use:

--log-file="$HOME/public_html/rsynclogs/rsync-backup-log-$(date +"%Y-%m-%d".log)"

Also backticks are legacy and should be replaced by $( ... )


An easy way would be to execute touch to create an empty file before executing rsync:

touch `date +"%Y-%m-%d"`.logrsync <options>

touch creates an empty file if the file name doesn't exist, but if the file does exist it simply updates the last modified time.