From 6a7de97c63b506e44419888a88e6c0e18fe0a0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20P=C3=A0mies?= Date: Thu, 20 Jul 2023 09:42:40 +0200 Subject: [PATCH] test scripts and examples --- backup_nextcloud.sh | 46 +++++++++++++++++++++++++++++ colors.sh | 10 +++++++ nextcloud_updater.sh | 17 +++++++++++ remote_updater.sh | 10 ++++--- spinner.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++ testsleep.sh | 4 +++ 6 files changed, 153 insertions(+), 4 deletions(-) create mode 100755 backup_nextcloud.sh create mode 100755 colors.sh create mode 100755 nextcloud_updater.sh create mode 100755 spinner.sh create mode 100755 testsleep.sh diff --git a/backup_nextcloud.sh b/backup_nextcloud.sh new file mode 100755 index 0000000..238a002 --- /dev/null +++ b/backup_nextcloud.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# A script to perform incremental backups using rsync + +set -o errexit +set -o nounset +set -o pipefail + +readonly SOURCE_DIR="/home/roger/prova" +readonly BACKUP_DIR="/home/roger/backups" +readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')" +readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}" +readonly LATEST_LINK="${BACKUP_DIR}/latest" + +mkdir -p "${BACKUP_DIR}" + +rsync -av --delete \ + "${SOURCE_DIR}/" \ + --link-dest "${LATEST_LINK}" \ + --exclude=".cache" \ + "${BACKUP_PATH}" + +rm -rf "${LATEST_LINK}" +ln -s "${BACKUP_PATH}" "${LATEST_LINK}" + + + + + + +#rsync -AaxzvPh --delete /home/roger/prova/ --link-dest /home/roger/latest /home/roger/prova_inc1 +#ln -s /home/roger/prova_inc1 /home/roger/latest + +# add file into prova + +rsync -rti --delete --dry-run /home/roger/prova/ /home/roger/latest/ 2>&1 | grep -v '^[^*]d' | head - +n1 + +#rsync -rtiv --link-dest "../latest/" "/home/roger/prova/" "/home/roger/provabkp_inc1" +sending incremental file list +created directory /home/roger/backups/provabkp_inc1 +cd+++++++++ prova/ +>f+++++++++ prova/file1.txt +>f+++++++++ prova/file2.txt +>f+++++++++ prova/file3.txt +>f+++++++++ prova/file4.txt diff --git a/colors.sh b/colors.sh new file mode 100755 index 0000000..b49e159 --- /dev/null +++ b/colors.sh @@ -0,0 +1,10 @@ +#! /usr/bin/env bash + +RED="\e[31m" +GREEN="\e[32m" +ENDCOLOR="\e[0m" + +echo -e "${RED}This is some red text, ${ENDCOLOR}" +echo -e "${GREEN}And this is some green text${ENDCOLOR}" + +#https://misc.flogisoft.com/bash/tip_colors_and_formatting diff --git a/nextcloud_updater.sh b/nextcloud_updater.sh new file mode 100755 index 0000000..bd291a6 --- /dev/null +++ b/nextcloud_updater.sh @@ -0,0 +1,17 @@ +sudo -u www-data php /mnt/md0/nextcloud/updater/updater.phar --no-interaction + +if no sudo: + +su -l www-data -s /bin/bash -c 'php /opt/nextcloud/updater/updater.phar --no-interaction' + +----- no updates ---- + +Nextcloud Updater - version: v20.0.0beta4-11-g68fa0d4 + +Current version is 23.0.12. + +No update available. + +Nothing to do. + +-------- diff --git a/remote_updater.sh b/remote_updater.sh index b30ef4f..4288801 100755 --- a/remote_updater.sh +++ b/remote_updater.sh @@ -73,12 +73,14 @@ elevate_cmd () { } remote_update () { -ssh $1 << EOF +ssh -t $1 << EOF $(typeset -f has_sudo) $(typeset -f elevate_cmd) - elevate_cmd apt update -qq - elevate_cmd apt upgrade -y - elevate_cmd apt autoremove + elevate_cmd apt-get update -qq + elevate_cmd echo 'debconf debconf/frontend select Noninteractive' | elevate_cmd debconf-set-selections + elevate_cmd apt-get upgrade -y + elevate_cmd apt-get autoremove + elevate_cmd echo 'debconf debconf/frontend select Dialog' | elevate_cmd debconf-set-selections EOF } diff --git a/spinner.sh b/spinner.sh new file mode 100755 index 0000000..a0b1aa6 --- /dev/null +++ b/spinner.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +FRAME_INTERVAL=0.1 +RED="\e[31m" +GREEN="\e[32m" +ENDCOLOR="\e[0m" + +start_spinner() { + #local step=0 + local CMD=$1 + local STEP=$2 + + tput civis -- invisible + + #while [ "$step" -lt "${#CMDS[@]}" ]; do + #${CMDS[$step]} & pid=$! + $CMD & pid=$! + #status=$($CMD & pid=$!) + #status=$? + #pid=$! + + while ps -p $pid &>/dev/null; do + #while kill -0 $pid 2>/dev/null; do + #echo -ne "\\r[ ] ${STEPS[$step]} ..." + echo -ne "\\r[ ] $STEP ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] " + sleep $FRAME_INTERVAL + done + done + + wait "$pid"; status=$! + #echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}" + #echo -ne "\\r[ ✔ ] ${STEPS[$step]}\\n" + #re='^[0-9]+$' + #if ! [[ $status =~ $re ]]; then + # echo $status + #else + if [ $status -eq 0 ]; then + echo -ne "\\r[ ${GREEN}✔${ENDCOLOR} ] $STEP\\n" + else + echo -ne "\\r[ ${RED}✗${ENDCOLOR} ] $STEP\\n" + fi + #fi + #step=$((step + 1)) + #done + + tput cnorm -- normal +} + +start_spinner "apt update -qq" "Search for updates" +start_spinner "apt upgrade -qq -y" "Apply updates" +start_spinner "./testsleep.sh" "Testing fail" #ha de donar FAIL!! +start_spinner "rm *.mp3" "Testing fail" +./testsleep.sh +echo $? + + +#/usr/bin/scp me@website.com:file somewhere 2>/dev/null & +#apt update -qq > /dev/null 2>/dev/null & pid=$! # Process Id of the previous running command +#spin='-\|/' + +#i=0 +#while kill -0 $pid 2>/dev/null +#do +# i=$(( (i+1) %4 )) +# printf "\r${spin:$i:1}" +# sleep .1 +#done diff --git a/testsleep.sh b/testsleep.sh new file mode 100755 index 0000000..e5a3356 --- /dev/null +++ b/testsleep.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +sleep 3 +exit 1