Technical Manual: Implementing Differential Backups with Rsync and GPG
Author: Full-Stack Innovator Pro (Based on Ubunturista's guide) Objective: We will set up a complete backup system using tools...
Author: Full-Stack Innovator Pro (Based on Ubunturista's guide) Objective: We will set up a complete backup system using standard tools (tar, gpg, rsync), applying encryption, rotation, integrity verification, and automation via Hard Links.
📋 Architecture and Scenario
Sección titulada «📋 Arquitectura y Escenario»We will work with two different servers to comply with security regulations (physical separation of backups):
- Source Server (Where the data is):
- IP:
172.20.0.136 - Folders:
/datos(Actual data to protect) and/backup(Local temporary folder).
- IP:
- Destination Server (Where copies are stored - Remote):
- IP:
172.20.0.123 - User:
user.portelcore - Folder structure:
/backup/full/(For encrypted full backups)/backup/diff/(For rsync differential backups)/backup/last(Symbolic link that will always point to the last backup)
- IP:
PART 1: Prerequisites and Environment Preparation
Sección titulada «PART 1: Prerequisitos y Preparación del Entorno»Before running commands, we need to prepare the communications and the folders.
1.1 Configure passwordless SSH (CRITICAL: DO THIS AS ROOT!)
Sección titulada «1.1 Configurar SSH sin contraseña (¡CRÍTICO: HACERLO COMO ROOT!)»The most common error is generating the key with our user. The backup script runs as root, so root must be the owner of the SSH connection.
On the Source Server (172.20.0.136):
-
We will enter as root permanently:
Ventana de terminal sudo -i -
We will generate the key as root:(If one already existed, we can overwrite it or simply press Enter).
ssh-keygen -t ed25519 -
We will copy the key to the destination (sending it to the remote user user.portelcore):
ssh-copy-id user.portelcore@172.20.0.123 -
Security verification: While we are still root, we will attempt to connect. It should not ask for a password.
Ventana de terminal ssh user.portelcore@172.20.0.123 "echo 'Conexión desde ROOT confirmada'" -
Exiting the root environment:
exit
1.2 Folder Structure Creation
Sección titulada «1.2 Creación de Estructura de Carpetas»On the Source Server (172.20.0.136):
sudo mkdir -p /datossudo mkdir -p /backup**On the Remote Server (172.20.0.123):**We will connect via SSH and execute:
sudo mkdir -p /backup/fullsudo mkdir -p /backup/diffsudo chown -R user.portelcore:user.portelcore /backupPART 2: Manual Execution of the Initial Full Backup (Mandatory)
Sección titulada «PART 2: Ejecución Manual del Full Backup Inicial (Obligatorio)»Since we currently do not have any copies, we must perform the first full backup entirely manually.
-
We will package:
Ventana de terminal sudo tar -cvpzf /backup/full-$(date +%F).tar.gz /datos -
We will encrypt:
Ventana de terminal sudo gpg --symmetric --cipher-algo AES256 /backup/full-$(date +%F).tar.gz -
SHA256 Hash:
Ventana de terminal sudo sha256sum /backup/full-$(date +%F).tar.gz.gpg | sudo tee /backup/full-$(date +%F).tar.gz.gpg.sha256 -
We will transfer:
Ventana de terminal sudo rsync -av /backup/full-$(date +%F).tar.gz.gpg* user.portelcore@172.20.0.123:/backup/full/
PART 3: Differential Backup (The Magic of Rsync)
Sección titulada «PART 3: Backup Diferencial (La Magia de Rsync)»sudo rsync -av --delete --link-dest=/backup/last /datos/ user.portelcore@172.20.0.123:/backup/diff/diff-$(date +%F)sudo ssh user.portelcore@172.20.0.123 "ln -sfn /backup/diff/diff-$(date +%F) /backup/last"PART 4: Automation with the Master Script
Sección titulada «PART 4: Automatización con el Script Maestro»4.1 GPG Password File
Sección titulada «4.1 Archivo de Contraseña GPG»sudo mkdir -p /etc/backup_credentialsecho "123" | sudo tee /etc/backup_credentials/gpg.keysudo chmod 400 /etc/backup_credentials/gpg.key4.2 Create Script
Sección titulada «4.2 Crear Script»The script must be located in /usr/local/bin/auto_backup_diferencial.sh to be globally accessible.
sudo nano /usr/local/bin/auto_backup_diferencial.sh# (Pegar código del script sin comentarios)sudo chmod +x /usr/local/bin/auto_backup_diferencial.sh4.3 Cron
Sección titulada «4.3 Cron»sudo crontab -e# Añadir al final:# 0 3 * * * /usr/local/bin/auto_backup_diferencial.shPART 5: Restoration and Verification
Sección titulada «PART 5: Restauración y Verificación»- We will download:
sudo rsync -av user.portelcore@172.20.0.123:/backup/full/archivo . - We will verify:
sha256sum -c archivo.sha256 - We will decrypt:
sudo gpg --decrypt archivo.gpg > full.tar.gz
PART 6: Validation and Success Results
Sección titulada «PART 6: Validación y Resultados de Éxito»To confirm that the automation is correct, we will run the script manually with sudo.
Test command:
sudo /usr/local/bin/auto_backup_diferencial.shExpected result (Success):
sending incremental file listsent 78 bytes received 12 bytes 180,00 bytes/sectotal size is 11 speedup is 0,12If the command finishes without asking for SSH or GPG passwords, the validation is successful.
PART 7: Log Verification
Sección titulada «PART 7: Verificación de Logs»The script generates a detailed log in /var/log/backup_diferencial.log. It is vital to verify that rotation processes are also running correctly:
sudo tail -n 20 /var/log/backup_diferencial.log📌 Final Best Practices
Sección titulada «📌 Buenas Prácticas Finales»- Separate Partitions: We will use an independent disk for
/backup. - SSH Security: We will change
PermitRootLogintonoin/etc/ssh/sshd_config. - Drills: We will conduct periodic restoration tests.
Full+Differential Backup Script Code
Sección titulada «Código Script del Backup Full+Diferencial»#!/bin/bashORIGEN="/datos"TEMP_DIR="/backup"LOG_FILE="/var/log/backup_diferencial.log"GPG_PASS_FILE="/etc/backup_credentials/gpg.key"REMOTE_USER="user.portelcore"REMOTE_HOST="172.20.0.123"REMOTE_DIR_FULL="/backup/full"REMOTE_DIR_DIFF="/backup/diff"REMOTE_LAST="/backup/last"DATA=$(date +%F)DIA_SEMANA=$(date +%u)
if [ "$(id -u)" -ne 0 ]; then echo "ERROR: Este script debe ejecutarse como root (sudo)" >&2 exit 1fi
if [ ! -f "$GPG_PASS_FILE" ]; then echo "[$DATA] ERROR CRÍTICO: No se encuentra el archivo de contraseña GPG en $GPG_PASS_FILE." >> "$LOG_FILE" exit 1fi
echo "========================================================================" >> "$LOG_FILE"echo "[$DATA] INICIANDO PROCESO DE COPIA DE SEGURIDAD" >> "$LOG_FILE"
if [ "$DIA_SEMANA" -eq 7 ]; then echo "[$DATA] Tipo de Copia: FULL BACKUP (Día $DIA_SEMANA)" >> "$LOG_FILE" LOCAL_TAR="$TEMP_DIR/full-$DATA.tar.gz" LOCAL_GPG="$LOCAL_TAR.gpg" tar -cvpzf "$LOCAL_TAR" "$ORIGEN" 2>> "$LOG_FILE" gpg --batch --yes --passphrase-file "$GPG_PASS_FILE" --symmetric --cipher-algo AES256 "$LOCAL_TAR" 2>> "$LOG_FILE" sha256sum "$LOCAL_GPG" > "$LOCAL_GPG.sha256" rsync -av "$LOCAL_GPG" "$LOCAL_GPG.sha256" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR_FULL/" 2>> "$LOG_FILE" if [ $? -eq 0 ]; then echo "[$DATA] ÉXITO: FULL Backup transferido." >> "$LOG_FILE" rm -f "$LOCAL_TAR" "$LOCAL_GPG" "$LOCAL_GPG.sha256" ssh "$REMOTE_USER@$REMOTE_HOST" "rm -f $REMOTE_LAST" else echo "[$DATA] ERROR al transferir el FULL Backup." >> "$LOG_FILE" fielse echo "[$DATA] Tipo de Copia: DIFERENCIAL (Día $DIA_SEMANA)" >> "$LOG_FILE" REMOTE_TARGET="$REMOTE_DIR_DIFF/diff-$DATA" rsync -av --delete --link-dest="$REMOTE_LAST" "$ORIGEN/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_TARGET" 2>> "$LOG_FILE" if [ $? -eq 0 ]; then echo "[$DATA] ÉXITO: Backup Diferencial completado." >> "$LOG_FILE" ssh "$REMOTE_USER@$REMOTE_HOST" "ln -sfn $REMOTE_TARGET $REMOTE_LAST" else echo "[$DATA] ERROR en la sincronización diferencial." >> "$LOG_FILE" fifi
echo "[$DATA] Ejecutando política de retención..." >> "$LOG_FILE"ssh "$REMOTE_USER@$REMOTE_HOST" "find $REMOTE_DIR_FULL -type f -mtime +30 -delete"ssh "$REMOTE_USER@$REMOTE_HOST" "find $REMOTE_DIR_DIFF -maxdepth 1 -mindepth 1 -type d -mtime +14 -exec rm -rf {} \;"echo "[$DATA] FIN DEL PROCESO." >> "$LOG_FILE"echo "========================================================================" >> "$LOG_FILE"
