Home » How to Create a Compressed Automated Backup Script in Linux with Tar and Cron
Systems & Servers

How to Create a Compressed Automated Backup Script in Linux with Tar and Cron

✨ Quick Answer

The most serious mistake in managing local or home servers is not having automated and intact backups against hardware failures or file corruption. Creating a scheduled task that packages your essential configuration files will guarantee peace of mind.

Quick Diagnostics

Cause
Cronjob failing due to relative paths or environment variables
Solution
Specify absolute paths in script (/bin/tar, /usr/bin/crontab)
Cause
Insufficient write permissions in destination directory
Solution
Ensure write permissions with chmod 755 or run cron as root

The most serious mistake in managing local or home servers is not having automated and intact backups against hardware failures or file corruption. Creating a scheduled task that packages your essential configuration files will guarantee peace of mind.

Step-by-Step Solution

  1. 1

    Step 1: Escribir el script de empaquetado en Bash

    Create a script in your user directory that automates the data compression by adding the current date to the name of the resulting file:

    BASH
    # Crear el script de respaldo
    nano ~/auto_backup.sh
    

    Paste the following code into the editor:

    BASH
    #!/bin/bash
    # Definir variables de rutas absolutas
    BACKUP_DIR="/var/backups/my_app"
    SOURCE_DIR="/var/www/html/my_app_data"
    FECHA=$(date +%Y-%m-%d_%H%M%S)
    FILE_NAME="backup_$FECHA.tar.gz"
    
    # Crear directorio de respaldo si no existe
    mkdir -p "$BACKUP_DIR"
    
    # Comprimir la carpeta de origen usando exclusión de temporales si es necesario
    /usr/bin/tar -czf "$BACKUP_DIR/$FILE_NAME" "$SOURCE_DIR"
    
    # Opcional: Eliminar archivos de respaldo más antiguos de 30 días para evitar saturar el disco
    /usr/bin/find "$BACKUP_DIR" -name "backup_*.tar.gz" -mtime +30 -delete
    
  2. 2

    Step 2: Otorgar permisos de ejecución al script

    Ensure that the system can invoke the script you just scheduled:

    BASH
    chmod +x ~/auto_backup.sh
    
  3. 3

    Step 3: Programar la ejecución en el programador de tareas Cron

    Open your user's task table:

    BASH
    crontab -e
    

    Add the following rule to run the automatic security packaging daily (for example, at 02:00 AM):

    PLAINTEXT
    0 2 * * * /usr/bin/bash /home/usuario/auto_backup.sh >> /home/usuario/auto_backup.log 2>&1
    

Prevention Advice

Recommended security practices:

  • Do not store your compressed backup files exclusively within the same physical drive of the production server. If the hard drive suffers a general mechanical collapse, you will lose both production data and backups. Always configure a secondary task in your script that replicates the packaged file to cloud storage, a local secondary server, or an external NAS using secure transmission utilities such as rsync or rclone.
Author • Web Designer & App Creator

Rodolfo Castro

Web designer, app developer, and founder of SoporteCero. Specializing in UI/UX architecture, digital products, and modern web environments. Every tutorial and guide on SoporteCero is thoroughly tested and verified in our technical lab to ensure reliable, up-to-date solutions.