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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With