Tuesday, March 15, 2022

[SOLVED] Tar files created in last 15 minutes and SCP them to my local machine. (Using Cygwin)

Issue

I am trying to create a shell script to make tar out of a selected list of files with filename starting with AppLog , created within last 15 minutes on a remote server. The tar is then supposed to scp the files to my local machine.

My current problem is that the find option fetches files and lists it, but when the tar is created only the recent 1 file is zipped even though there might be more than 1 item in the find output. Please review my script below and advise.

Please note, I am using Cygwin to run this script on Win7.

#!/bin/bash
Systime=`date +%F-%H%M%S`
sshpass -p xxxxxx ssh [email protected] "cd /path/to/dir/with/logs && find AppLog* -cmin -15 -type f -exec tar -zcvf /dest/sys01-$Systime.tar.gz {} \;"
sshpass -p xxxxxx scp [email protected]:/dest/sys01-$Systime.tar.gz . 
sshpass -p xxxxxx ssh [email protected] "rm -rf /dest/sys01-$Systime.tar.gz"

Solution

The find command is executing tar for every files found. So you are overwriting the tar file every time for this reason only the last file is present.

You need to execute the tar with the full list, only one time.

cd /path/to/dir/with/logs && find AppLog* -cmin -15 -type f | xargs tar -zcvf /dest/sys01-$Systime.tar.gz 


Answered By - matzeri
Answer Checked By - Terry (WPSolving Volunteer)