Thursday, December 15, 2011

Recover files deleted in linux

If any files accidentally deleted and needs to be recovered. First look in lost+found folder.
If not found follow the below steps.

Mount the filesystem in read only to avoid any rewrites on the disk block
# mount -o remount ro /dev/sdx /mntpoint.

If the partition is a root filesystem - Boot the server in single user mode
# init 1

To recover a text file you can use grep command to recover the contents. For this you need to know any uniq word from the deleted file - Preferably the start of the file.
grep -a -B2 -A1000 'uniq_word' /dev/sdx > /tmp/filerecovered.txt

It means 2lines before the uniq_word found and 1000lines after the uniq_word found from the filesystem and save the content into filerecovered file. (ensure the destination folder is read-write mode.
===================
Recover other types of files

Use package foremost There is an excellent article published in howtoforge portal.




Friday, December 09, 2011

check the disk space availablity

Script to check the disk space and mail if the disk space availablity is below the warning or critical thresholds

### Variable declaration ####
WARN=75
CRITICAL=77
ADMINI_LABS="useremailaccount"


#### Collecting Disk space information #####
df -h |grep -vE 'tmpfs|Filesystem|cdrom'|awk '{print $5 " " $6}' > /tmp/disksize

while read output; do
diskusage=$(echo $output|awk '{print $1}' |cut -d% -f1 )
filesystem=$(echo $output |awk '{print $2}' )
if [ $diskusage -ge $WARN ] && [ $diskusage -le $CRITICAL ]; then
mail -s"WARNING: $(hostname) : Running out of space $filesystem $diskusage" $ADMIN_ILABS
elif [ $diskusage -ge $CRITICAL ]; then
mail -s "CRITICAL: $(hostname) : Running out of space $filesystem $diskusage" $ADMIN_ILABS
fi
done < /tmp/disksize

## File Clean up ##
rm -f /tmp/disksize