How to Fix Tar Backup Errors in CentOS: Handling Sockets and SELinux Policy Issues


2 views

When attempting a full system backup in CentOS using tar, you'll encounter several expected warnings and one critical error:

tar: /selinux/policy: Cannot open: Invalid argument

The socket-related messages (e.g., tar: /dev/log: socket ignored) are normal warnings that can be safely ignored, but the SELinux policy error will cause your backup to fail.

Sockets cannot be meaningfully backed up through tar as they represent active communication endpoints. The SELinux policy error occurs because:

  • The file has special security attributes
  • It's being actively used by the system
  • Standard tar doesn't handle certain SELinux contexts well

Here's an improved version of your backup command that handles these cases:

tar --xattrs --selinux -cvpzf /TEMP_BACKUPS/backup.tgz \
    --exclude=/proc \
    --exclude=/lost+found \
    --exclude=/tmp \
    --exclude=/TEMP_BACKUPS \
    --exclude=/mnt \
    --exclude=/sys \
    --exclude=/selinux/policy \
    / 2>/TEMP_BACKUPS/tar_errors.log

For sockets: We redirect stderr to a log file to separate genuine errors from socket warnings

For SELinux: We add --xattrs and --selinux flags to properly handle extended attributes

For the policy file: We explicitly exclude the problematic SELinux policy file

For more reliable system backups, consider using rsync:

rsync -aAXv --delete \
    --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/TEMP_BACKUPS"} \
    / /TEMP_BACKUPS/centos_backup/

When restoring from backup, remember to:

  • Boot into rescue mode
  • Recreate excluded directories (/proc, /sys, etc.)
  • Restore SELinux contexts: restorecon -Rv /

When running your backup command:

tar cvpzf /TEMP_BACKUPS/backup.tgz --exclude=/proc --exclude=/lost+found \
--exclude=/tmp --exclude=/TEMP_BACKUPS --exclude=/mnt --exclude=/sys / \
> /TEMP_BACKUPS/mylog.txt

These "socket ignored" messages appear because tar cannot properly archive Unix domain sockets (special inter-process communication files). While they look like errors, they're actually warnings about expected behavior.

The critical error is:

tar: /selinux/policy: Cannot open: Invalid argument

This indicates SELinux policy files require special handling during backup operations.

Try this enhanced version that properly handles special files:

sudo tar --xattrs --acls -cvpzf /TEMP_BACKUPS/backup.tgz \
--exclude=/proc \
--exclude=/lost+found \
--exclude=/tmp \
--exclude=/TEMP_BACKUPS \
--exclude=/mnt \
--exclude=/sys \
--exclude=/dev \
--exclude=/selinux \
--exclude=/run \
/ 2>/TEMP_BACKUPS/tar_errors.log

For more reliable system backups, consider rsync:

sudo rsync -aAXv --delete \
--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/TEMP_BACKUPS/*"} \
/ /TEMP_BACKUPS/backup_folder/

To properly backup and restore SELinux contexts:

sudo tar --xattrs --selinux -cvpzf /TEMP_BACKUPS/backup.tgz /

Or restore with:

sudo tar --xattrs --selinux -xvpzf backup.tgz -C /

Always verify backups by listing contents:

tar -tzvf /TEMP_BACKUPS/backup.tgz | head -20

Or test extraction in a temporary directory:

mkdir /tmp/backup_test && cd /tmp/backup_test
sudo tar -xzvf /TEMP_BACKUPS/backup.tgz

Create a backup script (/usr/local/bin/full_backup.sh):

#!/bin/bash
BACKUP_DIR="/TEMP_BACKUPS"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
LOG_FILE="$BACKUP_DIR/backup_$DATE.log"

sudo tar --xattrs --acls --selinux -cvpzf "$BACKUP_DIR/backup_$DATE.tgz" \
--exclude={/proc,/lost+found,/tmp,/TEMP_BACKUPS,/mnt,/sys,/dev,/selinux,/run} \
/ 2>"$LOG_FILE"

# Verify backup was created
if [ -f "$BACKUP_DIR/backup_$DATE.tgz" ]; then
    echo "Backup completed successfully: $BACKUP_DIR/backup_$DATE.tgz"
else
    echo "Backup failed - check $LOG_FILE for errors"
fi

Make it executable:

sudo chmod +x /usr/local/bin/full_backup.sh