How to Restore Deleted/Lost Config Files in Linux Without Reinstalling Packages (MySQL my.cnf Example)


3 views

html

When working with package-managed configuration files in Linux (like /etc/mysql/my.cnf from mysql-common), you often can't simply purge and reinstall the package due to dependency chains. Here's the proper way to restore individual config files without disrupting your system.

# List all files in the package
dpkg -L mysql-common | grep my.cnf

# Extract the original file directly
sudo dpkg -i --force-confmiss /var/cache/apt/archives/mysql-common_*.deb

When the package is already installed but configs are missing:

# First ensure the package is downloaded
sudo apt-get download mysql-common

# Then force reinstall with config restoration
sudo dpkg -i --force-confmiss mysql-common_*.deb

When you're not sure which package owns the file:

# Install apt-file if needed
sudo apt install apt-file
sudo apt-file update

# Find the package
apt-file search /etc/mysql/my.cnf

# Then use the extraction method above

dpkg-reconfigure often doesn't restore deleted files - it only regenerates files that exist with modified content. The --force-confmiss flag specifically handles missing configuration files.

Important distinction for different scenarios:

# For modified files you want to reset:
sudo dpkg-reconfigure mysql-common

# For actually missing files:
sudo dpkg -i --force-confmiss package.deb

For frequent recovery needs, create a helper script:

#!/bin/bash
# restore_config.sh
pkg=$(dpkg -S "$1" | cut -d: -f1)
sudo apt-get download "$pkg"
sudo dpkg -i --force-confmiss "${pkg}_"*.deb
rm "${pkg}_"*.deb

Usage: sudo ./restore_config.sh /etc/mysql/my.cnf


When managing MySQL servers, it's common to modify configuration files like /etc/mysql/my.cnf. But what happens when you accidentally delete or corrupt this critical file? The naive approach of purging and reinstalling the mysql-common package would cause dependency issues:

# DON'T do this - breaks dependencies
sudo apt-get purge mysql-common
sudo apt-get install mysql-common

The cleanest method is to extract just the config file from the package without touching installed files:

# First locate the package file
sudo apt-get download mysql-common
PACKAGE=$(ls mysql-common*.deb)

# Extract just my.cnf
dpkg -x $PACKAGE /tmp/mysql-extract
sudo cp /tmp/mysql-extract/etc/mysql/my.cnf /etc/mysql/
sudo rm -rf /tmp/mysql-extract

The dpkg-reconfigure command didn't work in your case because it doesn't restore deleted files. However, you can force this behavior:

# Mark package as needing reinstallation
sudo apt-get --reinstall install -d mysql-common
sudo dpkg -i --force-confmiss /var/cache/apt/archives/mysql-common*.deb

For systems using newer apt versions (1.1+), there's a cleaner way:

sudo apt-get install --reinstall -o Dpkg::Options::="--force-confmiss" mysql-common

This will restore all missing configuration files from the package while preserving existing configurations.

After restoration, verify the file:

ls -l /etc/mysql/my.cnf
md5sum /etc/mysql/my.cnf

Compare the checksum with the package version to ensure integrity. If you need to customize the restored file, always make a backup first:

sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.bak