Outils pour utilisateurs

Outils du site


blog

Exemple de script rsync d'envoie de fichiers via SSH - pushlogs

QQOQCP

Quoi:

  • Envoie de fichiers logs des composants suivants : xxx01, xxx02

Où:

  • Le serveur destinataire est un serveur SFTP (SSH) renseigné dans le fichier pushlogs.env
  • La liste des fichiers à envoyer est défine dans le fichier pushlogs.lst

Quand:

  • Définie dans le(s) crontab de l'utilisateur(s) propriétaire des fichiers
  • Normalement il s'agit de l'utilisateur 'plopuser'

Qui:

  • Auteur: JBL
  • Pour: ACME

Comment:

  • Une crontab de l'utilsateur 'plopuser' (ou autre) appelle le script bash pushlogs.sh
  • pushlogs.sh utilise la commande rsync en utilisant le protocole SSH
  • L'authentification se fait par clef SSH (RSA 4096)
  • pushlogs.sh va lire le fichier .pushlogs.env et .pushlogs.lst présent à la racine du HOMEDIR

Prérequis

  • Les commandes suivants doivent être présentes : rsync

Options

Général
  • Le script ne prend aucun argument
  • Les options sont présentes et documentées dans le fichier .pushlogs.env
Crontab

Les crontab peuvent utiliser nice et ionice afin de limiter les ressources. L'envoie des logs étant de priorité inférieur aux traitements métier. Exemple (dans /var/spool/cron/plopuser) :

# Push logs
05 02 * * * nice -n 10 ionice -n 7 bash ~/scripts/pushlogs.sh >> ~/logs/pushlogs.txt 2>> ~/logs/pushlogs.err
~/.pushlogs.env
  • Voir ce fichier.
  • Une limitation de la bande passe permet de ne pas impacter le métier (BMLIMIT)
  • Il peut être utilse de limité l'envoie des logs aux seules fichiers récemment modifiés. Exemple pour les fichiers des 2 derniers jours : FIND_OPTS='-mtime -2
~/.pushlogs.lst
  • Contient la liste dans fichiers / dossier à envoyer
  • Le chemin est relatif à l'homedir de l'utilisateur (normalement '/home/plopuser/')
  • Il est possible d'utiliser des wildcards “*”. Exemple :
./app*/L*/logs_backup/

Génération des clefs

Les paires de clefs ont été générées de la façon suivante :

ssh-keygen -N '' -t rsa -b 4096 -C "XXX01_rsync_pushlog" -f id_xxx01_rsync_pushlog
ssh-keygen -N '' -t rsa -b 4096 -C "XXX02_rsync_pushlog" -f id_xxx02_rsync_pushlog

.pushlogs.env

# Limit network bandwidth in KiB/s
BMLIMIT=5000
 
# root dir to backup. Should not be changed
BACKUP_ROOT_DIR="$HOME"
 
# SSH settings
SSH_USER=plopuser
SSH_HOST=localhost
SSH_DIR=/tmp/plop
#SSH_PORT: See SSH_OPTS. Example: SSH_OPTS='-p 2222'
 
# Extra find options
FIND_OPTS=''
#FIND_OPTS='-mtime -2'
 
# Extra rsync options
RSYNC_OPTS='-c --itemize-changes'
 
# Extra SSH rsync options
SSH_OPTS='-p 22'

.pushlogs.env

./app*/L*/logs_backup/
./app*/L*/log/
./app*/L*/templog
./app*/scripts/backup_logfiles_l*.out

pushlogs.sh

#! /bin/bash
 
set -euo pipefail
IFS=$' \t\n'
export LC_ALL=C
 
SCRIPT_NAME=$(basename "$0")
PIDFILE=/dev/shm/${SCRIPT_NAME%.sh}.pid
ENVFILE=${HOME}/.${SCRIPT_NAME%.sh}.env
LISTSCRFILE=${HOME}/.${SCRIPT_NAME%.sh}.lst
 
trap 'rm -f "$PIDFILE"' EXIT ERR
 
echo "$(date --rfc-3339=second)" DEBUT EXECUTION
 
set +e
if [ -f "$PIDFILE" ]; then
    PLOP_PID=$(head -1 "$PIDFILE")
    echo "WARNING EALREADY: Operation already in progress (114)" >&2
    kill "$PLOP_PID" >/dev/null 2>&1
    sleep 5
    kill -9 "$PLOP_PID" >/dev/null 2>&1
    sleep 5
    if [ -d /proc/"$PLOP_PID" ]; then
        echo "ERROR EDEADLOCK: Resource deadlock avoided (35)" >&2
        exit 35
    else
        rm -f "$PIDFILE"
    fi
else
    echo $$ >"$PIDFILE"
fi
set -e
 
# shellcheck source=pushlogs.env
source "$ENVFILE"
 
main() {
    echo "$(date --rfc-3339=second)" DEBUT COPIE
    cd "$BACKUP_ROOT_DIR" || exit 2
 
    # shellcheck disable=SC2068,SC2046
    find $(grep -v '^#' "$LISTSCRFILE") -type f ${FIND_OPTS[@]} -print0 |
        rsync -e "ssh -o StrictHostKeyChecking=no -o CheckHostIP=no ${SSH_OPTS[*]}" \
            --bwlimit="$BMLIMIT" ${RSYNC_OPTS[@]} --files-from=- --from0 "${BACKUP_ROOT_DIR}" "${SSH_USER}"@"${SSH_HOST}":"${SSH_DIR}"
 
    cd -
    echo "$(date --rfc-3339=second)" FIN
}
 
main

FIXME

2025/10/30 10:30 · Jean-Baptiste

Notes dinit

Présentation

type = process | bgprocess | scripted | internal | triggered
command = ...
stop-command = ...
run-as = (user-id)
restart = (boolean)
logfile = ...
pid-file = ... (Pour bgprocess)
options: ...
depends-on: (service name) (Hard dependency / Need dependency)
depends-ms: (service name) (Milestone)
waits-for: (service name) (Soft dependency)

Install

Debian

Voir : https://repo.antixlinux.com/testing/pool/main/d/dinit/

wget https://repo.antixlinux.com/testing/pool/main/d/dinit/dinit_0.19.4-0_amd64.deb
mkdir dinit_tmp
cd dinit_tmp
ar x ../dinit_0.19.4-0_amd64.deb
tar xvf data.tar.xz
sudo cp -p lib/dinit/dinit* /usr/local/bin/
sudo rsync -ax usr/share/man/ /usr/local/share/man/
Android - Termux

Source : https://github.com/davmac314/dinit/issues/274

pkg install ndk-sysroot clang binutils-is-llvm make git
pkg install m4
git clone https://github.com/davmac314/dinit
cd dinit
 
git tag
git checkout v0.19.4
 
./configure --prefix=$PREFIX --syscontrolsocket=$PREFIX/var/run/dinitctl --sbindir=$PREFIX/bin --disable-cgroups --disable-shutdown
make -j $(nproc)
make install
Termnux boot

Installer Termux-Boot

cd .termux
mkdir boot
cd boot
 
cat > 00-dinit <<EOF
#! /bin/sh
 
termux-wake-lock
dinit
EOF
 
termux-fix-shebang ./00-dinit

Usage

$ dinit --version
Dinit version 0.19.4.
Supported features: cgroups utmp supplemental-groups
mkdir ~/.config/dinit.d
cd ~/.config/dinit.d
 
cat > ~/.config/dinit.d/boot <<EOF
type = internal
waits-for.d: boot.d
EOF
 
mkdir /.config/dinit.d/boot.d
Exemple de création de service

Voir :

cat > ~/.config/dinit.d/test_sleep <<EOF
type = process
command = sleep 600
restart = true
EOF
 
dinit
 
dinitctl start test_sleep

État

dinitctl list
 
dinitctl status mysql

Vérif

dinitcheck
 
# ou
 
dinitcheck <service-name>

Config

Lancement automatique de dinit

~/.bashrc

if ! pgrep -u "$USER" dinit > /dev/null; then
  nohup dinit --user -q -l ~/log/dinit.log >/dev/null 2>&1 &
fi

Source : Adaptation de https://wiki.artixlinux.org/Main/Dinit#User_services

Pb

oct 17 17:44:45 vivabelo systemd[2332]: nerdctl-3249c67de5a1a127575835e672573bd05d1fd478bca842293869a7bc2a0a1539.scope: Consumed 2min 37.055s CPU time, 2.1G memory peak, 430M read from disk, 82.7M written to disk.
oct 17 17:44:45 vivabelo containerd-rootless.sh[2454]: time="2025-10-17T17:44:45.171069378+02:00" level=error msg="ttrpc: received message on inactive stream" stream=9

Pb

Err provided file is not a console
Exited (137)
1 errors:\nprovided file is not a console

Solution

apt-get install expect

~/.config/dinit.d/kind

type = process
 
# command = $HOME/bin/nerdctl start --attach kind-control-plane
command = unbuffer $HOME/bin/nerdctl start --attach kind-control-plane
 
stop-command = $HOME/bin/nerdctl stop kind-control-plane
restart = false
logfile = /home/jibe/log/kind.log
stop-timeout = 120

Préfixer la commande par unbuffer

Mais ne marche pas :

  • stdbuf -i0 -o0 -e0 command
  • script --return --quiet --command “rancher exec -ti …” /dev/null
2025/10/11 21:11 · Jean-Baptiste

Notes Ansible EDA - Event-Driven Ansible - ansible-rulebook

Voir :

Ou comment réinventer le FAAS

FIXME

2025/10/09 22:01 · Jean-Baptiste

Notes PID1 conteneurs

Généralités

Voir aussi :

Design - supervision system vs service manager vs system manager - “hard” and “soft” dependencies.

Init process :

Init process pour les conteners :

L'entrypoint devrait être un init tel que “catatonit”

Something like dumb-init or tini can be used if you have a process that spawns new processes and you don't have good signal handlers implemented to catch child signals and stop your child if your process should be stopped etc.

If your process doesn't spawn new processes (e.g. Node.js), then this may not be necessary.

I guess that MongoDB, PostgreSQL, ... which may run child processes have good signal handlers implemented. Otherwise there would have been zombie processes and someone would have filed an issue to fix this.

Only problem may be the official language images, like node, ruby, golang. They don't have dumb-init/tini in it as you normally don't need them. But it's up to the developer which may implement bad child execution code to either fix the signal handlers or use helper as PID 1.

Source : https://stackoverflow.com/questions/37374310/how-critical-is-dumb-init-for-docker

Sans gestion de services

Voir :

Voir :

  • tini
  • dumb-init
  • catatonit (recommandé), utilisé par Podman - inspiré de tini et de dumb-init
  • pid1 (paquet pid1) https://github.com/fpco/pid1

Avec gestion de services

Voir :

Pas classé

Voir :

2025/10/09 21:46 · Jean-Baptiste
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki