Ir al contenido

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...

0views

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.

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).
  • 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)

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.

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):

  1. We will enter as root permanently:

    Ventana de terminal
    sudo -i
  2. We will generate the key as root:(If one already existed, we can overwrite it or simply press Enter).

    ssh-keygen -t ed25519
  3. 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
  4. 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'"
  5. Exiting the root environment:

    exit

On the Source Server (172.20.0.136):

Ventana de terminal
sudo mkdir -p /datos
sudo mkdir -p /backup

**On the Remote Server (172.20.0.123):**We will connect via SSH and execute:

Ventana de terminal
sudo mkdir -p /backup/full
sudo mkdir -p /backup/diff
sudo chown -R user.portelcore:user.portelcore /backup

PART 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.

  1. We will package:

    Ventana de terminal
    sudo tar -cvpzf /backup/full-$(date +%F).tar.gz /datos
  2. We will encrypt:

    Ventana de terminal
    sudo gpg --symmetric --cipher-algo AES256 /backup/full-$(date +%F).tar.gz
  3. SHA256 Hash:

    Ventana de terminal
    sudo sha256sum /backup/full-$(date +%F).tar.gz.gpg | sudo tee /backup/full-$(date +%F).tar.gz.gpg.sha256
  4. 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)»
Ventana de terminal
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"
Ventana de terminal
sudo mkdir -p /etc/backup_credentials
echo "123" | sudo tee /etc/backup_credentials/gpg.key
sudo chmod 400 /etc/backup_credentials/gpg.key

The script must be located in /usr/local/bin/auto_backup_diferencial.sh to be globally accessible.

Ventana de terminal
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.sh
Ventana de terminal
sudo crontab -e
# Añadir al final:
# 0 3 * * * /usr/local/bin/auto_backup_diferencial.sh
  1. We will download: sudo rsync -av user.portelcore@172.20.0.123:/backup/full/archivo .
  2. We will verify: sha256sum -c archivo.sha256
  3. We will decrypt: sudo gpg --decrypt archivo.gpg > full.tar.gz

To confirm that the automation is correct, we will run the script manually with sudo.

Test command:

Ventana de terminal
sudo /usr/local/bin/auto_backup_diferencial.sh

Expected result (Success):

sending incremental file list
sent 78 bytes received 12 bytes 180,00 bytes/sec
total size is 11 speedup is 0,12

If the command finishes without asking for SSH or GPG passwords, the validation is successful.

The script generates a detailed log in /var/log/backup_diferencial.log. It is vital to verify that rotation processes are also running correctly:

Ventana de terminal
sudo tail -n 20 /var/log/backup_diferencial.log
  • Separate Partitions: We will use an independent disk for /backup.
  • SSH Security: We will change PermitRootLogin to no in /etc/ssh/sshd_config.
  • Drills: We will conduct periodic restoration tests.
#!/bin/bash
ORIGEN="/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 1
fi
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 1
fi
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"
fi
else
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"
fi
fi
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"