new script backup gitea, unban with remote f2b and update README

This commit is contained in:
Roger Pàmies Fabra 2023-12-07 23:22:39 +01:00
parent 297c047dc9
commit 9907ccef25
4 changed files with 58 additions and 38 deletions

View File

@ -1,5 +1,7 @@
# Bash Scripts collection # Bash Scripts collection
TODO: Need install packages for each script
## Permanent enviroment variables in ~/.profile ## Permanent enviroment variables in ~/.profile
Append file `~/.profile` with this variables: Append file `~/.profile` with this variables:
@ -49,3 +51,16 @@ Scan rootkits into system with rkhunter
Scan virus and malware into system with clamav Scan virus and malware into system with clamav
* Usage: `./scan_clam.sh` * Usage: `./scan_clam.sh`
### updates_notifier.sh
TODO!
## Some tips
* https://www.appsloveworld.com/bash/100/6/how-have-both-local-and-remote-variable-inside-an-ssh-command
* https://stackoverflow.com/questions/27932694/shell-script-ssh-server-eof (script detect expire users)
* use xargs: https://www.baeldung.com/linux/xargs-multiple-arguments
* load env vars in cron: https://www.baeldung.com/linux/load-env-variables-in-cron-job
* crontab need path sometimes: https://askubuntu.com/questions/117978/script-doesnt-run-via-crontab-but-works-fine-standalone
* errors and best practices: https://tecadmin.net/resolved-unary-operator-expected-error-in-bash/

19
backup_gitea.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
GOTIFY_TOKEN="token"
GOTIFY_MESSAGE="Backup task init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r"
GITEA_DIR="/path/to/gitea"
BACKUP_FILE="gitea-backup_"`date +\%Y\%m\%d`".zip"
REMOTE_SERVER="remote.example.com"
REMOTE_PATH="/path/to/remote/store/backups/"
# gitea
su - gitea -s /bin/bash -c "$GITEA_DIR/gitea dump -c $GITEA_DIR/custom/conf/app.ini --file $BACKUP_FILE --tempdir $GITEA_DIR/"
rsync -AaxzPh --remove-source-files "$GITEA_DIR/$BACKUP_FILE" "$REMOTE_SERVER:$REMOTE_PATH"
GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Backup task end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r"
# send gotify notification
sh ./gotifypush.sh "Gitea $(hostname) Backup" "$GOTIFY_MESSAGE" 5 "$GOTIFY_TOKEN"

24
f2b_unban_ip.sh Executable file
View File

@ -0,0 +1,24 @@
#!/bin/bash
#Use ./unbanipcloud.sh <ip>
echo $#
JAIL_NAME=$ES7_F2B_JAIL_NAME
REMOTE_SERVER="estudiset.nextcloud.handycat"
UNBAN_IP=$1
if [ $# -eq 0 ]; then
echo "Falta la IP a desbloquejar com a argument. Per exemple: $0 X.X.X.X"
exit 1
elif [ $# -eq 2 ]; then
JAIL_NAME=$2
fi
# unban with fail2ban
out=$(ssh $REMOTE_SERVER "sudo fail2ban-client set $JAIL_NAME unbanip $UNBAN_IP")
if [ "$out" -eq "1" ]; then
echo "La IP [$UNBAN_IP] s'ha desbloquejat correctament"
else
echo "No s'ha trobat la IP [$UNBAN_IP] a la llista de bloquejos"
fi

View File

@ -1,38 +0,0 @@
#!/bin/bash
#Use ./logger.sh <logfile> <relativepath>
# uncomment when use script from cron
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# variables
REMOTE_PATH="$ES7_LOGS_PATH$2"
PUBLIC_LOG_URL="$ES7_LOGS_URL/$2"
# get file name in case of $1 is a path
file=$(basename -- "$1")
extension=${file##*.}
base=$(basename "${file%.*}")
# create remote folder to store logs if not exist
# \$ allow read remote variable value
ssh $ES7_LOGS_SERVER_SSH "sudo mkdir -p $REMOTE_PATH && sudo chown -R \$(whoami): $REMOTE_PATH"
# Copy local logfile to remote destination
scp $1 "$ES7_LOGS_SERVER_SSH:$REMOTE_PATH"
# set correct permissions
ssh $ES7_LOGS_SERVER_SSH "chmod 644 $REMOTE_PATH$file"
# Clear x days old remote logs
ssh $ES7_LOGS_SERVER_SSH 'bash -s' << EOF
find $REMOTE_PATH -maxdepth 1 -mtime +$E7S_LOGS_PRESERVE_DAYS -exec "rm" -R {} \;
EOF
# return final log url
# change to txt if log is another text format
if [ $extension != "txt" ]; then
ssh $ES7_LOGS_SERVER_SSH "mv $REMOTE_PATH$file $REMOTE_PATH$base.txt"
fi
echo "${PUBLIC_LOG_URL}${base}.txt"