Aggiunto script del backup nel file volumebk.md

This commit is contained in:
piccihud 2024-02-25 20:12:02 +01:00
parent b03e8984d0
commit be9e705ef0
1 changed files with 66 additions and 0 deletions

View File

@ -102,6 +102,72 @@ Permissions Size User Date Modified Name
.rw-r--r-- 259 root 25 Feb 15:42  snikket-data_bk_2024-02-25.tar.gz .rw-r--r-- 259 root 25 Feb 15:42  snikket-data_bk_2024-02-25.tar.gz
``` ```
## Script di esempio
```bash
#!/bin/bash
TOPIC="nomedeltopic"
SERVER="ntfy.server.net"
BACKUP_DIR="/etc/snikket/backup"
CONTAINER_LIST=$(docker container ls -q)
# Lista di immagini con il tag 'stable'
IMAGE_LIST=$(docker images --filter "reference=snikket/*:stable" -q)
VOLUME_LIST=$(docker volume ls --format '{{.Name}}')
# Funzioni per inviare notifiche
send_success_notification() {
curl -H "X-Tags: tada" -H "Attach: https://yanbin.blog/wp-content/uploads/2020/03/docker-2.png" -d "Backup volume $VOLUME_NAME OK! :)" $SERVER/$TOPIC
}
# Funzione per inviare notifica di errore
send_error_notification() {
curl -H "X-Tags: skull" -d "Backup volume $VOLUME_NAME ERROR!" $SERVER/$TOPIC
}
notify_backup_removed() {
curl -H "X-Tags: warning" -d "Some backups have been deleted" $SERVER/$TOPIC
}
# Rimozione dei backup piu' vecchi di 7 giorni
remove_old_backups() {
if [ find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +7 -exec rm {} \; ]; then
notify_backup_removed
else
echo "Nessun backup eliminato!"
fi
}
### BACKUP ####
cd $BACKUP_DIR
# Commit per ogni container nella lista
for CONTAINER_ID in $CONTAINER_LIST; do
docker container commit $CONTAINER_ID || send_error_notification
done
# tr (translate or delete characters):
# tr: un comando che serve per tradurre o eliminare caratteri in un flusso di dati.
# tr '/' '-' traduce ogni carattere '/' in '-'
# Backup delle immagini
for IMAGE_ID in $IMAGE_LIST; do
# Ottieni il nome dell'immagine con il tag
IMAGE_NAME_WITH_TAG=$(docker inspect --format='{{.RepoTags}}' $IMAGE_ID | tr -d '[] ')
docker image save -o $(echo $IMAGE_NAME_WITH_TAG | cut -d '/' -f 2 | tr ':' '-')_$(date +%F)_IMAGE.tar.gz $IMAGE_NAME_WITH_TAG || send_error_notification
done
# Backup dei volumi
for VOLUME_NAME in $VOLUME_LIST; do
docker run --rm --mount source=$VOLUME_NAME,target=/$VOLUME_NAME -v $(pwd):/backup busybox tar -czvf /backup/$VOLUME_NAME\_bk_$(date +%F)_VOLUME.tar.gz /$VOLUME_NAME && send_success_notification || send_error_notification
done
# Rimozione dei backup piu' vecchi di 7 giorni
remove_old_backups
```
## Collegamenti ## Collegamenti
- [https://docs.docker.com/desktop/backup-and-restore/](https://docs.docker.com/desktop/backup-and-restore/) - [https://docs.docker.com/desktop/backup-and-restore/](https://docs.docker.com/desktop/backup-and-restore/)