Last active 1 month ago

Full backup zip + all compose file ! Compose Docker + Report Html

Erreur32's Avatar Erreur32 revised this gist 6 months ago. Go to revision

1 file changed, 1 insertion, 7 deletions

Backup-Home-docker.tar.gz.sh

@@ -245,13 +245,7 @@ DURATION="$(format_time $SECONDS)"
245 245
246 246
247 247
248 - log_message "----- RÉSUMÉ GLOBAL -----"
249 - log_message "Nombre de .tar.gz : $TOTAL_TAR"
250 - log_message "Backup le plus ancien : ${OLDEST_BACKUP:-N/A}"
251 - log_message "Backup le plus récent : ${NEWEST_BACKUP:-N/A}"
252 - log_message "Archive courante : $BACKUP_FILE"
253 - log_message "Dossier config courant : $CONFIG_DIR"
254 - log_message "Durée totale : $DURATION"
248 +
255 249
256 250 echo -e "${GREEN}===== BACKUP TERMINÉ =====${NC}"
257 251 echo -e "${CYAN}----- DOCKER -----${NC}"

Erreur32's Avatar Erreur32 revised this gist 6 months ago. Go to revision

1 file changed, 1 insertion, 12 deletions

Backup-Home-docker.tar.gz.sh

@@ -243,18 +243,7 @@ DURATION="$(format_time $SECONDS)"
243 243 # 6) RÉSUMÉS
244 244 ##############################################
245 245
246 - log_message "----- RÉSUMÉ DOCKER -----"
247 - log_message "Compose source (hors CasaOS) : $SOURCE_COMPOSE_COUNT"
248 - log_message "Compose backup (config/docker) : $DOCKER_COMPOSE_BACKUP"
249 - log_message "Dossier compose Docker backup : $CONFIG_DIR/docker"
250 -
251 - log_message "----- RÉSUMÉ CASAOS -----"
252 - log_message "Compose CasaOS backup (config/casaos/<container>) : $CASAOS_COMPOSE_BACKUP"
253 - log_message "Dossier CasaOS backup : $CONFIG_DIR/casaos"
254 -
255 - log_message "----- RÉSUMÉ PORTAINER -----"
256 - log_message "Fichiers config Portainer (full_config) : $PORTAINER_FILES"
257 - log_message "Dossier Portainer backup : $PORTAINER_CONFIG_DIR"
246 +
258 247
259 248 log_message "----- RÉSUMÉ GLOBAL -----"
260 249 log_message "Nombre de .tar.gz : $TOTAL_TAR"

Erreur32's Avatar Erreur32 revised this gist 6 months ago. Go to revision

1 file changed, 270 insertions, 56 deletions

Backup-Home-docker.tar.gz.sh

@@ -1,73 +1,287 @@
1 1 #!/bin/bash
2 2
3 - # echosystem.fr HOME erreur32
4 - # need pigz (apt-get install pigz)
3 + #############################
4 + # CONFIG
5 + #############################
5 6
6 - Bpath="/home/backup/"
7 - Bfile="Backup-$HOSTNAME-HOME-DOCKER.tar.gz"
8 - Blink=$Bpath$Bfile
9 - #echo "Check Path : $Blink"
10 - DIRECTORYB="/home/docker/"
7 + # Répertoire contenant tes containers
8 + SOURCE_DIR="/home/docker"
9 +
10 + # Répertoire où stocker les sauvegardes
11 + BACKUP_DIR="/home/backup/docker"
12 +
13 + # Nombre max de backups à conserver
14 + MAX_BACKUPS=5
15 +
16 + # Date format
17 + DATE="$(date +'%Y%m%d_%H%M%S')"
18 +
19 + # Fichier TAR final
20 + BACKUP_FILE="$BACKUP_DIR/docker_backup_${DATE}.tar.gz"
21 +
22 + # Dossier de configs des compose à côté du TAR
23 + CONFIG_DIR="$BACKUP_DIR/config_${DATE}"
24 +
25 + # Dossiers à exclure (chemins relatifs à $SOURCE_DIR)
26 + EXCLUDE_DIRS=(
27 + "casaos_data/*"
28 + "nginx_proxy/data/logs/*"
29 + "yt-dl/video/*"
30 + )
31 +
32 + # LOGS
33 + LOG_FILE="/var/log/docker_backup.log"
34 +
35 + #############################
36 + # COULEURS
37 + #############################
38 + NC="\e[0m"
39 + GREEN="\e[32m"
40 + YELLOW="\e[33m"
41 + CYAN="\e[36m"
42 + RED="\e[31m"
43 +
44 + #############################
45 + # FONCTIONS
46 + #############################
47 +
48 + log_message() {
49 + local msg="$1"
50 + local now
51 + now="$(date +'%Y-%m-%d %H:%M:%S')"
52 + echo "$now - $msg" >> "$LOG_FILE"
53 + echo -e "$now - $msg"
54 + }
11 55
12 56 format_time() {
13 - ((h=${1}/3600))
14 - ((m=(${1}%3600)/60))
15 - ((s=${1}%60))
57 + local t=$1
58 + ((h=t/3600))
59 + ((m=(t%3600)/60))
60 + ((s=t%60))
16 61 printf "%02d:%02d:%02d\n" $h $m $s
17 - }
18 -
19 - tar_lebackup() {
20 - tar -cf $Blink -I pigz \
21 - --directory=$DIRECTORYB \
22 - --exclude=.composer \
23 - --exclude=.aptitude \
24 - --exclude=.cache \
25 - --exclude=.cmake \
26 - --exclude=.yarn \
27 - --exclude=.w3m \
28 - --exclude=.pip \
29 - --exclude=.pm2 \
30 - --exclude=.pm \
31 - --exclude=.bundle \
32 - --exclude=.cache \
33 - --exclude=.gem \
34 - --exclude=.cpan \
35 - --exclude=.cpanm \
36 - --exclude=.gem \
37 - --exclude=.git-credential-cache \
38 - --exclude=.gnupg \
39 - --exclude=.local \
40 - --exclude=.npm \
41 - --exclude=.nvm \
42 - --exclude=.rvm \
43 - --exclude=.git \
44 - --exclude=.msf4 \
45 - --exclude=node_modules \
46 - --exclude=lost+found . && echo -e " ✅ BACKUP $Blink 🆗" || echo "Houton we have a problem with TAR..."
47 62 }
48 63
49 - tar_lebackup3() {
50 - tar --exclude="resolv.conf" --exclude-backups fcz $Blink -C /etc/ && echo -e " \n ✅ BACKUP $Blink 🆗" || echo "Houton we have a problem with TAR..."
64 + check_pigz() {
65 + if ! command -v pigz >/dev/null 2>&1; then
66 + echo -e "${YELLOW}pigz n'est pas installé. Voulez-vous l’installer ? (y/n)${NC}"
67 + read rep
68 + if [ "$rep" = "y" ]; then
69 + sudo apt update && sudo apt install -y pigz || {
70 + echo -e "${RED}Échec de l’installation de pigz. Abandon.${NC}"
71 + exit 1
72 + }
73 + else
74 + echo -e "${RED}pigz est requis pour ce script. Abandon.${NC}"
75 + exit 1
76 + fi
77 + fi
51 78 }
52 79
80 + rotate_backups() {
81 + log_message "Nettoyage des anciens backups (conserver les $MAX_BACKUPS plus récents)..."
82 +
83 + # TAR
84 + mapfile -t backups_tar < <(ls -1t "$BACKUP_DIR"/docker_backup_*.tar.gz 2>/dev/null || true)
85 + if [ "${#backups_tar[@]}" -gt "$MAX_BACKUPS" ]; then
86 + for f in "${backups_tar[@]:$MAX_BACKUPS}"; do
87 + log_message "Suppression ancien backup tar : $f"
88 + rm -f "$f"
89 + done
90 + fi
91 +
92 + # Dossiers config_
93 + mapfile -t backups_cfg < <(ls -1td "$BACKUP_DIR"/config_* 2>/dev/null || true)
94 + if [ "${#backups_cfg[@]}" -gt "$MAX_BACKUPS" ]; then
95 + for d in "${backups_cfg[@]:$MAX_BACKUPS}"; do
96 + log_message "Suppression ancien dossier config : $d"
97 + rm -rf "$d"
98 + done
99 + fi
100 + }
101 +
102 + #############################
103 + # DÉBUT SCRIPT
104 + #############################
105 +
106 + SECONDS=0
107 +
108 + check_pigz
109 +
110 + mkdir -p "$BACKUP_DIR" "$CONFIG_DIR"
111 + touch "$LOG_FILE"
112 +
113 + echo -e "${CYAN}=== Backup Docker + Configs (Portainer / CasaOS) ===${NC}"
114 + log_message "Début sauvegarde Docker + configs Portainer & CasaOS..."
115 +
116 + ##############################################
117 + # 1) Sauvegarde TAR via pigz + exclusions
118 + ##############################################
119 + log_message "Création de l’archive : $BACKUP_FILE"
120 +
121 + # Construction des arguments d'exclusion dynamiques
122 + EXCLUDE_ARGS=(
123 + "--exclude=*.log"
124 + "--exclude=.composer"
125 + "--exclude=.aptitude"
126 + "--exclude=.cache"
127 + "--exclude=.cmake"
128 + "--exclude=.yarn"
129 + "--exclude=.w3m"
130 + "--exclude=.pip"
131 + "--exclude=.pm2"
132 + "--exclude=.pm"
133 + "--exclude=.bundle"
134 + "--exclude=.gem"
135 + "--exclude=.cpan"
136 + "--exclude=.cpanm"
137 + "--exclude=.git"
138 + "--exclude=.local"
139 + "--exclude=.npm"
140 + "--exclude=.nvm"
141 + "--exclude=.rvm"
142 + "--exclude=node_modules"
143 + "--exclude=lost+found"
144 + "--exclude=casaos_data"
145 + )
146 +
147 + for d in "${EXCLUDE_DIRS[@]}"; do
148 + EXCLUDE_ARGS+=( "--exclude=$d" )
149 + done
150 +
151 + tar -cf "$BACKUP_FILE" -I pigz \
152 + --directory="$SOURCE_DIR" \
153 + "${EXCLUDE_ARGS[@]}" \
154 + . 2>> "$LOG_FILE"
53 155
54 - if [ -f $Blink ]; then
55 - echo -e "\n 📦 Old backup Detected ... moving Files"
56 - echo -n " "; du -shx $Blink
57 - rm -f $Blink
58 - echo -e " 🔥 Old Backup 💣 DELETED ! "
156 + if [ $? -ne 0 ]; then
157 + log_message "ERREUR lors de la création du tar."
158 + echo -e "${RED}Erreur lors de la création de l’archive.${NC}"
159 + exit 1
160 + fi
161 +
162 + ##############################################
163 + # 2) Extraction des docker-compose
164 + # - CasaOS: config_<date>/casaos/<container>/
165 + # - Docker: config_<date>/docker/<container>/
166 + ##############################################
167 +
168 + log_message "Extraction des docker-compose CasaOS..."
169 + if [ -d "$SOURCE_DIR/casaos_data" ]; then
170 + find "$SOURCE_DIR/casaos_data" -maxdepth 3 -type f -name "docker-compose.y*ml" | while read file; do
171 + container_name=$(basename "$(dirname "$file")")
172 + dest_dir="$CONFIG_DIR/casaos/$container_name"
173 + mkdir -p "$dest_dir"
174 + cp "$file" "$dest_dir/"
175 + log_message " [CasaOS] → Compose trouvé : $file"
176 + done
177 + fi
178 +
179 + log_message "Extraction des docker-compose Docker (hors casaos_data)..."
180 + find "$SOURCE_DIR" -type f -name "docker-compose.y*ml" ! -path "$SOURCE_DIR/casaos_data/*" | while read file; do
181 + container_name=$(basename "$(dirname "$file")")
182 + dest_dir="$CONFIG_DIR/docker/$container_name"
183 + mkdir -p "$dest_dir"
184 + cp "$file" "$dest_dir/"
185 + log_message " [Docker] → Compose trouvé : $file"
186 + done
187 +
188 + ##############################################
189 + # 3) Config Portainer (full)
190 + ##############################################
191 + PORTAINER_CONFIG_DIR="$CONFIG_DIR/portainer/full_config"
192 + PORTAINER_FILES=0
193 +
194 + if docker volume inspect portainer_data >/dev/null 2>&1; then
195 + log_message "Sauvegarde config Portainer..."
196 + mkdir -p "$PORTAINER_CONFIG_DIR"
197 + cp -r /var/lib/docker/volumes/portainer_data/_data/* "$PORTAINER_CONFIG_DIR/" 2>>"$LOG_FILE"
198 + PORTAINER_FILES=$(find "$PORTAINER_CONFIG_DIR" -type f 2>/dev/null | wc -l)
59 199 else
60 - echo -e " ⛔ No old Backup found. "
200 + log_message "Portainer non détecté (volume portainer_data absent)."
61 201 fi
62 - echo -e " 🔥 Creation file starting ... "
63 - tar_lebackup
64 - if [ -f $Blink ]; then
65 - echo -n " 📌 "; du -shx $Blink
66 - echo -e "\n 🆗 BACKUP SUCCESS"
202 +
203 + ##############################################
204 + # 4) Config CasaOS (full)
205 + ##############################################
206 + CASAOS_CONFIG_DIR="$CONFIG_DIR/casaos/full_config"
207 + CASAOS_FILES=0
208 +
209 + if [ -d "/var/lib/casaos" ]; then
210 + log_message "Sauvegarde config CasaOS (full)..."
211 + mkdir -p "$CASAOS_CONFIG_DIR"
212 + cp -r /var/lib/casaos/* "$CASAOS_CONFIG_DIR/" 2>>"$LOG_FILE"
213 + CASAOS_FILES=$(find "$CONFIG_DIR/casaos" -type f -name "docker-compose.y*ml" ! -path "$CONFIG_DIR/casaos/full_config/*" 2>/dev/null | wc -l)
67 214 else
68 - echo -e " \n ==> ⛔ BACKUP ERROR "
215 + log_message "CasaOS non détecté (/var/lib/casaos manquant)."
69 216 fi
70 - echo -e " 🕑 Script completed in $(format_time $SECONDS)"
71 217
218 + ##############################################
219 + # 5) Comptages finaux (sur le backup courant)
220 + ##############################################
221 +
222 + # Compose dans le backup (docker + casaos compose seulement)
223 + CASAOS_COMPOSE_BACKUP=$(find "$CONFIG_DIR/casaos" -maxdepth 3 -type f -name "docker-compose.y*ml" ! -path "$CASAOS_CONFIG_DIR/*" 2>/dev/null | wc -l)
224 + DOCKER_COMPOSE_BACKUP=$(find "$CONFIG_DIR/docker" -type f -name "docker-compose.y*ml" 2>/dev/null | wc -l)
225 + TOTAL_COMPOSE_BACKUP=$((CASAOS_COMPOSE_BACKUP + DOCKER_COMPOSE_BACKUP))
226 +
227 + # Compose dans le dossier source (hors casaos_data)
228 + SOURCE_COMPOSE_COUNT=$(find "$SOURCE_DIR" -type f -name "docker-compose.y*ml" ! -path "$SOURCE_DIR/casaos_data/*" 2>/dev/null | wc -l)
229 +
230 + # Rotation des anciens backups
231 + rotate_backups
232 +
233 + # Nombre total de tar.gz
234 + TOTAL_TAR=$(ls -1 "$BACKUP_DIR"/docker_backup_*.tar.gz 2>/dev/null | wc -l)
235 +
236 + # Oldest / newest après rotation
237 + OLDEST_BACKUP=$(ls -1 "$BACKUP_DIR"/docker_backup_*.tar.gz 2>/dev/null | head -n1)
238 + NEWEST_BACKUP=$(ls -1 "$BACKUP_DIR"/docker_backup_*.tar.gz 2>/dev/null | tail -n1)
239 +
240 + DURATION="$(format_time $SECONDS)"
241 +
242 + ##############################################
243 + # 6) RÉSUMÉS
244 + ##############################################
245 +
246 + log_message "----- RÉSUMÉ DOCKER -----"
247 + log_message "Compose source (hors CasaOS) : $SOURCE_COMPOSE_COUNT"
248 + log_message "Compose backup (config/docker) : $DOCKER_COMPOSE_BACKUP"
249 + log_message "Dossier compose Docker backup : $CONFIG_DIR/docker"
250 +
251 + log_message "----- RÉSUMÉ CASAOS -----"
252 + log_message "Compose CasaOS backup (config/casaos/<container>) : $CASAOS_COMPOSE_BACKUP"
253 + log_message "Dossier CasaOS backup : $CONFIG_DIR/casaos"
254 +
255 + log_message "----- RÉSUMÉ PORTAINER -----"
256 + log_message "Fichiers config Portainer (full_config) : $PORTAINER_FILES"
257 + log_message "Dossier Portainer backup : $PORTAINER_CONFIG_DIR"
258 +
259 + log_message "----- RÉSUMÉ GLOBAL -----"
260 + log_message "Nombre de .tar.gz : $TOTAL_TAR"
261 + log_message "Backup le plus ancien : ${OLDEST_BACKUP:-N/A}"
262 + log_message "Backup le plus récent : ${NEWEST_BACKUP:-N/A}"
263 + log_message "Archive courante : $BACKUP_FILE"
264 + log_message "Dossier config courant : $CONFIG_DIR"
265 + log_message "Durée totale : $DURATION"
266 +
267 + echo -e "${GREEN}===== BACKUP TERMINÉ =====${NC}"
268 + echo -e "${CYAN}----- DOCKER -----${NC}"
269 + echo -e "${CYAN}Compose source (hors CasaOS) : ${GREEN}$SOURCE_COMPOSE_COUNT${NC}"
270 + echo -e "${CYAN}Compose backup (Docker) : ${GREEN}$DOCKER_COMPOSE_BACKUP${NC}"
271 + echo -e "${CYAN}Dossier compose Docker : ${YELLOW}$CONFIG_DIR/docker${NC}"
272 +
273 + echo -e "${CYAN}----- CASAOS -----${NC}"
274 + echo -e "${CYAN}Compose CasaOS backup : ${GREEN}$CASAOS_COMPOSE_BACKUP${NC}"
275 + echo -e "${CYAN}Dossier CasaOS backup : ${YELLOW}$CONFIG_DIR/casaos${NC}"
276 +
277 + echo -e "${CYAN}----- PORTAINER -----${NC}"
278 + echo -e "${CYAN}Fichiers config Portainer : ${GREEN}$PORTAINER_FILES${NC}"
279 + echo -e "${CYAN}Dossier Portainer backup : ${YELLOW}$PORTAINER_CONFIG_DIR${NC}"
72 280
73 - ### extract tar -zxvpf /fullbackup.tar.gz
281 + echo -e "${CYAN}----- GLOBAL -----${NC}"
282 + echo -e "${CYAN}Nombre de .tar.gz : ${GREEN}$TOTAL_TAR${NC}"
283 + echo -e "${CYAN}Oldest backup : ${YELLOW}${OLDEST_BACKUP:-N/A}${NC}"
284 + echo -e "${CYAN}Newest backup : ${YELLOW}${NEWEST_BACKUP:-N/A}${NC}"
285 + echo -e "${CYAN}Archive courante : ${YELLOW}$BACKUP_FILE${NC}"
286 + echo -e "${CYAN}Dossier config courant : ${YELLOW}$CONFIG_DIR${NC}"
287 + echo -e "${CYAN}Durée totale : ${GREEN}$DURATION${NC}"

Erreur32's Avatar Erreur32 revised this gist 6 months ago. Go to revision

1 file changed, 73 insertions

Backup-Home-docker.tar.gz.sh(file created)

@@ -0,0 +1,73 @@
1 + #!/bin/bash
2 +
3 + # echosystem.fr HOME erreur32
4 + # need pigz (apt-get install pigz)
5 +
6 + Bpath="/home/backup/"
7 + Bfile="Backup-$HOSTNAME-HOME-DOCKER.tar.gz"
8 + Blink=$Bpath$Bfile
9 + #echo "Check Path : $Blink"
10 + DIRECTORYB="/home/docker/"
11 +
12 + format_time() {
13 + ((h=${1}/3600))
14 + ((m=(${1}%3600)/60))
15 + ((s=${1}%60))
16 + printf "%02d:%02d:%02d\n" $h $m $s
17 + }
18 +
19 + tar_lebackup() {
20 + tar -cf $Blink -I pigz \
21 + --directory=$DIRECTORYB \
22 + --exclude=.composer \
23 + --exclude=.aptitude \
24 + --exclude=.cache \
25 + --exclude=.cmake \
26 + --exclude=.yarn \
27 + --exclude=.w3m \
28 + --exclude=.pip \
29 + --exclude=.pm2 \
30 + --exclude=.pm \
31 + --exclude=.bundle \
32 + --exclude=.cache \
33 + --exclude=.gem \
34 + --exclude=.cpan \
35 + --exclude=.cpanm \
36 + --exclude=.gem \
37 + --exclude=.git-credential-cache \
38 + --exclude=.gnupg \
39 + --exclude=.local \
40 + --exclude=.npm \
41 + --exclude=.nvm \
42 + --exclude=.rvm \
43 + --exclude=.git \
44 + --exclude=.msf4 \
45 + --exclude=node_modules \
46 + --exclude=lost+found . && echo -e " ✅ BACKUP $Blink 🆗" || echo "Houton we have a problem with TAR..."
47 + }
48 +
49 + tar_lebackup3() {
50 + tar --exclude="resolv.conf" --exclude-backups fcz $Blink -C /etc/ && echo -e " \n ✅ BACKUP $Blink 🆗" || echo "Houton we have a problem with TAR..."
51 + }
52 +
53 +
54 + if [ -f $Blink ]; then
55 + echo -e "\n 📦 Old backup Detected ... moving Files"
56 + echo -n " "; du -shx $Blink
57 + rm -f $Blink
58 + echo -e " 🔥 Old Backup 💣 DELETED ! "
59 + else
60 + echo -e " ⛔ No old Backup found. "
61 + fi
62 + echo -e " 🔥 Creation file starting ... "
63 + tar_lebackup
64 + if [ -f $Blink ]; then
65 + echo -n " 📌 "; du -shx $Blink
66 + echo -e "\n 🆗 BACKUP SUCCESS"
67 + else
68 + echo -e " \n ==> ⛔ BACKUP ERROR "
69 + fi
70 + echo -e " 🕑 Script completed in $(format_time $SECONDS)"
71 +
72 +
73 + ### extract tar -zxvpf /fullbackup.tar.gz
Newer Older