Linux Shell Scripting

Rename All Files

Debian-user#./renamefiles mp3 haydeh mp3"
echo =====================start=====================
#!/bin/bash
#./renamefiles
if [ $# -ne 3 ]; then
echo 1>&2 Usage: [curent part of file name] [new file name] [extension] "Example: ./rename mp3 haydeh mp3"
exit 127
fi

oldfilename=*$1*;
newfilename=$2;
extension=$3;

echo "oldfilename="$oldfilename;<br />echo "newfilename="$newfilename;<br />echo "extension=" $extension;

files=`find $oldfilename | sort`;
#k=`ls -l $oldfilename |wc -l`;
k=0;
echo $files;
for readfile in $files; do

if [ ! -d $readfile ]; then

echo;
echo $readfile
echo;
temp=$k-$newfilename.$extension;
if [ "$readfile" = "$temp" ]; then
echo source Current File Name and New File Name in The Same
exit 0
else
mv -v $readfile $temp;
fi;
fi;
let k=k+1;
done;

echo =====================end=====================

rc.local

Debian-user # sh rc.localhowto

===================rc.localhowto==================
echo '#!/bin/sh' > /etc/rc.local

chmod 744 /etc/rc.local

RL=`grep ':initdefault:' /etc/inittab | cut -d: -f2`

echo "LO:$RL:once:/etc/rc.local" >> /etc/inittab

killall -HUP init
=================end====================

jigdo-lite Debian

we can mount all iso i386 and amd64 debian by this script then by another script you can download all jigdo file

mbahmani@nagcmdb:~$ cat updateiso/scripts/isomount.sh

mbahmani@nagcmdb:~$ cat updateiso/scripts/isomount.sh
#!/bin/sh
#########################################################################################
#isomount.sh #
#mount iso by batch #
#It created by Mahdi Bahmani at 17-11-2010 #
#########################################################################################

#mbahmani@nagcmdb:~$ ls updateiso/osiso/testing/amd64/
#amd64-squeez-testing-26-Apr-2010 isodir mnt
#mbahmani@nagcmdb:~$ ls updateiso/osiso/testing/i386/
#i386-squeez-16-Feb-2010 i386-squeez-26-Feb-2010 isodir mnt
#mbahmani@nagcmdb:~$

########################################################################################



set -x
#variables to change is newisopi386 newisopamd64
newisopi386="i386-squeez-26-Feb-2010"
newisopamd64="amd64-squeez-testing-26-Apr-2010"

###############################################################
#static variables

staticp1="/home/mbahmani/updateiso/osiso/testing/i386/"<br />staticp2="/home/mbahmani/updateiso/osiso/testing/amd64/"<br /><br />isodiri386="isodir"<br />isodiramd64="isodir"<br /><br />sudo mkdir $staticp1$newisopi386<br />sudo mkdir $staticp2$newisopamd64<br />sudo mkdir $staticp1"mnt"
sudo mkdir $staticp2"mnt"
####to be changed##########
sudo rm $staticp1$isodiri386
sudo rm $staticp2$isodiramd64
sudo rm /home/mbahmani/updateiso/newlinkiso32
sudo rm /home/mbahmani/updateiso/newlinkiso

sudo ln -s $staticp1$newisopi386 $staticp1$isodiri386
sudo ln -s $staticp2$newisopamd64 $staticp2$isodiramd64

sudo ln -s $staticp1$newisopi386 /home/mbahmani/updateiso/newlinkiso32
sudo ln -s $staticp2$newisopamd64 /home/mbahmani/updateiso/newlinkiso
##########################

CDCOUNT=7
CURCD=1
MNTDIRi386=$staticp1"mnt/"
MNTDIR=$staticp2"mnt/"

ISOSRCi386="$staticp1$isodiri386/debian-testing-i386-DVD-"<br />ISOSRC="$staticp2$isodiramd64/debian-testing-amd64-DVD-"<br /><br />#main<br />if [ "$1" = '-u' ]
then

while test $CURCD -le $CDCOUNT
do
umount $MNTDIR$CURCD
umount $MNTDIRi386$CURCD
CURCD="`expr $CURCD + 1`"<br /> done<br />fi<br /><br />if [ "$1" = '-m' ]
then
while test $CURCD -le $CDCOUNT
do
mount -o loop $ISOSRC$CURCD.iso $MNTDIR$CURCD
mount -o loop $ISOSRCi386$CURCD.iso $MNTDIRi386$CURCD
CURCD="`expr $CURCD + 1`"<br /> done<br />fi<br />set +x<br /><br /></span></p> <p style="text-align: left;">mbahmani@nagcmdb:~$

mbahmani@nagcmdb:~$ cat updateiso/scripts/getdeb
#!/bin/sh
########################################################################
#getdeb #
#Get Debian by jigdo-lite by batch #
#It created by Mahdi Bahmani at 17-11-2010 #
########################################################################
set -x

#variables
WORKDIR="/home/mbahmani/updateiso/jigdo/amd64-squeez-testing-16-Feb-2010"<br />URLTMPL="http://cdimage.debian.org/cdimage/weekly-builds/amd64/jigdo-dvd/debian-testing-amd64-DVD-XXX.jigdo"
CDCOUNT=7
SCANDIR="/home/mbahmani/updateiso/osiso/testing/amd64/mnt/"<br />JIGDOLITE="jigdo-lite --scan"<br /><br />#main<br />cd $WORKDIR<br />yes | tr 'y' '\012' | { <br /> CURCD=1 <br /> while test $CURCD -le $CDCOUNT <br /> do <br /> URL="`echo \"$URLTMPL\" | sed s/XXX/$CURCD/`"
$JIGDOLITE $SCANDIR$CURCD/ "$URL"
CURCD="`expr $CURCD + 1`"
done
}
set +x
mbahmani@nagcmdb:~$

mbahmani@nagcmdb:~$ cat updateiso/scripts/getdebi386
#!/bin/sh
#########################################################################################
#getdebi386 #
#Get Debian by jigdo-lite by batch #
#It created by Mahdi Bahmani at 17-11-2010 #
#########################################################################################
set -x

#variables
WORKDIR="/home/mbahmani/updateiso/jigdo/i386-squeez-testing-26-Apr-2010"<br />URLTMPL="http://cdimage.debian.org/cdimage/weekly-builds/i386/jigdo-dvd/debian-testing-i386-DVD-XXX.jigdo"<br />CDCOUNT=7<br />SCANDIR="/home/mbahmani/updateiso/osiso/testing/i386/mnt/"<br />JIGDOLITE="jigdo-lite --scan"<br /><br />#main<br />cd $WORKDIR<br />yes | tr 'y' '\012' | {<br /> CURCD=1<br /> while test $CURCD -le $CDCOUNT<br /> do<br /> URL="`echo \"$URLTMPL\" | sed s/XXX/$CURCD/`"
$JIGDOLITE $SCANDIR$CURCD/ "$URL"
CURCD="`expr $CURCD + 1`"<br /> done<br />}<br />set +x<br />mbahmani@nagcmdb:~$<br /><br /></p>"

joomla backup howto

configuration files

1- crontab

2-mysql_bk_script

3-www backup script


slinux:~# crontab -e

#m h dom mon dow command
40 12 * * * /bin/sh /home/script/automysqlbackup.sh.2.5
40 19 * * * /bin/sh /home/script/automysqlbackup.sh.2.5
40 19 * * * /bin/sh /home/script/sitebackscript
40 12 * * * /bin/sh /home/script/sitebackscript


slinux:~# cat /home/script/sitebackscript
tar -cvvf /home/backup/sitembctux/mbctuxwebsite_`date +%A_%d_%B%m_%Y_%Hh_%Mm`.tar /var/www/mbctux

#!/bin/bash
#
# MySQL Backup Script
# VER. 2.5 - http://sourceforge.net/projects/automysqlbackup/
# Copyright (c) 2002-2003 This e-mail address is being protected from spambots. You need JavaScript enabled to view it.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
# Set the following variables to your system needs
# (Detailed instructions below variables)
#

# Username to access the MySQL server e.g. dbuser
USERNAME=root

# Username to access the MySQL server e.g. password
PASSWORD=123

# Host name (or IP address) of MySQL server e.g localhost
DBHOST=localhost

# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
#DBNAMES="DB1 DB2 DB3"<br />DBNAMES="mbctux"<br /># Backup directory location e.g /backups<br />BACKUPDIR="/home/backup"<br /><br /># Mail setup<br /># What would you like to be mailed to you?<br /># - log : send only log file<br /># - files : send log file and sql files as attachments (see docs)<br /># - stdout : will simply output the log to the screen if run manually.<br /># - quiet : Only send logs if an error occurs to the MAILADDR.<br />MAILCONTENT="stdout"<br /><br /># Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])<br />MAXATTSIZE="4000"<br /><br /># Email Address to send mail to? ( This e-mail address is being protected from spambots. You need JavaScript enabled to view it. )<br />MAILADDR=" This e-mail address is being protected from spambots. You need JavaScript enabled to view it. "<br /><br /><br /># ============================================================<br /># === ADVANCED OPTIONS ( Read the doc's below for details )===<br />#=============================================================<br /><br /># List of DBBNAMES for Monthly Backups.<br />MDBNAMES="mysql $DBNAMES"<br /><br /># List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes)
DBEXCLUDE=""<br /><br /># Include CREATE DATABASE in backup?<br />CREATE_DATABASE=yes<br /><br /># Separate backup directory and file for each DB? (yes or no)<br />SEPDIR=yes<br /><br /># Which day do you want weekly backups? (1 to 7 where 1 is Monday)<br />DOWEEKLY=6<br /><br /># Choose Compression type. (gzip or bzip2)<br />COMP=gzip<br /><br /># Compress communications between backup server and MySQL server?<br />COMMCOMP=no<br /><br /># Additionally keep a copy of the most recent backup in a seperate directory.<br />LATEST=no<br /><br /># The maximum size of the buffer for client/server communication. e.g. 16MB (maximum is 1GB)<br />MAX_ALLOWED_PACKET=<br /><br /># For connections to localhost. Sometimes the Unix socket file must be specified.<br />SOCKET=<br /><br /># Command to run before backups (uncomment to use)<br />#PREBACKUP="/etc/mysql-backup-pre"<br /><br /># Command run after backups (uncomment to use)<br />#POSTBACKUP="/etc/mysql-backup-post"<br /><br />#<br /># Options documantation<br />#<br /># Set USERNAME and PASSWORD of a user that has at least SELECT permission<br /># to ALL databases.<br />#<br /># Set the DBHOST option to the server you wish to backup, leave the<br /># default to backup "this server".(to backup multiple servers make<br /># copies of this file and set the options for that server)<br />#<br /># Put in the list of DBNAMES(Databases)to be backed up. If you would like<br /># to backup ALL DBs on the server set DBNAMES="all".(if set to "all" then
# any new DBs will automatically be backed up without needing to modify
# this backup script when a new DB is created).
#
# If the DB you want to backup has a space in the name replace the space
# with a % e.g. "data base" will become "data%base"
# NOTE: Spaces in DB names may not work correctly when SEPDIR=no.
#
# You can change the backup storage location from /backups to anything
# you like by using the BACKUPDIR setting..
#
# The MAILCONTENT and MAILADDR options and pretty self explanitory, use
# these to have the backup log mailed to you at any email address or multiple
# email addresses in a space seperated list.
# (If you set mail content to "log" you will require access to the "mail" program
# on your server. If you set this to "files" you will have to have mutt installed
# on your server. If you set it to "stdout" it will log to the screen if run from
# the console or to the cron job owner if run through cron. If you set it to "quiet"
# logs will only be mailed if there are errors reported. )
#
# MAXATTSIZE sets the largest allowed email attachments total (all backup files) you
# want the script to send. This is the size before it is encoded to be sent as an email
# so if your mail server will allow a maximum mail size of 5MB I would suggest setting
# MAXATTSIZE to be 25% smaller than that so a setting of 4000 would probably be fine.
#
# Finally copy automysqlbackup.sh to anywhere on your server and make sure
# to set executable permission. You can also copy the script to
# /etc/cron.daily to have it execute automatically every night or simply
# place a symlink in /etc/cron.daily to the file if you wish to keep it
# somwhere else.
# NOTE:On Debian copy the file with no extention for it to be run
# by cron e.g just name the file "automysqlbackup"
#
# Thats it..
#
#
# === Advanced options doc's ===
#
# The list of MDBNAMES is the DB's to be backed up only monthly. You should
# always include "mysql" in this list to backup your user/password
# information along with any other DBs that you only feel need to
# be backed up monthly. (if using a hosted server then you should
# probably remove "mysql" as your provider will be backing this up)
# NOTE: If DBNAMES="all" then MDBNAMES has no effect as all DBs will be backed
# up anyway.
#
# If you set DBNAMES="all" you can configure the option DBEXCLUDE. Other
# wise this option will not be used.
# This option can be used if you want to backup all dbs, but you want
# exclude some of them. (eg. a db is to big).
#
# Set CREATE_DATABASE to "yes" (the default) if you want your SQL-Dump to create
# a database with the same name as the original database when restoring.
# Saying "no" here will allow your to specify the database name you want to
# restore your dump into, making a copy of the database by using the dump
# created with automysqlbackup.
# NOTE: Not used if SEPDIR=no
#
# The SEPDIR option allows you to choose to have all DBs backed up to
# a single file (fast restore of entire server in case of crash) or to
# seperate directories for each DB (each DB can be restored seperately
# in case of single DB corruption or loss).
#
# To set the day of the week that you would like the weekly backup to happen
# set the DOWEEKLY setting, this can be a value from 1 to 7 where 1 is Monday,
# The default is 6 which means that weekly backups are done on a Saturday.
#
# COMP is used to choose the copmression used, options are gzip or bzip2.
# bzip2 will produce slightly smaller files but is more processor intensive so
# may take longer to complete.
#
# COMMCOMP is used to enable or diable mysql client to server compression, so
# it is useful to save bandwidth when backing up a remote MySQL server over
# the network.
#
# LATEST is to store an additional copy of the latest backup to a standard
# location so it can be downloaded bt thrid party scripts.
#
# If the DB's being backed up make use of large BLOB fields then you may need
# to increase the MAX_ALLOWED_PACKET setting, for example 16MB..
#
# When connecting to localhost as the DB server (DBHOST=localhost) sometimes
# the system can have issues locating the socket file.. This can now be set
# using the SOCKET parameter.. An example may be SOCKET=/private/tmp/mysql.sock
#
# Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands
# or scripts to perform tasks either before or after the backup process.
#
#
#
# Backup Rotation..
#
#
# Daily Backups are rotated weekly..
# Weekly Backups are run by default on Saturday Morning when
# cron.daily scripts are run...Can be changed with DOWEEKLY setting..
# Weekly Backups are rotated on a 5 week cycle..
# Monthly Backups are run on the 1st of the month..
# Monthly Backups are NOT rotated automatically...
# It may be a good idea to copy Monthly backups offline or to another
# server..
#
#
# Please Note!!
#
#
# I take no resposibility for any data loss or corruption when using
# this script..
# This script will not help in the event of a hard drive crash. If a
# copy of the backup has not be stored offline or on another PC..
# You should copy your backups offline regularly for best protection.
#
# Happy backing up...
#
#
# Restoring
#
# Firstly you will need to uncompress the backup file.
# eg.
# gunzip file.gz (or bunzip2 file.bz2)
#
# Next you will need to use the mysql client to restore the DB from the
# sql file.
# eg.
# mysql --user=username --pass=password --host=dbserver database < /path/file.sql
# or
# mysql --user=username --pass=password --host=dbserver -e "source /path/file.sql" database
#
# NOTE: Make sure you use "<" and not ">" in the above command because
# you are piping the file.sql to mysql and not the other way around.
#
# Lets hope you never have to use this.. :)
#
#
# Change Log
#
#
# VER 2.5 - (2006-01-15)
# Added support for setting MAXIMUM_PACKET_SIZE and SOCKET parameters (suggested by Yvo van Doorn)
# VER 2.4 - (2006-01-23)
# Fixed bug where weekly backups were not being rotated. (Fix by wolf02)
# Added hour an min to backup filename for the case where backups are taken multiple
# times in a day. NOTE This is not complete support for mutiple executions of the script
# in a single day.
# Added MAILCONTENT="quiet" option, see docs for details. (requested by snowsam)
# Updated path statment for compatibility with OSX.
# Added "LATEST" to additionally store the last backup to a standard location. (request by Grant29)
# VER 2.3 - (2005-11-07)
# Better error handling and notification of errors (a long time coming)
# Compression on Backup server to MySQL server communications.
# VER 2.2 - (2004-12-05)
# Changed from using depricated "-N" to "--skip-column-names".
# Added ability to have compressed backup's emailed out. (code from Thomas Heiserowski)
# Added maximum attachment size setting.
# VER 2.1 - (2004-11-04)
# Fixed a bug in daily rotation when not using gzip compression. (Fix by Rob Rosenfeld)
# VER 2.0 - (2004-07-28)
# Switched to using IO redirection instead of pipeing the output to the logfile.
# Added choice of compression of backups being gzip of bzip2.
# Switched to using functions to facilitate more functionality.
# Added option of either gzip or bzip2 compression.
# VER 1.10 - (2004-07-17)
# Another fix for spaces in the paths (fix by Thomas von Eyben)
# Fixed bug when using PREBACKUP and POSTBACKUP commands containing many arguments.
# VER 1.9 - (2004-05-25)
# Small bug fix to handle spaces in LOGFILE path which contains spaces (reported by Thomas von Eyben)
# Updated docs to mention that Log email can be sent to multiple email addresses.
# VER 1.8 - (2004-05-01)
# Added option to make backups restorable to alternate database names
# meaning that a copy of the database can be created (Based on patch by Rene Hoffmann)
# Seperated options into standard and advanced.
# Removed " from single file dump DBMANES because it caused an error but
# this means that if DB's have spaces in the name they will not dump when SEPDIR=no.
# Added -p option to mkdir commands to create multiple subdirs without error.
# Added disk usage and location to the bottom of the backup report.
# VER 1.7 - (2004-04-22)
# Fixed an issue where weelky backups would only work correctly if server
# locale was set to English (issue reported by Tom Ingberg)
# used "eval" for "rm" commands to try and resolve rotation issues.
# Changed name of status log so multiple scripts can be run at the same time.
# VER 1.6 - (2004-03-14)
# Added PREBACKUP and POSTBACKUP command functions. (patch by markpustjens)
# Added support for backing up DB's with Spaces in the name.
# (patch by markpustjens)
# VER 1.5 - (2004-02-24)
# Added the ability to exclude DB's when the "all" option is used.
# (Patch by kampftitan)
# VER 1.4 - (2004-02-02)
# Project moved to Sourceforge.net
# VER 1.3 - (2003-09-25)
# Added support for backing up "all" databases on the server without
# having to list each one seperately in the configuration.
# Added DB restore instructions.
# VER 1.2 - (2003-03-16)
# Added server name to the backup log so logs from multiple servers
# can be easily identified.
# VER 1.1 - (2003-03-13)
# Small Bug fix in monthly report. (Thanks Stoyanski)
# Added option to email log to any email address. (Inspired by Stoyanski)
# Changed Standard file name to .sh extention.
# Option are set using yes and no rather than 1 or 0.
# VER 1.0 - (2003-01-30)
# Added the ability to have all databases backup to a single dump
# file or seperate directory and file for each database.
# Output is better for log keeping.
# VER 0.6 - (2003-01-22)
# Bug fix for daily directory (Added in VER 0.5) rotation.
# VER 0.5 - (2003-01-20)
# Added "daily" directory for daily backups for neatness (suggestion by Jason)
# Added DBHOST option to allow backing up a remote server (Suggestion by Jason)
# Added "--quote-names" option to mysqldump command.
# Bug fix for handling the last and first of the year week rotation.
# VER 0.4 - (2002-11-06)
# Added the abaility for the script to create its own directory structure.
# VER 0.3 - (2002-10-01)
# Changed Naming of Weekly backups so they will show in order.
# VER 0.2 - (2002-09-27)
# Corrected weekly rotation logic to handle weeks 0 - 10
# VER 0.1 - (2002-09-21)
# Initial Release
#
#
#
#
#
# Should not need to be modified from here down!!
#
#
#
#
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin
DATE=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21
DOW=`date +%A` # Day of the week e.g. Monday
DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday
DOM=`date +%d` # Date of the Month e.g. 27
M=`date +%B` # Month e.g January
W=`date +%V` # Week Number e.g 37
VER=2.5 # Version Number
LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log # Logfile Name
LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log # Logfile Name
BACKUPFILES=""<br />OPT="--quote-names --opt" # OPT string for use with mysqldump ( see man mysqldump )

# Add --compress mysqldump option to $OPT
if [ "$COMMCOMP" = "yes" ];
then
OPT="$OPT --compress"<br /> fi<br /><br /># Add --compress mysqldump option to $OPT<br />if [ "$MAX_ALLOWED_PACKET" ];
then
OPT="$OPT --max_allowed_packet=$MAX_ALLOWED_PACKET"<br /> fi<br /><br /># Create required directories<br />if [ ! -e "$BACKUPDIR" ] # Check Backup Directory exists.
then
mkdir -p "$BACKUPDIR"
fi

if [ ! -e "$BACKUPDIR/daily" ] # Check Daily Directory exists.
then
mkdir -p "$BACKUPDIR/daily"
fi

if [ ! -e "$BACKUPDIR/weekly" ] # Check Weekly Directory exists.
then
mkdir -p "$BACKUPDIR/weekly"
fi

if [ ! -e "$BACKUPDIR/monthly" ] # Check Monthly Directory exists.
then
mkdir -p "$BACKUPDIR/monthly"
fi

if [ "$LATEST" = "yes" ]
then
if [ ! -e "$BACKUPDIR/latest" ] # Check Latest Directory exists.
then
mkdir -p "$BACKUPDIR/latest"
fi
eval rm -fv "$BACKUPDIR/latest/*"
fi

# IO redirection for logging.
touch $LOGFILE
exec 6>&1 # Link file descriptor #6 with stdout.
# Saves stdout.
exec > $LOGFILE # stdout replaced with file $LOGFILE.
touch $LOGERR
exec 7>&2 # Link file descriptor #7 with stderr.
# Saves stderr.
exec 2> $LOGERR # stderr replaced with file $LOGERR.


# Functions

# Database dump function
dbdump () {
mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 > $2
return 0
}

# Compression function plus latest copy
SUFFIX=""<br />compression () {<br />if [ "$COMP" = "gzip" ]; then
gzip -f "$1"
echo
echo Backup Information for "$1"
gzip -l "$1.gz"
SUFFIX=".gz"<br />elif [ "$COMP" = "bzip2" ]; then
echo Compression information for "$1.bz2"
bzip2 -f -v $1 2>&1
SUFFIX=".bz2"<br />else<br /> echo "No compression option set, check advanced settings"<br />fi<br />if [ "$LATEST" = "yes" ]; then
cp $1$SUFFIX "$BACKUPDIR/latest/"
fi
return 0
}


# Run command before we begin
if [ "$PREBACKUP" ]
then
echo ==============
echo "Prebackup command output."
echo
eval $PREBACKUP
echo
echo ==============
echo
fi


if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump
if [ "$CREATE_DATABASE" = "no" ]; then
OPT="$OPT --no-create-db"<br /> else<br /> OPT="$OPT --databases"<br /> fi<br />else<br /> OPT="$OPT --databases"<br />fi<br /><br /># Hostname for LOG information<br />if [ "$DBHOST" = "localhost" ]; then
HOST=`hostname`
if [ "$SOCKET" ]; then
OPT="$OPT --socket=$SOCKET"<br /> fi<br />else<br /> HOST=$DBHOST<br />fi<br /><br /># If backing up all DBs on the server<br />if [ "$DBNAMES" = "all" ]; then
DBNAMES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show databases"| sed 's/ /%/g'`"<br /><br /> # If DBs are excluded<br /> for exclude in $DBEXCLUDE<br /> do<br /> DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"`<br /> done<br /><br /> MDBNAMES=$DBNAMES<br />fi<br /> <br />echo ==============<br />echo AutoMySQLBackup VER $VER<br />echo http://sourceforge.net/projects/automysqlbackup/<br />echo <br />echo Backup of Database Server - $HOST<br />echo ==============<br /><br /># Test is seperate DB backups are required<br />if [ "$SEPDIR" = "yes" ]; then
echo Backup Start Time `date`
echo ==============
# Monthly Full Backup of all Databases
if [ $DOM = "01" ]; then
for MDB in $MDBNAMES
do

# Prepare $DB for using
MDB="`echo $MDB | sed 's/%/ /g'`"<br /><br /> if [ ! -e "$BACKUPDIR/monthly/$MDB" ] # Check Monthly DB Directory exists.
then
mkdir -p "$BACKUPDIR/monthly/$MDB"
fi
echo Monthly Backup of $MDB...
dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX"<br /> echo ----------------------------------------------------------------------<br /> done<br /> fi<br /><br /> for DB in $DBNAMES<br /> do<br /> # Prepare $DB for using<br /> DB="`echo $DB | sed 's/%/ /g'`"<br /> <br /> # Create Seperate directory for each DB<br /> if [ ! -e "$BACKUPDIR/daily/$DB" ] # Check Daily DB Directory exists.
then
mkdir -p "$BACKUPDIR/daily/$DB"
fi

if [ ! -e "$BACKUPDIR/weekly/$DB" ] # Check Weekly DB Directory exists.
then
mkdir -p "$BACKUPDIR/weekly/$DB"
fi

# Weekly Backup
if [ $DNOW = $DOWEEKLY ]; then
echo Weekly Backup of Database \( $DB \)
echo Rotating 5 weeks Backups...
if [ "$W" -le 05 ];then
REMW=`expr 48 + $W`
elif [ "$W" -lt 15 ];then
REMW=0`expr $W - 5`
else
REMW=`expr $W - 5`
fi
eval rm -fv "$BACKUPDIR/weekly/$DB_week.$REMW.*"
echo
dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX"<br /> echo ----------------------------------------------------------------------<br /> <br /> # Daily Backup<br /> else<br /> echo Daily Backup of Database \( $DB \)<br /> echo Rotating last weeks Backup...<br /> eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*"
echo
dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX"<br /> echo ----------------------------------------------------------------------<br /> fi<br /> done<br />echo Backup End `date`<br />echo ==============<br /><br /><br />else # One backup file for all DBs<br />echo Backup Start `date`<br />echo ==============<br /> # Monthly Full Backup of all Databases<br /> if [ $DOM = "01" ]; then
echo Monthly full Backup of \( $MDBNAMES \)...
dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX"<br /> echo ----------------------------------------------------------------------<br /> fi<br /><br /> # Weekly Backup<br /> if [ $DNOW = $DOWEEKLY ]; then<br /> echo Weekly Backup of Databases \( $DBNAMES \)<br /> echo<br /> echo Rotating 5 weeks Backups...<br /> if [ "$W" -le 05 ];then
REMW=`expr 48 + $W`
elif [ "$W" -lt 15 ];then
REMW=0`expr $W - 5`
else
REMW=`expr $W - 5`
fi
eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*"
echo
dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql"
compression "$BACKUPDIR/weekly/week.$W.$DATE.sql"
BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX"<br /> echo ----------------------------------------------------------------------<br /> <br /> # Daily Backup<br /> else<br /> echo Daily Backup of Databases \( $DBNAMES \)<br /> echo<br /> echo Rotating last weeks Backup...<br /> eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*"
echo
dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql"
compression "$BACKUPDIR/daily/$DATE.$DOW.sql"
BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX"<br /> echo ----------------------------------------------------------------------<br /> fi<br />echo Backup End Time `date`<br />echo ==============<br />fi<br />echo Total disk space used for backup storage..<br />echo Size - Location<br />echo `du -hs "$BACKUPDIR"`<br />echo<br />echo ==============<br />echo If you find AutoMySQLBackup valuable please make a donation at<br />echo http://sourceforge.net/project/project_donations.php?group_id=101066<br />echo ==============<br /><br /># Run command when we're done<br />if [ "$POSTBACKUP" ]
then
echo ==============
echo "Postbackup command output."
echo
eval $POSTBACKUP
echo
echo ==============
fi

#Clean up IO redirection
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
exec 1>&7 7>&- # Restore stdout and close file descriptor #7.

if [ "$MAILCONTENT" = "files" ]
then
if [ -s "$LOGERR" ]
then
# Include error log if is larger than zero.
BACKUPFILES="$BACKUPFILES $LOGERR"<br /> ERRORNOTE="WARNING: Error Reported - "<br /> fi<br /> #Get backup size<br /> ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//`
if [ $MAXATTSIZE -ge $ATTSIZE ]
then
BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments
mutt -s "$ERRORNOTE MySQL Backup Log and SQL Files for $HOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE #send via mutt
else
cat "$LOGFILE" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR
fi
elif [ "$MAILCONTENT" = "log" ]
then
cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR
if [ -s "$LOGERR" ]
then
cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR
fi
elif [ "$MAILCONTENT" = "quiet" ]
then
if [ -s "$LOGERR" ]
then
cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR
cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR
fi
else
if [ -s "$LOGERR" ]
then
cat "$LOGFILE"
echo
echo "###### WARNING ######"
echo "Errors reported during AutoMySQLBackup execution.. Backup failed"
echo "Error log below.."
cat "$LOGERR"
else
cat "$LOGFILE"
fi
fi

if [ -s "$LOGERR" ]
then
STATUS=1
else
STATUS=0
fi

# Clean up Logfile
eval rm -f "$LOGFILE"
eval rm -f "$LOGERR"

exit $STATUS

منابع:

http://www.debianhelp.co.uk/mysqlscript.htm

get linux system info

Debian-user#./basesysinfo
=========================basesysinfo================================
#/bin/sh

#simple script to give a summary of system information

# Author:
# http://www.pixelbeat.org/
# Notes:
# Unless you run as root, disk info can't be shown
# Changes:
# V0.1, 16 Nov 2005, Initial release
# V0.s, 22 Oct 2007, Tweak to ensure cdrom info shown.
# Comment out partition info as a bit noisy.


find_sbin_cmd() {
for base in / /usr/ /usr/local; do
if [ -e $base/sbin/$1 ]; then
echo $base/sbin/$1
exit
fi
done
}
FDISK=`which fdisk 2>/dev/null`
LSUSB=`which lsusb 2>/dev/null`
LSPCI=`which lspci 2>/dev/null`
[ -z "$FDISK" ] && FDISK=`find_sbin_cmd fdisk`
[ -z "$LSUSB" ] && LSUSB=`find_sbin_cmd lsusb`
[ -z "$LSPCI" ] && LSPCI=`find_sbin_cmd lspci`

echo "============= Drives ============="<br />(<br />sed -n 's/.* \([hs]d[a-f]$\)/\1/p' < /proc/partitions<br />[ -e /dev/cdrom ] && readlink -f /dev/cdrom | cut -d/ -f3<br />) |<br />sort | uniq |<br />while read disk; do<br /> echo -n "/dev/$disk: "<br /> if [ ! -r /dev/$disk ]; then<br /> echo "permission denied" #could parse /proc for all but
else
size=`$FDISK -l /dev/$disk | grep Disk | cut -d' ' -f3-4 | tr -d ,`
rest=`/sbin/hdparm -i /dev/$disk 2>/dev/null | grep Model`
rest=`echo $rest` #strip spaces
echo -n "$rest"
if [ ! -z "$size" ]; then
echo ", Size=$size"
else
echo
fi
fi
done

#if [ `id -u` == "0" ]; then
#echo "========== Partitions =========="<br />#$FDISK -l 2>/dev/null<br />#fi<br /><br />echo "============= CPUs ============="<br />grep "model name" /proc/cpuinfo #show CPU(s) info

echo "============= MEM ============="<br />KiB=`grep MemTotal /proc/meminfo | tr -s ' ' | cut -d' ' -f2`<br />MiB=`expr $KiB / 1024`<br />#note various mem not accounted for, so round to appropriate size<br />#on my 384MiB system over 8MiB was unaccounted for<br />#on my 1024MiB system over 20MiB was unaccounted for so round to next highest power of 2<br />round=32<br />echo "`expr \( \( $MiB / $round \) + 1 \) \* $round` MiB"<br /><br />echo "============= PCI ============="<br />$LSPCI -tv<br /><br />echo "============= USB ============="<br />$LSUSB<br />echo ==============end==============</div>"