Developer's Closet A place where I can put my PHP, SQL, Perl, JavaScript, and VBScript code.

21Jul/100
Search within files on a Linux Server

This is a very useful bit of code, used to search within files on a Linux server. Replace the . after find to search within a folder.

find . | xargs grep "search string" > search.log

This script finds all files at or below current directory, then runs a search within the files.

find . (find all files at or below current directory)
xargs (build and execute commands from standard input, separated by newline)
grep (search within the file for the quoted string)

20Jul/100
Recursively count the number of files on a Linux server

Simple script to recursively count the number of files on a Linux server.

ls -laR | wc -l

How this works:

ls (lists folder contents), -l (long listing format), -a (all), -R (recursive)
wc (word count), -l (or --lines, prints only the newline counts)

15Jul/100
Simple MySQL Database Backup

Another simple backup script. This script builds on the rsync file backup to allow daily, monthly, and yearly incremental MySQL database backups. The backup will be a file name that includes the database name followed by an underscore and either the day, month, or year. The script will overwrite daily and monthly files as needed. The script relies on a separate file to hold the password, and we all know that is a very bad idea. You should use something more secure, so use this script is only as an example. Replace databasename with the name of your database. Give it a try, it is a pretty good example of an incremental database backup.

# Author: Kevin Verhoeven
# Description: Script to backup a MySQL database based on daily, monthly, and yearly increments
#
[ ! -f asof.dat ] && touch asof.dat
ATIME=$(stat -c "%y" asof.dat)

rm asof.dat
touch asof.dat
if [ `date +%e` -eq 1 ]; then
     date +%B >> asof.dat
fi
if [ `date +%Y` -gt ${ATIME:0:4} ]; then
      echo ${ATIME:0:4} >> asof.dat
fi
date +%A >> asof.dat
for i in $(cat asof.dat); do "mysqldump" -uroot -p`cat passwordfile` --all --add-drop-table --add-locks --databases databasename > databasename_$i.sql; done
15Jul/100
Simple rsync Backup

This is a simple rsync backup that creates daily, monthly, and yearly incremental backups. This type of script would allow for recovery of just about any critical file going back many months or years. Very nice for a quick enterprise backup to impress your boss. I use it for my home web server. If the web server is hacked, the pages are protected under the www folder and will not be touched. Restore is a snap. Combine this with a great MySQL database backup and you are set.

# Author: Kevin Verhoeven
# Description: Script to backup data based on daily, monthly, and yearly increments
#
[ ! -f asof.dat ] && touch asof.dat
ATIME=$(stat -c "%y" asof.dat)

rm asof.dat
touch asof.dat
if [ `date +%e` -eq 1 ]; then
     date +%B >> asof.dat
fi
if [ `date +%Y` -gt ${ATIME:0:4} ]; then
      echo ${ATIME:0:4} >> asof.dat
fi
date +%A >> asof.dat
for i in $(cat asof.dat); do [ ! -d destination/$i/ ] && mkdir destination/$i/;touch destination/$i/rsyncbackup.log;rsync -av --stats "source/" "destination/$i/" > "destination/$i/rsyncbackup.log"; done