Compare commits

..

1 Commits

Author SHA1 Message Date
Sem
7804314d3c WIP maybe convert bash scripts to c? 2026-07-11 23:21:17 +02:00
5 changed files with 13 additions and 107 deletions

BIN
c-scratchbook/main Executable file

Binary file not shown.

View File

@@ -0,0 +1,10 @@
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int rsync_res = system("rsync");
printf("%s", rsync_res);
return 0;
}

View File

@@ -1,27 +0,0 @@
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No argument supplied. Use this script with:"
echo "send-zabbix-backup-info.sh <servicename> <duration_ms> <files_created> <bytes_sent> <end_date>"
exit 0
fi
SERVICE_NAME=$1
DURATION_MS=$2
FILES_CREATED=$3
BYTES_SENT=$4
END_DATE=$5
if ! command -v zabbix_sender >/dev/null 2>&1
then
echo "zabbix_sender could not be found. Is the package zabbix-sender installed?"
exit 1
fi
echo " == Sending backup information to zabbix"
# send completed
zabbix_sender -s "$(hostname)" -k "backup.status[$SERVICE_NAME] -o 1"
zabbix_sender -s "$(hostname)" -k "backup.duration[$SERVICE_NAME] -o $DURATION_MS"
zabbix_sender -s "$(hostname)" -k "backup.files[$SERVICE_NAME] -o $FILES_CREATED"
zabbix_sender -s "$(hostname)" -k "backup.bytes[$SERVICE_NAME] -o $BYTES_SENT"
zabbix_sender -s "$(hostname)" -k "backup.date[$SERVICE_NAME] -o '$END_DATE'"

View File

@@ -5,8 +5,7 @@
if [ $# -eq 0 ]
then
echo "No argument supplied. Use this script with:"
echo "tar-and-backup-service.sh <servicename> <foldername> [-z]"
echo "Use the -z flag to also send information about the backup to zabbix"
echo "tar-and-backup-service.sh <servicename> <foldername>"
exit 0
fi
@@ -14,34 +13,10 @@ SERVICE_NAME=$1
BACKUP_FOLDER=$2
TAR_FILE=$SERVICE_NAME.tar.gz
START_MS=$(date +%s%3N)
echo " == Backing up $SERVICE_NAME to $TAR_FILE"
echo " == Creating tar.gz file"
tar czvf $TAR_FILE $BACKUP_FOLDER
echo " == transfering tar file"
TMPFILE=$(mktemp)
rsync -avz --stats --progress $TAR_FILE sem@10.10.100.52:/home/sem/$SERVICE_NAME/ | tee "$TMPFILE"
RSYNC_OUTPUT="$(cat $TMPFILE)"
rm "$TMPFILE"
rsync -avz --stats --progress $TAR_FILE sem@10.10.100.52:/home/sem/$SERVICE_NAME/
echo " == Removing local tar file"
rm $TAR_FILE
BYTES_SENT=$(echo "$RSYNC_OUTPUT" | grep "Total transferred file size" | cut -d: -f2 | cut -d' ' -f2 | tr -d ' ' | tr -d ',')
FILES_CREATED=$(echo "$RSYNC_OUTPUT" | grep "Number of regular files transferred" | cut -d: -f2 | tr -d ' ')
END_MS=$(date +%s%3N)
DURATION_MS=$((END_MS - START_MS))
END_DATE=$(date "+%d-%m-%Y %H:%M:%S")
echo " == Backup completed in $DURATION_MS ms"
echo " == Total bytes sent: $BYTES_SENT"
echo " == Total files created: $FILES_CREATED"
if [ "$3" == "-z" ]
then
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
"$SCRIPT_DIR/send-zabbix-backup-info.sh" $SERVICE_NAME $DURATION_MS $FILES_CREATED $BYTES_SENT "$END_DATE"
fi
rm $TAR_FILE

View File

@@ -1,52 +0,0 @@
#!/bin/bash
# script to send info about a backup to zabbix.
# Usage:
# update-zabbix-backup-status.sh [-sf] HOSTNAME SERVICE_NAME [DURATION] [SIZE]
# parameters:
# -s Backup succeeded (required if not failed)
# -f Backup failed (required if not succeeded)
# HOSTNAME hostname of the host that performed the backup
# SERVICE_NAME name of the service that was backed up
# DURATION duration of the backup
# SIZE Size in bytes of the transfered files
function print_help
{
echo "script to send info about a backup to zabbix."
echo "Usage:"
echo " update-zabbix-backup-status.sh [-sf] HOSTNAME SERVICE_NAME [DURATION] [SIZE]"
echo "parameters:"
echo " -s Backup succeeded (required if not failed)"
echo " -f Backup failed (required if not succeeded)"
echo " -h Show this message"
echo " HOSTNAME hostname of the host that performed the backup"
echo " SERVICE_NAME name of the service that was backed up"
echo " DURATION duration of the backup. Only needed if succeeded"
echo " SIZE Size in bytes of the transfered files. Only needed if succeeded"
}
if [ -z $1 ]
then
echo "Please provide arguments. Use -h to show help"
exit 0
fi
if [ "$1" = "-h" ]
then
print_help
fi
if ! command -v zabbix_sender >/dev/null 2>&1
then
echo "zabbix_sender could not be found. Is it and the zabbix_agent2 installed?"
exit 1
fi
# TODO implement sending items to zabbix
# Use zabbix_sender directly? (for some reason not installed on games vm) https://www.zabbix.com/documentation/current/en/manual/concepts/sender
# Or create python script and use python library? https://www.zabbix.com/documentation/current/en/devel/python
# if using python script I can run the rsync backup command using subprocess, and catch the output of that
# then I can check the bytes sent to calculate the size