Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:linux_maintenance_scripts [2010/07/14 09:52]
smayr
systems:linux_maintenance_scripts [2018/05/01 15:07] (current)
smayr [RSync]
Line 1: Line 1:
 == Linux Maintenance Scripts == == Linux Maintenance Scripts ==
  
 +== Services ==
 +==== BIND/DNS ====
 +''bind-restart.sh'':
 +<code bash>
 +#!/bin/bash
 +## script: ./bind-restart.sh
 +
 +#/etc/init.d/bind stop
 +#/etc/init.d/bind start
 +/etc/init.d/bind9 restart
 +</code>
 +
 +==== DHCP ====
 +''dhcpd-restart.sh'':
 +<code bash>
 +#!/bin/bash
 +## script: ./dhcpd-restart.sh
 +
 +#service isc-dhcp-server stop
 +#service isc-dhcp-server start
 +service isc-dhcp-server restart
 +
 +# Check status
 +ps aux | grep dhcpd
 +</code>
 +
 +==== Windows Server Mapping ====
 +''map-win-backup-server.sh'':
 +<code bash>
 +#!/bin/bash
 +## script: ./map-win-backup-server.sh
 +## Mount and map Windows backup server (for backup purposes)
 +mount -t cifs -o username=backuponly,password=backup //backupserver2/LinuxBackup /data/win-fileserver/
 +</code>
 +
 +==== RSync ====
 +''rsync-server.sh'':
 +<code bash>
 +#!/bin/bash
 +
 +#rsync --verbose  --progress --stats --compress --rsh=/usr/bin/ssh \
 +#      --recursive --times --perms --links --delete \
 +#      --exclude "*bak" --exclude "*~" \
 +#      192.168.0.31:webfiles /var/www/mirror
 +
 +# Website
 +rsync --archive --verbose --progress --stats --rsh=/usr/bin/ssh --recursive --times --perms 
 +  --links --delete --exclude=stats 192.168.0.31::webfiles /data/mirror/swdev/www
 +
 +# Databases
 +rsync --archive --verbose --progress --stats --rsh=/usr/bin/ssh --recursive --times --perms 
 +  --links --delete 192.168.0.31::databases /data/mirror/swdev/databases
 +
 +# Root user home
 +rsync --archive --verbose --progress --stats --rsh=/usr/bin/ssh --recursive --times --perms 
 +  --links --delete 192.168.0.31::root /data/mirror/swdev/root
 +
 +# Subserver Repositories
 +rsync --archive --verbose --progress --stats --rsh=/usr/bin/ssh --recursive --times --perms 
 +  --links --delete 192.168.0.31::repos /data/mirror/swdev/repos
 +</code>
 +
 +== Anti-spam / Anti-crack ==
 +Run this script to scan the website and see signs of spamming or cracking: ''scan-website.sh'':
 +<code bash>
 +#!/bin/bash
 +## script: scan-website.sh
 +##
 +
 +echo "Scan Website for vulnerabilities" > /tmp/scan.txt
 +
 +cd /var/www
 +find . -name "*.php.suspected" >> /tmp/scan.txt
 +find . -name "*.php" -exec grep -H "eval(" "{}" \; >> /tmp/scan.txt
 +find . -name "*.php" -exec grep -H "\$GLOBALS.*\\x" "{}" \; >> /tmp/scan.txt
 +find . -name "*.php" -exec grep -H "function.*for.*strlen.*isset" "{}" \; >> /tmp/scan.txt
 +find . -name "*.php" -exec grep -H '<iframe' "{}" \; >> /tmp/scan.txt
 +find . -name "*.php" -exec grep -H 'header(*Location:' "{}" \; >> /tmp/scan.txt
 +less /tmp/scan.txt
 +</code>
 == Mail Spool Maintenance == == Mail Spool Maintenance ==
  
Line 36: Line 116:
 |  ''\t'' | for tab. | |  ''\t'' | for tab. |
 == Backup Critical Data and Configuration == == Backup Critical Data and Configuration ==
 +
 +==== Backup Web Systems ====
 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'': 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'':
 <code bash> <code bash>
Line 48: Line 130:
  
 # Try to create TargetDir if it does not exist # Try to create TargetDir if it does not exist
-if cd $TARGETDIR; then +if cd $TARGETDIR; then
-  cd / +
-else+
   mkdir $TARGETDIR   mkdir $TARGETDIR
 fi fi
Line 57: Line 137:
 if cd $TARGETDIR; then if cd $TARGETDIR; then
  
-  #-----------------------------+ #-----------------------------
   # Remove old backup file   # Remove old backup file
   #-----------------------------   #-----------------------------
-  rm -rf $TARGETDIR/*+  #rm -rf $TARGETDIR/*
  
   #-----------------------------   #-----------------------------
-  ## Backup +  ## Backup
   #-----------------------------   #-----------------------------
   # Backup mail spool   # Backup mail spool
-  tar -czvf $TARGETDIR/mailbackup.tar.gz /var/spool/mail+  rm -f $TARGETDIR/mail-backup.tar.gz 
 +  tar -czvf $TARGETDIR/mail-backup.tar.gz /var/spool/mail
  
   # Backup user directories   # Backup user directories
-  tar -czvf $TARGETDIR/homesbackup.tar.gz /home+  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   # Backup system settings, ie. /etc directory
 +  rm -f $TARGETDIR/etc-backup.tar.gz
   tar -czvf $TARGETDIR/etc-dir.tar.gz /etc   tar -czvf $TARGETDIR/etc-dir.tar.gz /etc
  
   # Backup MYSQL databases   # Backup MYSQL databases
 +  rm -f $TARGETDIR/mysql-data-backup.tar.gz
   tar -czvf $TARGETDIR/mysql-data-backup.tar.gz /var/lib/mysql   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 > $TARGETDIR/website_db.sql
 +  tar -czf $TARGETDIR/website_db.tar.gz $TARGETDIR/website_db.sql
  
- # Backup SAMBA config files (when using tdbsam)+  # 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   tar -czvf $TARGETDIR/samba-data-backup.tar.gz /var/lib/samba
  
   # Backup website   # Backup website
-  tar -czvf $TARGETDIR/websitebackup.tar.gz /var/www+  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
  
   #-----------------------------   #-----------------------------
Line 97: Line 201:
 </code> </code>
  
 +==== Backup Subversion Repositories ====
 +Including Subversion repositories:
 +<code bash>
 +#!/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"
 +
 +</code>
 +
 +==== Backup Databases ====
 +Create a scrip ''backup-db.sh'' to backup databases:
 +<code bash>
 +#!/bin/bash
 +## script: backup-db.sh
 +##
 +
 +# backup last 7 days
 +#export d=`date +%u`
 +# backup using date
 +export d=$(date +'%Y-%m-%d')
 +
 +mkdir -p backup/$d
 +#for i in `echo "show tables" | mysql -u username -ppassword database|grep -v Tables_in_`;
 +for i in `echo "show tables" | mysql -u root -p database|grep -v Tables_in_`;
 +do
 +  echo $i; mysqldump --add-drop-table --allow-keywords -q -a -c -u username -ppassword database $i > backup/$d/$i.sql
 +  rm -f backup/$d/$i.sql.gz
 +  gzip backup/$d/$i.sql
 +done
 +</code>
 +
 +==== Backup to Window Server ====
 +If you need to backup data to a remote Windows server (running Samba), use this script '''backup-to-winserver.sh'':
 +<code bash>
 +#!/bin/bash
 +## script: /etc/cron.daily/backuptowinsvr
 +##
 +
 +###!!!! THIS SCRIPT IS DISABLED
 +###!!!! COMMENT OUT THIS NEXT LINE TO ENABLE
 +#exit 99
 +
 +#-----------------------------
 +# Define Target Directory
 +#-----------------------------
 +SOURCEDIR="/data/backup/$HOSTNAME"
 +TARGETDIR="/data/win-fileserver"
 +
 +# Verify if source is available, otherwise we bail out
 +if [ ! -d "$SOURCEDIR" ]; then
 +  echo " Nothing to backup on $(date). Source directory $SOURCEDIR does not exist." >> /var/log/backup.log
 +  echo " Nothing to backup on $(date)."
 +  echo " Source directory $SOURCEDIR does not exist."
 +  echo " Exiting."
 +  exit;
 +fi
 +
 +# Try to create and mount TargetDir if it does not exist
 +if [ ! -d "$TARGETDIR" ]; then
 +  mkdir $TARGETDIR
 +fi
 +
 +# Perform operations if TargetDir exists
 +if [ -d "$TARGETDIR" ]; then
 +  #------------------------------------------
 +  # Mount Windows server samba drive (CIFS)
 +  #------------------------------------------
 +  # Requires cifs-utils to be installed for mount to work.
 +  mount -t cifs -o username=backuponly,password=backup //backupserver2/LinuxBackup $TARGETDIR
 +
 +  #-----------------------------
 +  # Remove old backup files
 +  #-----------------------------
 +  rm -rf $TARGETDIR/$HOSTNAME/*
 +
 +  #-----------------------------
 +  # Backup
 +  #-----------------------------
 +  # Backup ALL files
 +  #cp -R $SOURCEDIR $TARGETDIR
 +
 +  # Backup all files EXCEPT 'downloads*' files
 +  # Method 1: cp *?[!excludepattern] $SOURCEDIR $TARGETDIR
 +  # Method 2: cp `ls $SOURCEDIR |grep -v <yourexcludepattern>` $TARGETDIR
 +  cd $SOURCEDIR
 +  cp `ls $SOURCEDIR | grep -v downloads` $TARGETDIR/$HOSTNAME
 +
 +  #-----------------------------
 +  # Send Reminder
 +  #-----------------------------
 +  ## send reminder to sysadmin to pull the tarball from the server.
 +  echo "Backup to Windows Backup Server Complete"
 +  echo "Nightly Backup to Windows Backup Server Completed: $(date)" >> /var/log/backup.log
 +  echo "Source: $SOURCEDIR" >> /var/log/backup.log
 +  echo "Target: $TARGETDIR" >> /var/log/backup.log
 +  #mail root@localhost -s "Get the backup tarball off the server"
 +
 +else
 +
 +  ## send message to sysadmin.
 +  echo "Backup to Windows Backup Server failed on $(date). %TARGETDIR does not exist" >> /var/log/backup.log
 +  echo "Backup to Windows Backup Server failed on $(date). %TARGETDIR does not exist"
 +  #mail root@localhost -s "Backup failed. %TARGETDIR does not exist"
 +
 +fi
 +
 +</code>