Semaphores and castles in Ubuntu

I have a backup server that receives many rsyncconnections every hour . Since too many open instances rsynccan crash it, I want to limit the number of concurrent instances using Semaphore . What I mean is something like:

ssh root@backup_server "get_semaphore"     #Will hold until semaphore released
rsync -avzrL --super --delete local_directory root@backup_server:`localhost`

Any ideas?

+3
source share
1 answer

In my opinion, you should look at limiting the number of simultaneous connections directly on the server side, so you do not need to do anything extreme on the client side:

(1) If you run rsync in daemon mode, an option exists for rsyncd.conf max connections.

(2) Linux iptables connlimit limit . , rsync , ssh, sshd.

(3) rsync script, -server, rsync , . script , N . .

#!/bin/bash

N=5

mutex_hold() {
    while ! mkdir /var/lock/rsync/mutex 2>/dev/null; do
       sleep 1
    done
}

mutex_release() {
    rmdir /var/lock/rsync/mutex
}

if [[ "$1" = "--server" ]]; then
    shopt -s nullglob

    while mutex_hold && A=(/var/lock/rsync/[0-9]*) && [[ "${#A[@]}" -ge "$N" ]] && mutex_release; do
       sleep 1
    done

    touch /var/lock/rsync/$$

    mutex_release

    rsync.bin "$@"

    rm -f /var/lock/rsync/$$
else
    rsync.bin "$@"
fi

, script , , . - .. .

, , , lockfiles , ( ) .

, .

, , script .

+12

Source: https://habr.com/ru/post/1777734/


All Articles