diff --git a/btrfs/snapshot-locales/btrfs-backup.service b/btrfs/snapshot-locales/btrfs-backup.service new file mode 100644 index 0000000..0c5632e --- /dev/null +++ b/btrfs/snapshot-locales/btrfs-backup.service @@ -0,0 +1,12 @@ +[Unit] +Description=Script de Snapshots BTRFS Diarios +After=network.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/bash /root/btrfs/init.sh +User=root +Group=root + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/btrfs/snapshot-locales/btrfs-backup.timer b/btrfs/snapshot-locales/btrfs-backup.timer new file mode 100644 index 0000000..128f811 --- /dev/null +++ b/btrfs/snapshot-locales/btrfs-backup.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Ejecutar Backup BTRFS a medianoche + +[Timer] +# Se ejecuta a las 00:00 todos los días +OnCalendar=*-*-* 00:00:00 +# Asegura que si el PC estaba apagado a medianoche, se ejecute al encender +Persistent=true + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/btrfs/snapshot-locales/init.sh b/btrfs/snapshot-locales/init.sh new file mode 100644 index 0000000..6810f6c --- /dev/null +++ b/btrfs/snapshot-locales/init.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +set -e + +# Declarar varables +dir_disk=("/dev/nvme0n1p3" "/dev/bcache0") + +dir_temp=("/tmp/root" "/tmp/srv") + +dir_create="/tmp/{root,srv}" + +name_snapshot=$(date +"%m%d%Y") + + + +# Crear ruta +mkdir -p "$dir_create" + +#Montar rutas + +for i in "${!dir_disk[@]}"; do + disk_origen="${dir_disk[$i]}" + dir_backup="${dir_temp[$i]}" + dir_snapshot="$dir_backup/snapshot/daily" + #Montar rutas + mount "$disk_origen" "$dir_backup" + + # Verificar que existan rutas, caso contrario crearlas + + [ -d "$dir_snapshot" ] || mkdir -p "$dir_snapshot" + + # Hacer snapshot + if [ $i -eq 0 ]; then + btrfs subvolume snapshot / "$dir_snapshot/$name_snapshot" + else + btrfs subvolume snapshot /srv "$dir_snapshot/$name_snapshot" + fi + # Desmontar rutas + + readarray -t snapshots < <(ls -1 "$dir_snapshot" | sort) + + total=${#snapshots[@]} + + if (( total <= 3 )); then + echo "Hay $total snapshots, no se elimina nada" + continue + fi + + # cuantos borrar + borrar=$(( total - 3 )) + + for (( contador=0; contador [!DANGER] +> No se recomienda usar en sistemas RAID 5/6, existen mejores alternativas. Esta opción aún se encuentra en fase experimental. + +El presente repositorio es un ejemplo de uso para generar snapshot automáticas cada 24 h mediante bash script y un demonio de SystemD. Es altamente recomendado usar esta técnica para servidores y/u HomeLabs para hacer roll-back en un punto cero. + +> [!NOTE] +> En este ejemplo se mantienen solo 3 snaphots y se usa para dos unidades de almacenamiento. Se puede modificar el número de unidades y snapshot que se puede usar. + +1. Script + > [!TIP] Description=Script de Snapshots BTRFS Diarios + After=network.target + + [Service] + Type=oneshot + ExecStart=/usr/bin/bash /home/sysadmin/init.sh + User=root + Group=root + + [Install] + WantedBy=multi-user.target + > NO es necesario alojarlo en la misma ruta que este ejemplo, si deseas cambiar, tambien debes cambiar la ruta de 'btrfs-backup.service'. + 1. Copiar script + ``` + sudo mkdir -p /root/btrfs + sudo touch /root/btrfs/init.sh + sudo nano /root/btrfs/init.sh + ``` + > [!NOTE] + > Se puede usar cualquier metodo para copiar el script. Se recomienda que el script este en un directorio donde un usuario de bajo no privilegiado pueda acceder. Ya que se puede modificar el script y generar un medio de ataque. + ``` + #!/bin/bash + + set -e + + # Declarar varables + dir_disk=("/dev/nvme0n1p3" "/dev/bcache0") + + dir_temp=("/tmp/root" "/tmp/srv") + + dir_create="/tmp/{root,srv}" + + name_snapshot=$(date +"%m%d%Y") + + + + # Crear ruta + mkdir -p "$dir_create" + + #Montar rutas + + for i in "${!dir_disk[@]}"; do + disk_origen="${dir_disk[$i]}" + dir_backup="${dir_temp[$i]}" + dir_snapshot="$dir_backup/snapshot/daily" + #Montar rutas + mount "$disk_origen" "$dir_backup" + + # Verificar que existan rutas, caso contrario crearlas + + [ -d "$dir_snapshot" ] || mkdir -p "$dir_snapshot" + + # Hacer snapshot + if [ $i -eq 0 ]; then + btrfs subvolume snapshot / "$dir_snapshot/$name_snapshot" + else + btrfs subvolume snapshot /srv "$dir_snapshot/$name_snapshot" + fi + # Desmontar rutas + + readarray -t snapshots < <(ls -1 "$dir_snapshot" | sort) + + total=${#snapshots[@]} + + if (( total <= 3 )); then + echo "Hay $total snapshots, no se elimina nada" + continue + fi + + # cuantos borrar + borrar=$(( total - 3 )) + + for (( contador=0; contador