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
  mkdir $TARGETDIR
fi
 
# Perform operations if TargetDir exists
if cd $TARGETDIR; then
 
 #-----------------------------
  # Remove old backup file
  #-----------------------------
  #rm -rf $TARGETDIR/*
 
  #-----------------------------
  ## Backup
  #-----------------------------
  # Backup mail spool
  rm -f $TARGETDIR/mail-backup.tar.gz
  tar -czvf $TARGETDIR/mail-backup.tar.gz /var/spool/mail
 
  # Backup user directories
  rm -f $TARGETDIR/homes-backup.tar.gz
  tar -czvf $TARGETDIR/homes-backup.tar.gz /home
 
  # Backup root user directories
  rm -f $TARGETDIR/root-backup.tar.gz
  tar -czvf $TARGETDIR/root-backup.tar.gz /root
 
  # Backup system settings, ie. /etc directory
  rm -f $TARGETDIR/etc-backup.tar.gz
  tar -czvf $TARGETDIR/etc-dir.tar.gz /etc
 
  # Backup MYSQL databases
  rm -f $TARGETDIR/mysql-data-backup.tar.gz
  tar -czvf $TARGETDIR/mysql-data-backup.tar.gz /var/lib/mysql
  # Hot backup with mysqldump command: 
  # mysqldump -u<username> -p<password> <database name> > <outputfile.sql>
  # Restore with:
  # mysql -u<username> -p<password> <database name> < <inputfile.sql>
  mysqldump -uroot -pMyPass website_db > website_db.sql
  tar -czf $TARGETDIR/website_db.tar.gz /tmp/website_db.sql
  rm /tmp/website_db.sql
 
  # Backup SAMBA config files (when using tdbsam)
  rm -f $TARGETDIR/samba-data-backup.tar.gz
  tar -czvf $TARGETDIR/samba-data-backup.tar.gz /var/lib/samba
 
  # Backup website
  rm -f $TARGETDIR/websitebackup.tar.gz
  #tar -czvf $TARGETDIR/websitebackup.tar.gz /var/www
  tar -czvf $TARGETDIR/websitebackup.tar.gz /var/www --exclude "/var/www/downloads" --exclude "/var/www/es/downloads"
  rm -f $TARGETDIR/downloads-backup.tar.gz
  tar -czvf $TARGETDIR/downloads-backup.tar.gz /var/www/downloads
  # Backup database as SQL
 
  # Backup projects
  rm -f $TARGETDIR/projrepos.tar.gz
  tar -czvf $TARGETDIR/projrepos.tar.gz /data/repos
 
  #-----------------------------
  # 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

Including Subversion repositories:

#!/bin/bash
 
#-----------------------------
# Define Repository Directory
#-----------------------------
REPOSDIR="/data/repos/myproj"
TARGETDIR="/data/backup/$HOSTNAME"
 
 
#---------------------------------------------------------
# Remove old Subversion dump file
#---------------------------------------------------------
rm -f $TARGETDIR/repo-myproj.svn_dump
 
#---------------------------------------------------------
# Step 1: Backup your old Repository
#---------------------------------------------------------
#
# The first thing you need when moving from one server to another
# is a dump of your subversion repository. Hopefully you are already
# creating dump's with a backup script, but if not here's how you can
# create a subversion dump file:
#
# svnadmin dump $REPOSDIR > $TARGETDIR/repo-myproj.svn_dump
 
rm -f $TARGETDIR/repo-clarujust.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-clarujust.svn_dump
 
rm -f $TARGETDIR/repo-ezarkd7.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-ezarkd7.svn_dump
 
rm -f $TARGETDIR/repo-ezfit4.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-ezfit4.svn_dump
 
rm -f $TARGETDIR/repo-ezfit4.localization.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-ezfit4.localization.svn_dump
 
rm -f $TARGETDIR/repo-ezfit5.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-ezfit5.svn_dump
 
rm -f $TARGETDIR/repo-ezfit-autorun.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-ezfit-autorun.svn_dump
 
rm -f $TARGETDIR/repo-ezhit.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-ezhit.svn_dump
 
rm -f $TARGETDIR/repo-eznzd7.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-eznzd7.svn_dump
 
rm -f $TARGETDIR/repo-gennum.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-gennum.svn_dump
 
rm -f $TARGETDIR/repo-installation.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-installation.svn_dump
 
rm -f $TARGETDIR/repo-mycomponents.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-mycomponents.svn_dump
 
rm -f $TARGETDIR/repo-nal.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-nal.svn_dump
 
rm -f $TARGETDIR/repo-prninvoice.svn_dump
svnadmin dump $REPOSDIR > $TARGETDIR/repo-prninvoice.svn_dump
 
rm -f $TARGETDIR/repo-projskel.tar.gz
tar czf $TARGETDIR/repo-projskel.tar.gz $REPOSDIR/projskel
 
rm -f $TARGETDIR/repo-conf.tar.gz
tar czf $TARGETDIR/repo-conf.tar.gz $REPOSDIR/conf
 
# The dump file contains all the revisions you have ever made
# to your svn repository, so it will probably be quite large
# (it even includes files you may have deleted in a previous revision).
#
#---------------------------------------------------------
# Step 2: Create the new Repository
#---------------------------------------------------------
#
# Now, simply transfer the dump file on to your new subversion server,
# and create an empty repository:
#
# svnadmin create /path/to/repository
# svnadmin create $REPOSDIR
#
#---------------------------------------------------------
# Step 3: Import your old repository into the new one
#---------------------------------------------------------
#
# Next import your dump file:
#
# svnadmin load /path/to/repository < repo_name.svn_dump
# svnadmin load $REPOSDIR < $TARGETDIR/repo-myproj.svn_dump
 
## send reminder to sysadmin to pull the tarball from the server.
#mail ttran -s "Get the backup tarball off the server"