Compare commits

9 Commits
cv ... main

Author SHA1 Message Date
c7d803e61a Update .gitea/workflows/kumonoboru.yaml 2024-02-09 12:00:12 +02:00
3853da701a Merge remote-tracking branch 'refs/remotes/origin/main'
Some checks failed
Configure Kumonoboru / deploy (push) Failing after 1m55s
2023-11-24 20:09:41 +02:00
4e67ef5394 Remove Prometheus file after 5 minutes; remove inotify dependency 2023-11-24 20:09:34 +02:00
0eba6d9caa Update .gitea/workflows/kumonoboru.yaml
All checks were successful
Configure Kumonoboru / deploy (push) Successful in 1m4s
2023-11-24 08:28:57 +02:00
d5842ba95a PROM file path fix
All checks were successful
Configure Kumonoboru / deploy (push) Successful in 1m4s
2023-11-23 22:21:18 +02:00
3d720ee591 Remove Okiro; moved to file-based definition
All checks were successful
Configure Kumonoboru / deploy (push) Successful in 1m6s
2023-11-23 22:18:49 +02:00
1cd7474734 Merge remote-tracking branch 'origin/main'
All checks were successful
Configure Kumonoboru / deploy (push) Successful in 1m10s
2023-11-22 09:27:30 +02:00
a7207954f8 Add Prometheus monitoring 2023-11-22 09:26:50 +02:00
7adc47279c Inventory is no more
All checks were successful
Configure Kumonoboru / deploy (push) Successful in 1m6s
2023-11-21 23:03:22 +02:00
5 changed files with 123 additions and 105 deletions

View File

@@ -1,36 +1,38 @@
name: Configure Kumonoboru
on:
push:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Python
run: |
apt -y update
apt -y install python3 python3-pip skopeo
- name: Install Ansible
run: |
python3 -m pip install --upgrade pip
pip install ansible
- name: Set up SSH
uses: webfactory/ssh-agent@v0.5.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Checkout Ansible inventory
run: |
INVENTORY_URL="https://shmick:${{ secrets.ANSIBLE_TOKEN }}@git.pukeko.xyz/shmick/ansible/raw/branch/main/inventory.yaml"
curl -o inventory.yaml $INVENTORY_URL
- name: Run Ansible Playbook
run: |
ansible-playbook -i inventory.yaml -e 'ANSIBLE_SUDO_PASS="${{ secrets.SUDO_PASS }}"' kumonoboru.yaml -vv
name: Configure Kumonoboru
#on:
# push:
# schedule:
# - cron: '30 08 * * *'
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Python
run: |
apt -y update
apt -y install python3 python3-pip skopeo
- name: Install Ansible
run: |
python3 -m pip install --upgrade pip
pip install ansible
- name: Set up SSH
uses: webfactory/ssh-agent@v0.5.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Checkout Ansible inventory
run: |
INVENTORY_URL="https://shmick:${{ secrets.ANSIBLE_TOKEN }}@git.pukeko.xyz/shmick/ansible/raw/branch/main/inventory.yaml"
curl -o inventory.yaml $INVENTORY_URL
- name: Run Ansible Playbook
run: |
ansible-playbook -i inventory.yaml -e 'ANSIBLE_SUDO_PASS="${{ secrets.SUDO_PASS }}"' kumonoboru.yaml -vv

3
.kumonoboru Normal file
View File

@@ -0,0 +1,3 @@
Gerbil-TK /var/Red-Vol/Media/Pictures
Pukeko-XYZ-Containers /var/Red-Vol/Media/Containers
Pukeko-XYZ-Cloud /var/Red-Vol/Media/Cloud

View File

@@ -1,5 +0,0 @@
all:
hosts:
takahe:
ansible_host: 192.168.0.66
ansible_user: shmick

View File

@@ -9,10 +9,6 @@ show_help()
echo " {-l|--limit} #[Kbps] -- Limit upload & download speed"
echo " {-v|--verbose} -- Print debug messages"
echo " {-h|--show_help} -- Print this show_help message and exit"
echo "Available repositories:"
echo "Gerbil-TK Photos (path: /var/Red-Vol/Media/Pictures)"
echo "Pukeko-XYZ-Containers Containers (path: /var/Red-Vol/Media/Containers)"
echo "Pukeko-XYZ-Cloud Data from all devices (path: /var/Red-Vol/Media/Cloud)"
exit 0
}
#Pass arguments to the script
@@ -44,6 +40,22 @@ flags()
}
flags "$@"
#File to write results to; picked up by Prometheus and yells about changes
PROM_FILE="/var/Red-Vol/Media/Containers/prometheus/data/kumonoboru.prom"
#Remove log from last runs, if present
if [[ -f $PROM_FILE ]]; then
rm $PROM_FILE
fi
## Monitoring codes:
#+ -3 - failed cleaning
#+ -2 - failed integrity check
#+ -1 - failed to unlock
#+ 0 - succesfully backed up
#+ 1 - failed backup
#+ 2 - passed integrity check
#+ 3- succesfully cleaned
#Defaults
if [[ -z $BWLIMIT ]]; then
export BWLIMIT="0"
@@ -59,17 +71,18 @@ fi
export B2_ACCOUNT_ID=8582a42a3b99 #Master Key
export B2_ACCOUNT_KEY=00041845e8dd29d7e3d091d77bb8a631ee71332be7 #Master Application ID
RESTIC_PASSWORD='f$774$#je4%U8vp8ov*UsZMHqL$m3Smh#fEbbt7hyULQxfnnWmSiS5MEndzVWT$$n^@s$P*o4vV*^rgv3jvvrv@y35VppU$$y*vnG5V@botU&4$39Y6t9HSb3Z548M!4'
export RESTIC_PASSWORD='f$774$#je4%U8vp8ov*UsZMHqL$m3Smh#fEbbt7hyULQxfnnWmSiS5MEndzVWT$$n^@s$P*o4vV*^rgv3jvvrv@y35VppU$$y*vnG5V@botU&4$39Y6t9HSb3Z548M!4'
#Safety function; accepts repository to check
safety(){
REPOSITORY="$1"
echo -e "Checking if repository is in use - " "$REPOSITORY"
echo -e "Checking if repository $REPOSITORY is in use "
#Check no other Restic process is using this repository; Free unnecessary locks, if present
if [[ -n $(ps aux | grep restic | grep "$REPOSITORY") ]]; then
echo -e "Repository is in use - ignoring"
return 1
echo "system_backup{name=\"$REPOSITORY\"} -1" >> $PROM_FILE
return 1 # code for ^ failed to unlock
# ^ If there's a restic process holding the repository, leave it alone.
else
echo -e "Repository is not in use - unlocking"
@@ -87,8 +100,10 @@ backup(){
echo -e "Backing up repository" "$REPOSITORY"
if restic --cache-dir="$RESTIC_CACHE_DIR" -r b2:"$REPOSITORY" backup "$REPOSITORY_PATH" --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT"; then
echo -e "$REPOSITORY_PATH" "completed upload to $REPOSITORY."
echo "system_backup{name=\"$REPOSITORY\"} 0" >> $PROM_FILE
else
echo -e "$REPOSITORY failed to upload path" "$REPOSITORY_PATH"
echo "system_backup{name=\"$REPOSITORY\"} 1" >> $PROM_FILE
fi
fi
}
@@ -96,20 +111,24 @@ backup(){
check(){
REPOSITORY="$1"
PRUNE="$2"
echo -e "Checking integrity (prune: $PRUNE) of repository" "$REPOSITORY"
echo -e "Checking integrity (prune: $PRUNE) of repository $REPOSITORY"
## ^ This variable will have value if repo is already clean, indicating
#+ This is a post backup check.
if [[ -n $PRUNE ]]; then
echo -e "This repository has been cleaned already; will not clean again."
fi
if safety "$REPOSITORY"; then
echo -e "Checking repository health - " "$REPOSITORY"
echo -e "Checking health of repository $REPOSITORY"
if restic -r b2:"$REPOSITORY" check --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT"; then
echo -e "Repository passed integrity check - " "$REPOSITORY"
echo -e "Repository $REPOSITORY passed integrity check"
echo "system_backup{name=\"$REPOSITORY\"} 2" >> $PROM_FILE
echo -e "Current snapshots:"
restic -r b2:"$REPOSITORY" snapshots | tee -a $LOG
else
echo -e "Repository failed integrity check - " "$REPOSITORY"
echo -e "Repository $REPOSITORY failed integrity check"
echo "system_backup{name=\"$REPOSITORY\"} -2" >> $PROM_FILE
# code for ^ failed integrity check
fi
fi
}
@@ -119,66 +138,65 @@ clean(){
if safety "$REPOSITORY"; then
echo -e "Cleaning repository" "$REPOSITORY"
if restic -r b2:$REPOSITORY forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT"; then
echo -e "Repository is trim - " "$REPOSITORY"
echo -e "Repository $REPOSITORY is clean"
echo "system_backup{name=\"$REPOSITORY\"} 3" >> $PROM_FILE
echo -e "Running post clean check..."
check "$REPOSITORY" "1"
# Marks repository as cleaned already ^ so it won't passed to this function again.
else
echo -e "Failed to prune repository" "$REPOSITORY"
echo -e "Failed to clean repository $REPOSITORY"
echo "system_backup{name=\"$REPOSITORY\"} -3" >> $PROM_FILE
# code for ^ failed pruning
fi
fi
}
#If cleaning was forced, or if it's the first of this month - clean.
if [[ -n $CLEAN ]] || [[ $(date +%d) == "1" ]]; then
check Gerbil-TK
clean Gerbil-TK
check Pukeko-XYZ-Containers
clean Pukeko-XYZ-Containers
check Pukeko-XYZ-Cloud
clean Pukeko-XYZ-Cloud
#If a specific repository was requested, back it up; otherwise, back them all up.
elif [[ -n $REPOSITORY ]] && [[ -z $CLEAN ]]; then
case "$REPOSITORY" in
(Gerbil-TK)
backup Gerbil-TK /var/Red-Vol/Media/Pictures/
;;
(Pukeko-XYZ-Containers)
backup Pukeko-XYZ-Containers /var/Red-Vol/Media/Containers/
;;
(Pukeko-XYZ-Cloud)
backup Pukeko-XYZ-Cloud /var/Red-Vol/Media/Cloud/
;;
(*)
show_help;;
esac
#If cleaning was not forced, backup the repositories
elif [[ -z $CLEAN ]]; then
backup Gerbil-TK /var/Red-Vol/Media/Pictures/
backup Pukeko-XYZ-Containers /var/Red-Vol/Media/Containers/
backup Pukeko-XYZ-Cloud /var/Red-Vol/Media/Cloud/
#If a specific repository was requested to be cleaned, clean it
elif [[ -n $REPOSITORY ]] && [[ -n $CLEAN ]]; then
case "$REPOSITORY" in
(Gerbil-TK)
check Gerbil-TK
clean Gerbil-TK
;;
(Pukeko-XYZ-Containers)
check Pukeko-XYZ-Containers
clean Pukeko-XYZ-Containers
;;
(Pukeko-XYZ-Cloud)
check Pukeko-XYZ-Cloud
clean Pukeko-XYZ-Cloud
;;
(*)
show_help;;
esac
REPO_FILE=".kumonoboru"
if [[ ! -f $REPO_FILE ]]; then
echo "Repository file $REPO_FILE is undefined. Please define $REPO_FILE."
echo "Format:"
echo "[B2-REPOSITORY] [LOCAL_PATH]"
echo "Example:"
echo "potato_tmp /tmp/potato"
exit 1
fi
if [[ -n $REPOSITORY ]]; then
repo_name=$(cat .kumonoboru | grep $REPOSITORY | awk '{print $1}')
repo_path=$(cat .kumonoboru | grep $REPOSITORY | awk '{print $2}')
if [[ -z $repo_name ]] || [[ -z $repo_path ]]; then
echo "Could not find repository $REPOSITORY"
else
REPOS["$repo_name"]=$repo_path
fi
else
declare -A REPOS
while read -r repo_entry; do
repo_name=$(echo "$repo_entry" | awk '{print $1}')
repo_path=$(echo "$repo_entry" | awk '{print $2}')
REPOS["$repo_name"]=$repo_path
done < .kumonoboru
fi
#Wrap up this run's log and report nicely
for repo in "${!REPOS[@]}"; do
repo_path=${REPOS[$repo]}
#If cleaning was forced, or if it's the first of this month - clean.
if [[ -n $CLEAN ]] || [[ $(date +%d) == "1" ]]; then
check $repo
clean $repo
#If a specific repository was requested, back it up; otherwise, back them all up.
elif [[ -z $CLEAN ]]; then
backup $repo $repo_path
fi
done
echo "All done; have a nice day!"
## Once the script finishes, the .prom file will live on for 2 minutes before being deleted.
#+ This allows Prometheus to pick up the alert, send out a notification, and move on with its life.
(
sleep 120
rm $PROM_FILE
) 2>1 >/dev/null &

View File

@@ -6,7 +6,7 @@
ansible_sudo_pass: "{{ ANSIBLE_SUDO_PASS }}"
tasks:
- name: Install Restic
- name: Install dependencies
ansible.builtin.package:
name: restic
state: latest