Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Log file for Rsync if not exists

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.

like image 958
Fahad Sohail Avatar asked Dec 06 '22 15:12

Fahad Sohail


1 Answers

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

like image 51
Fahad Sohail Avatar answered Dec 11 '22 08:12

Fahad Sohail