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

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
15Jun/100
Setup SSH Keys Between Two Linux Servers

SSH Keys will allow data to move between two Linux servers without first prompting for a login. This is useful if you want to automate tasks but don’t want to pass a username and password. This is also safer than storing a password in a file and calling the file from a script. Keep in mind that it is much more secure to use a passphrase when setting up these keys.

  1. SSH to the two Linux machines, Space and Needle.
  2. On each machine type ssh somemachine.example.com and make a connection with your regular password. This will create an .ssh dir in your home directory with proper permissions.
  3. You may access this file by typing:
  4. cd ~/.ssh/
  5. On your primary server where you want your secret keys to live (let’s say Space), type:
    ssh-keygen -t dsa
  6. This will prompt you for a secret passphrase. If this is your primary identity key, make sure to use a good passphrase. If this works right you will get two files called id_dsa and id_dsa.pub in your .ssh dir. Note: it is possible to just press the enter key when prompted for a passphrase, which will make a key with no passphrase. This is a bad idea for an identity key, so don’t do it! See below for uses of keys without passphrases.
  7. Copy the id_dsa.pub file to the other host’s .ssh dir with the name authorized_keys2.
    scp ~/.ssh/id_dsa.pub Needle:.ssh/authorized_keys2
  8. Now Needle is ready to accept your ssh key. How to tell it which keys to use? The ssh-add command will do it. For a test, type:
    ssh-agent sh -c 'ssh-add < /dev/null && bash'
  9. That command will start the ssh-agent, add your default identity (prompting you for your passphrase), and spawn a bash shell.
  10. From this new shell you should be able to:
  11. ssh Needle

This should let you in without typing a password or passphrase. Hooray! You can ssh and scp all you want from this bash shell and not have to type any password or passphrase.

Tagged as: , , No Comments
26May/090
How to Repair a MySQL Database

If a server is shutdown unexpectedly (power outage or hardware failure), one or more MySQL tables can be locked open and may need to be repaired.

Browse to the folder that contains the MySQL databases:

cd /var/lib/mysql/

Check your tables by running the following command:

myisamchk *.MYI

The easiest and safest method is to run the following command (-r means "recovery mode" and -q means "quick recovery mode"). This command will not touch the data within the database:

myisamchk -r -q tbl_name

A more difficult repair to run is as follows. This command will delete damaged data:

myisamchk -r tbl_name

Reference http://dev.mysql.com/doc/refman/5.0/en/myisam-repair.html for more information.