This is an old revision of the document!


Linux Maintenance Scripts
Mail Spool Maintenance

Remove mail files of certain size (mailboxes that have spam and are not checked by users)

#!/bin/bash
 
x=''
 
for x in $( ls /var/spool/mail ); do
  if [ $( stat -c %s /var/spool/mail/$x ) -gt 10000000 ]; then
    echo $x >> names.log
    # rm $x
  fi
done
Commonly Used ''sed'' Commands

Deleting Tabs with ''sed''

To delete the rest of a line after the tab in command du (disk used), try one of the following commands:

Delete characters from the begining of line until the tab:

$ du | sed "s/^.*\t//"

Delete tabe from the end of the line:

$ du | sed "s/\t.*$//"
Notation
^ for beginning.
$ for end.
. for one character.
* for 0 or more repetitions.
\t for tab.
Backup Critical Data and Configuration

Create a daily cron task to backup some important files to a directory (for example: /data/backup). This script should be created as /etc/cron.daily/backup.sh:

#!/bin/bash
## script: /etc/cron.daily/backup.sh
##
 
#-----------------------------
# Define Target Directory
#-----------------------------
TARGETDIR="/data/backup/$HOSTNAME"
 
# Try to create TargetDir if it does not exist
if cd $TARGETDIR; then
  cd /
else
  mkdir $TARGETDIR
fi
 
# Perform operations if TargetDir exists
if cd $TARGETDIR; then
 
  #-----------------------------
  # Remove old backup file
  #-----------------------------
  rm -rf $TARGETDIR/*
 
  #-----------------------------
  ## Backup 
  #-----------------------------
  # Backup mail spool
  tar -czvf $TARGETDIR/mailbackup.tar.gz /var/spool/mail
 
  # Backup user directories
  tar -czvf $TARGETDIR/homesbackup.tar.gz /home
 
  # Backup system settings, ie. /etc directory
  tar -czvf $TARGETDIR/etc-dir.tar.gz /etc
 
  # Backup MYSQL databases
  tar -czvf $TARGETDIR/mysql-data-backup.tar.gz /var/lib/mysql
 
  # Backup website
  tar -czvf $TARGETDIR/websitebackup.tar.gz /var/www
 
  #-----------------------------
  # Send Reminder
  #-----------------------------
  ## send reminder to sysadmin to pull the tarball from the server.
  mail root@localhost -s "Get the backup tarball off the server"
 
else
 
  ## send message to sysadmin.
  mail root@localhost -s "Backup failed. %TARGETDIR does not exist"  
 
fi