How to Backup Crontab Entries for Complete Server Migration (With Practical Code Examples)


2 views

When setting up comprehensive server backups, most admins remember web files and databases but often overlook crontab entries. These scheduled tasks are stored differently than regular files, requiring special handling.

Contrary to temporary file appearances, crontabs are stored in:

/var/spool/cron/crontabs/ (for user-specific crontabs)
/etc/crontab (system-wide crontab)
/etc/cron.d/ (additional system crontabs)

Method 1: Direct Export

# Backup all user crontabs
for user in $(cut -f1 -d: /etc/passwd); do 
    crontab -u $user -l > /backups/crontabs/$user.cron
done

# Backup system crontabs
cp /etc/crontab /backups/crontabs/system.cron
cp -r /etc/cron.d /backups/crontabs/

Method 2: Package Management (Debian/Ubuntu)

# List installed cron packages
dpkg -l | grep cron > /backups/crontabs/installed_packages.txt

# Backup configuration
apt-get install debconf-utils
debconf-get-selections | grep cron > /backups/crontabs/debconf_settings.txt

Here's how to modify your existing tar command:

#!/bin/bash
# Backup crontabs first
mkdir -p /tmp/backup/crontabs
crontab -l > /tmp/backup/crontabs/root.cron
cp -r /etc/cron* /tmp/backup/

# Then proceed with your existing backup
tar -czf /backups/server_backup_$(date +%Y%m%d).tar.gz \
    /var/www/html \
    /backups/mysql_dumps \
    /tmp/backup/crontabs

To test your backup:

# For user crontabs
crontab /backups/crontabs/username.cron

# For system crontabs
cp /backups/crontabs/system.cron /etc/crontab
cp -r /backups/crontabs/cron.d/* /etc/cron.d/

For better tracking:

#!/bin/bash
CRON_REPO="/backups/cron_git"

[ -d "$CRON_REPO" ] || git init "$CRON_REPO"

# Commit new changes
crontab -l > "$CRON_REPO/root_$(date +%s).cron"
cd "$CRON_REPO" && git add . && git commit -m "Crontab update $(date)"

When you edit crontab entries using crontab -e, the system temporarily stores your changes in /tmp before applying them to the actual crontab database. This explains why you can't find the files immediately after saving - they're not meant to persist in /tmp.

Instead of hunting for temporary files, use these commands to properly export crontab entries:

# Backup all user crontabs
for user in $(cut -f1 -d: /etc/passwd); do 
  crontab -u $user -l > /backups/crontab-$user.bak 2>/dev/null
done

# Backup system crontab files
cp /etc/crontab /backups/system-crontab.bak
cp -r /etc/cron.* /backups/

Here's a complete bash script that integrates with your existing backup system:

#!/bin/bash
BACKUP_DIR="/path/to/your/backup/folder"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

# Create backup directory
mkdir -p "$BACKUP_DIR/cron_backup_$TIMESTAMP"

# Backup individual user crontabs
echo "Backing up user crontabs..."
for user in $(cut -f1 -d: /etc/passwd); do
  crontab -u $user -l > "$BACKUP_DIR/cron_backup_$TIMESTAMP/$user.cron" 2>/dev/null
done

# Backup system cron files
echo "Backing up system cron files..."
cp /etc/crontab "$BACKUP_DIR/cron_backup_$TIMESTAMP/system_crontab"
cp -r /etc/cron.* "$BACKUP_DIR/cron_backup_$TIMESTAMP/"

# Add to your existing tar.gz
tar -czf "$BACKUP_DIR/full_backup_$TIMESTAMP.tar.gz" -C "$BACKUP_DIR" "cron_backup_$TIMESTAMP" your_other_files

# Cleanup
rm -rf "$BACKUP_DIR/cron_backup_$TIMESTAMP"

To restore from backup:

# For individual users
crontab -u username /path/to/backup/username.cron

# For system crontab
sudo cp /path/to/backup/system_crontab /etc/crontab
sudo cp -r /path/to/backup/cron.* /etc/
  • Always test your backups by restoring to a test environment
  • Include permissions in your backup if you're using /etc/cron.allow or /etc/cron.deny
  • Consider adding verification steps to your backup script to confirm crontab entries were successfully saved