LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get time and use rsync in same script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-time-and-use-rsync-in-same-script-4175593772/)

rsi142 11-18-2016 01:28 AM

How to get time and use rsync in same script
 
Actually i need to copy the file from remote server using rsync and also need to get the date because the name of the file is access.2016-11-16.log
and i need to get the file in daily basis so the next day the name of the file will be access.2016-11-17.log
my code
#!/usr/bin/expect

#w="$(date)"
#printf "Current date and time %s\n" "$now"

now="$(date +'%Y-%m-%d')"
#printf "Current date in dd/mm/yyyy format %s\n" "$now"
x="access."$now
echo $x

spawn rsync --inplace --no-whole-file root@10.207.5.1:/app/hybris/hybris/l...2016-11-16.log .

#spawn rsync -v root@10.207.16.45:/u02/opt/weblogic/...ogs/access.log .
expect "*root@10.207.5.199's password:" {
send "******"
expect "\r"
expect "*\r"
}

spawn rsync --inplace --no-whole-file root@10.207.5.199:/app/hybris/hybris...e-20161116.log .

#spawn rsync -v root@10.207.16.3:/u02/opt/weblogic/d...ogs/access.log .
expect "*root@10.207.5.199's password:" {
send "******\r"
expect "\r"
expect "*\r"
}

pan64 11-18-2016 01:54 AM

please use [code][/code] tags around your code to keep formatting.
I do not really understand, you have only one access.log and want to copy day by day using different names, or do you have new file(s) every day?

rsi142 11-18-2016 02:04 AM

actually everyday new logs are created and name is also new
name would be like
acces.presentdate.log

rsi142 11-18-2016 02:10 AM

Answer to the question
 
#!/bin/sh

#w="$(date)"
#printf "Current date and time %s\n" "$now"

now="$(date +'%Y-%m-%d')"
#printf "Current date in dd/mm/yyyy format %s\n" "$now"
x="access."$now
y=x."log"
echo $y
expect <<EOS

#!/usr/bin/expect
spawn rsync --inplace --no-whole-file root@10.207.5.199:/app/hybris/hybris/log/tomcat/$x.log .

#spawn rsync -v root@10.207.16.45:/u02/opt/weblogic/...ogs/access.log .
expect "*root@10.207.5.199's password:" {
send "password\r"
expect "\r"
expect "*\r"
}

spawn rsync --inplace --no-whole-file root@10.207.5.192323:/app/hybris/hyb...e-20161116.log .

#spawn rsync -v root@10.207.16.45:/u02/opt/weblogic/...ogs/access.log .
expect "*root@10.207.5.2323's password:" {
send "password\r"
expect "\r"
expect "*\r"
}
EOS

pan64 11-18-2016 02:44 AM

if you could set ssh keys:
Code:

filename="$(date +'access.%Y-%m-%d.log')"
rsync --inplace --no-whole-file root@10.207.5.199:/app/hybris/hybris/log/tomcat/$filename .

will be enough (or something similar), no need to use expect
but anyway rsync is able to copy only new files, so probably you only need to rsync directory and new file(s) will be automatically found and transferred (no need to construct the name of it).


All times are GMT -5. The time now is 07:30 PM.