Create a Log file for Rsync if not exists
Asked Answered
P

3

5

Im writing a simple bash script where the bash script will sync my live servers with my staging servers. I am using rsync to do this.

What I need is a log file for each day the script was executed. I am using the following command

rsync -azP --stats source-directory [email protected]:destination-directory --log-file=~/public_html/rsynclogs/rsync-backup-log-`date +"%Y-%m-%d"`.log

The error this command gives me is

rsync: failed to open log-file ~/public_html/rsynclogs/rsync-backup-log-2017-01-11.log: No such file or directory (2)
Ignoring "log file" setting.

So most probably it is looking for an existing log file but I want it to be created if it does not exist. Please advice on how I can achieve this.

Pantechnicon answered 11/1, 2017 at 10:19 Comment(0)
P
14

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 [email protected]: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

Pantechnicon answered 12/1, 2017 at 12:25 Comment(1)
Thanks to this post I was able to determine 3 valid forms that work!: --log-file=$HOME/path OR --log-file="$HOME/path" OR --log-file ~/pathDissipated
M
3

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 $( ... )

Martellato answered 11/1, 2017 at 10:42 Comment(5)
replacing $HOME with all ~ still gives me the same error. Also I didnt understand what do you mean by backticks? Im sorry im new to this..Pantechnicon
Does the directory you are trying to write the file exist (eg. $HOME/public_html/rsynclogs/ aka ~/public_html/rsynclogs/, aka /Users/YourUser/public_html/rsynclogs/)? Create the directory first if it doesn't exist. Also remove the = from --log-file=, so it would be --logfile "~/path"... it's required if you use ~. Backticks are the ticks ( ` ) you have around your date cmd..Disposal
No it doesnot. But I want to create it if it does not exist. is it possible?Pantechnicon
Yes, just create it now by doing mkdir ~/public_html/rsynclogs, then it should accept your logfile when you rsync. Sometimes you might need to change permissions on it too, but creating it is the first step.Disposal
Huge thanks for pointing this out! Bash can be so tricky sometimes... Even after years, sometimes I miss stuff like this; the syntax is super picky and your suggestion helped me realize my mistake.Dissipated
B
2

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

touch `date +"%Y-%m-%d"`.log
rsync <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.

Bantling answered 11/1, 2017 at 10:25 Comment(4)
Upon using touch it also gives an error on touch touch: cannot touch ~/public_html/rsynclogs/rsync-backup-log-2017-01-11.log': No such file or directory Most probably it is also looking for a directory that does not exist by the name rsynclogsPantechnicon
In that case either create the rsynclogs directory beforehand or every time in the script using mkdir -p <path>Bantling
can we check if the directory exists do not create it if it does then do create it?Pantechnicon
@FahadSohail That's what the -p flag for mkdir does - creates the directory if necessary, doesn't return error if it exists.Bantling

© 2022 - 2024 — McMap. All rights reserved.