Files
Maison/Migration/Ansible/roles/samba4-dc/templates/samba-restore.sh.j2
2026-02-10 12:12:11 +01:00

135 lines
3.9 KiB
Django/Jinja

#!/bin/bash
# Samba4 Simple Restore Script
# Generated by Ansible
BASE_BACKUP_DIR="{{ backup_dir | default('/backup/samba') }}"
HOSTNAME="{{ target_hostname }}"
SAMBA_DIR="/var/lib/samba"
ETC_DIR="/etc/samba"
{% raw %}
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
echo -e "${CYAN}🔄 Samba4 Restore Script"
echo "==============================${NC}"
# Check if backup directory exists
if [ ! -d "$BASE_BACKUP_DIR" ]; then
echo -e "${RED}❌ Backup directory not found: $BASE_BACKUP_DIR${NC}"
exit 1
fi
# List available backups
echo -e "${YELLOW}📁 Available backups for ${HOSTNAME}:${NC}"
echo ""
backup_files=($(ls -1t "$BASE_BACKUP_DIR"/${HOSTNAME}*.tgz 2>/dev/null))
if [ ${#backup_files[@]} -eq 0 ]; then
echo -e "${RED}❌ No backup files found for ${HOSTNAME}${NC}"
exit 1
fi
# Display backup files with index and timestamp
for i in "${!backup_files[@]}"; do
file="${backup_files[$i]}"
filename=$(basename "$file")
filesize=$(du -h "$file" | cut -f1)
timestamp=$(stat -c %y "$file" | cut -d'.' -f1)
echo -e "${GREEN}[$((i+1))]${NC} $filename"
echo " 📅 Created: $timestamp"
echo " 📦 Size: $filesize"
echo ""
done
# Ask user which backup to restore
echo -n -e "${YELLOW}Select backup to restore [1-${#backup_files[@]}]: ${NC}"
read -r selection
# Validate selection
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#backup_files[@]} ]; then
echo -e "${RED}❌ Invalid selection${NC}"
exit 1
fi
selected_backup="${backup_files[$((selection-1))]}"
echo -e "${GREEN}✅ Selected: $(basename "$selected_backup")${NC}"
echo ""
# Ask for restore location
echo -e "${YELLOW}Restore options:${NC}"
echo "1) In place (replace current Samba installation)"
echo "2) To custom directory"
echo ""
echo -n -e "${YELLOW}Choose option [1-2]: ${NC}"
read -r restore_option
case "$restore_option" in
1)
# In-place restore
echo -e "${YELLOW}⚠️ WARNING: This will replace your current Samba installation!${NC}"
echo -n -e "${RED}Are you sure? Type 'YES' to continue: ${NC}"
read -r confirmation
if [ "$confirmation" != "YES" ]; then
echo -e "${YELLOW}🚫 Restore cancelled${NC}"
exit 0
fi
echo -e "${CYAN}🛑 Stopping Samba service...${NC}"
systemctl stop samba-ad-dc
echo -e "${CYAN}📦 Restoring backup directly to filesystem...${NC}"
tar -xzf "$selected_backup" -C /
echo -e "${GREEN}✅ Samba directories restored${NC}"
echo -e "${CYAN}🚀 Starting Samba service...${NC}"
systemctl start samba-ad-dc
# Check service status
sleep 3
if systemctl is-active --quiet samba-ad-dc; then
echo -e "${GREEN}✅ Samba restore completed successfully!${NC}"
echo -e "${GREEN}✅ Samba service is running${NC}"
else
echo -e "${RED}❌ Samba service failed to start${NC}"
echo "Check logs: journalctl -u samba-ad-dc"
fi
;;
2)
# Custom directory restore
echo -n -e "${YELLOW}Enter target directory: ${NC}"
read -r target_dir
if [ -z "$target_dir" ]; then
echo -e "${RED}❌ Target directory cannot be empty${NC}"
exit 1
fi
mkdir -p "$target_dir"
echo -e "${CYAN}📦 Extracting backup to $target_dir...${NC}"
tar -xzf "$selected_backup" -C "$target_dir"
echo -e "${GREEN}✅ Backup extracted to: $target_dir${NC}"
echo -e "${CYAN}📁 Contents:${NC}"
ls -la "$target_dir"
;;
*)
echo -e "${RED}❌ Invalid option${NC}"
exit 1
;;
esac
echo ""
echo -e "${GREEN}🎉 Restore operation completed!${NC}"
{% endraw %}