LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-19-2024, 05:32 AM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Rep: Reputation: Disabled
SSH -rsync


Hi Folks,

I encountered a strange problem. There is a video encoder that constantly records files, and I created this syncroniser script.
If I --remove-source-files parameter to rsync it senses that a file is beeing written by another process, and skips that file. I tested it on my laptop and it really skips it, but not always. On the server side some files are truncated to 828 bytes. Now I user it without that option, but I would really like to remove the files after sync:

Code:
#!/bin/sh
set -x

MAC=$(ip a | grep ether | head -1 | awk '{print $2}' | sed 's/:/-/g')
CONFIG="/usr/bin/syncroniser.cfg"
KEY="/usr/bin/dropbear_rsa_host_key"
SRC="/media/sdmmc/mmcblk0p1/RECORD/"
DST="$MAC"
FREQ=60
while true
    do
        if test -f "$CONFIG"; then
            . "$CONFIG"
        fi
        rsync -avme "ssh -i $KEY -p $REMOTE_PORT" $SRC $REMOTE_USER@$REMOTE_IP:$DST 
        sleep $FREQ
    done
I was trying to pipe to rsync with find and xargs, but it still did that file truncation on the file beeing written.

Code:
find $SRC -type f -exec sh -c 'if ! lsof `readlink -f {}` > /dev/null; then echo `basename {}`; fi' \; | tr '\n' '\0' | rsync -avme "ssh -i $KEY -p $REMOTE_PORT" $SRC $REMOTE_USER@$REMOTE_IP:$DST

Last edited by kzo81; 03-19-2024 at 05:44 AM.
 
Old 03-19-2024, 05:52 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,311
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
There's a lot which appears undefined in the script as presented. The variables $REMOTE_PORT, $REMOTE_USER, $REMOTE_IP, and $DST seem undefined. The variable $MAC seems unused but if you are going to fetch it using the formula which you have given, then it can all be done in AWK:

Code:
MAC=$(ip a | awk '/link\/ether/ { gsub(":", "-", $2); print $2; exit; };')
The ip utility can take the name of the specified interface as an option too.
 
Old 03-19-2024, 06:20 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Do you mean you want to copy a file which is actually being written? That won't work, you cannot make it work. You need to skip that file. Next time you can copy/move it when it's saved and ready.

Instead of
Code:
echo `basename {}`
you can simply write
Code:
basename {}
Also would be nice to use shellcheck to validate your script.
 
1 members found this post helpful.
Old 03-19-2024, 07:17 AM   #4
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
This is shell checked :-)
Code:
set -x

MAC=$(ip a | grep ether | head -1 | awk '{print $2}' | sed 's/:/-/g')
CONFIG="/usr/bin/syncroniser.cfg"
KEY="/usr/bin/dropbear_rsa_host_key"
SRC="/media/sdmmc/mmcblk0p1/RECORD/"
DST="$MAC"
FREQ=60
while true
    do
        if test -f "$CONFIG"; then
            . "$CONFIG"
        fi
        find $SRC -type f -exec sh -c 'if ! lsof `readlink -f {}` > /dev/null; then basename {}; fi' \; | tr '\n' '\0' | rsync -avme "ssh -i $KEY -p $REMOTE_PORT" "$SRC" "$REMOTE_USER"@"$REMOTE_IP":"$DST" 
        sleep $FREQ
    done
 
Old 03-22-2024, 05:04 AM   #5
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
Hi Folks,

Do you have idea why I cannot use this script with systemD anymore? It was running seamlessly before I added the red line.

Code:
#!/bin/sh
set -x

MAC=$(ip a | grep ether | head -1 | awk '{print $2}' | sed 's/:/-/g')
CONFIG="/usr/bin/syncroniser.cfg"
KEY="/usr/bin/dropbear_rsa_host_key"
SRC="/media/sdmmc/mmcblk0p1/RECORD/"
EXPORTS="/media/sdmmc/mmcblk0p1/RECORD/EXPORTS/"
DST="$MAC"
FREQ=60

while true
    do
        if test -f "$CONFIG"; then
            . "$CONFIG"
        fi

        mkdir -p "$EXPORTS";

        find $SRC -name *.mkv -mmin +1 -exec mv "{}" "$EXPORTS" \;
        rsync --remove-source-files -avme "ssh -i $KEY -p $REMOTE_PORT" $EXPORTS $REMOTE_USER@$REMOTE_IP:$DST        

        sleep $FREQ

    done
 
Old 03-22-2024, 08:08 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,710

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Code:
 find $SRC -name *.mkv -mmin +1 -exec mv "{}" "$EXPORTS" \;
What exactly isn't working correctly? What sticks out is that by default find is recursive. I have not tried testing your code but try adding -maxdepth 0 and you need quotes around *.mkv

Last edited by michaelk; 03-22-2024 at 08:11 AM.
 
Old 03-22-2024, 12:19 PM   #7
lvm_
Member
 
Registered: Jul 2020
Posts: 928

Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
If your video encoder runs on the same linux machine, why don't you hook you script to it - via inotifywait, if there are no more straightforward means. And if it is a separate device which exposes its storage to linux as memory card, lsof trick won't work as it cannot see files opened there. As for that 'find $SRC -name *.mkv...' line, you have to enquote the file mask, otherwise it will be expanded prematurely.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on ssh authorization for rsync daemon server and rsync ssh shell framp Linux - Security 2 11-29-2021 09:47 AM
rsync over ssh VS rsync.d RISKS tripialos Linux - Security 4 02-20-2013 06:22 PM
[SOLVED] rsync fails in cron - ssh key prob for rsync? jonathansfl Linux - Server 6 12-09-2010 09:48 AM
Rsync server vs rsync over ssh humbletech99 Linux - Networking 1 10-18-2006 12:10 PM
Windows Rsync Upload to Linux Rsync - permissions inspleak Linux - Software 0 10-12-2004 02:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration