Backup and Rotate MySQL Databases Simple Bash Script

Make a directory ( it can anywhere ) called baks/mysql

mkdir -p /baks/mysql

Create a file (it can be anywhere) called /root/mysql_backups.sh and put this script in it

#!/bin/bash

# modify the following to suit your environment
export DB_BACKUP="/baks/mysql"
export DB_USER="root"
export DB_PASSWD="your-mysql-password-goes-here"

# title and version
echo ""
echo "Backup and rotate all mysql databases"
echo "--------------------------"

rm -rf $DB_BACKUP/04
mv $DB_BACKUP/03 $DB_BACKUP/04
mv $DB_BACKUP/02 $DB_BACKUP/03
mv $DB_BACKUP/01 $DB_BACKUP/02
mkdir $DB_BACKUP/01

echo "* Creating backup..."
mysqldump --user=$DB_USER --password=$DB_PASSWD --all-databases | bzip2 > $DB_BACKUP/01/mysql-`date +%Y-%m-%d`.bz2
echo "----------------------"
echo "Done"
exit 0

Install it via cron and have it run at 3:10 am every morning.

crontab -e

10 3 * * * /root/mysql_backups.sh  > /baks/status.log

This script will save the last 4 days of data.

How to Copy and Paste to/from the Global Register with Tmux on Mac OS X

Using the system clipboard with tmux on OS X is broken. I really like tmux but having copy and paste is kind of important for me. Here is my attempt to get it back with a simple bash script until I find something better. The approach is to take a named pipe and feed it the contents of “tmux showb”. A bash script will manage the pipe and because this script is initialized from a normal session it will write to the system clipboard just fine.

In my .bash_profile…

pipe4tmux=/tmp/pipe4tmux
alias tcp="tmux showb > $pipe4tmux"
if [[ ! -p $pipe4tmux ]]; then
~/pipe4tmux.sh &
fi

And in pipe4tmux.sh…

#!/bin/bash
pipe4tmux=/tmp/pipe4tmux
echo "Starting named pipe $pipe4tmux"
trap "rm -f $pipe4tmux" EXIT

if [[ ! -p $pipe4tmux ]]; then
  mkfifo $pipe4tmux
fi

while true
do
  pbcopy < $pipe4tmux
done

echo "Quitting pipe4tmux $pip4tmux"

Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time

./script/console production
# =>
Loading production environment (Rails 2.3.5)
Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

You then scratch your head and run

which gem
which ruby
which rails

to find that all appears to be in order. You have rubygems installed , ruby is installed and so is rails. You also find that each are pointing to the correct location, which is something like /usr/bin/gem -> /opt/ruby-enterprise-x.x.x.x/bin/gem, where x.x.x.x is the version of REE.

The problem isn’t however, with any of the above. The issue is with the location of irb. If you installed (like me) irb with apt-get install irb, then irb isn’t aware of your shiny new REE and ruby gems. It’s a simple fix however, unlink irb and symlink the /usr/bin/irb to REE’s irb like so…

rm /usr/bin/irb

And symlink it to the irb that REE has in bin

ln -s /opt/ruby-enterprise-x.x.x.x/bin/irb /usr/bin/irb

Now cd into your rails app and run

./script/console production
21 Sep 2010, 3:53pm
Linux:
by

1 comment

How Many Gigs of RAM Are On My Server?

How much memory is on my linux server? Run the free command

free -g
 total       used       free     shared    buffers     cached
Mem:             2          1          0          0          0          1
-/+ buffers/cache:          0          1
Swap:            3          0          3

Which will tell you memory in Gigs. You can pass other flags, such as, -m or -k, which will give you the number in megs and kilobytes respectively.

The man page is as follows

man free

FREE(1)                                                                       Linux User's Manual                                                                      FREE(1)

NAME
       free - Display amount of free and used memory in the system

SYNOPSIS
       free [-b | -k | -m | -g] [-o] [-s delay ] [-t] [-V]

DESCRIPTION
       free  displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.  The shared memory column should be
       ignored; it is obsolete.

   Options
       The -b switch displays the amount of memory in bytes; the -k switch (set by default) displays it in kilobytes; the -m switch displays it in megabytes;  the  -g  switch
       displays it in gigabytes.

       The -t switch displays a line containing the totals.

       The  -o  switch  disables the display of a "buffer adjusted" line.  If the -o option is not specified, free subtracts buffer memory from the used memory and adds it to
       the free memory reported.

       The -s switch activates continuous polling delay seconds apart. You may actually specify any floating point number for delay, usleep(3) is used for microsecond resolu-
       tion delay times.

       The -V displays version information.

FILES
       /proc/meminfo
              memory information

SEE ALSO
       ps(1), slabtop(1), vmstat(8), top(1)

Have Git Email Committers After Pushes

You need a Mail Transfer Agent MTA on the server. The easiest way is to install Sendmail, which Git uses by default.

apt-get install sendmail

Remember that /etc/hosts file needs the ip address to map to the domain name your sending mail from

# vim /etc/hosts
127.0.0.1     localhost localhost.localdomain
207.136.202.87    wwwexample.com
 

Sendmail has a tendency to hang when sending mail otherwise. To test sendmail

sendmail email@example.com
this is a test
how are you today world?
.

The period on a line by itself denotes end of message and will terminate the prompt and deliver the message.

Now you need to configure Git to send email after it receives a “push” from a committer. You can add email addresses, or you can set up a mailing list to email all members. Either way, you accomplish this with the following command, just remember to cd into the git repository.

git config --add hooks.mailinglist "mailinglist@example.com"

Next you need to activate the post-receive hook, located in the hooks directory of your repository.

cp post-receive.sample post-receive

And uncomment the last line, which uses sendmail to deliver the commit message

# uncomment the last line but keep the period "."
. /usr/share/doc/git-core/contrib/hooks/post-receive-email

All done. Now just make some changes to your source code, add and commit them and you should receive an email with all the details!

Uploading Files with Curl

curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload

Courtesy of http://ariejan.net/2010/06/07/uploading-files-with-curl/

Recursively Zip Up a Directory while Excluding Certain Files Based on File Extension Type

In the example below, I’m going to zip up a directory that includes images in both PNG and PSD file formats. However, I want to exclude the PSDs because they are huge!

zip -r my-compressed-dir-without-psd.zip directory-to-zip -x '*.psd'

Uncompress A Bz2 File Using Tar Command

Uncompress a bz2 file using tar command

tar --use-compress-program bzip2 -xvf your-file-to-uncompress.tar.bz2

@source http://www.kde.gr.jp/help/doc/kdebase/doc/khelpcenter/faq/HTML/bzip2.html

Adding Public/Private Key Pairs on Mac OS X and Ubuntu for Passwordless Remote SSH Sessions

On your local machine cd into the .ssh directory in your home “~/” directory. If it doesn’t exist you can create it with “mkdir ~/.ssh”. Next generate your public/private keys and copy the public key to the remote server.

cd ~/.ssh
ssh-keygen -t rsa -b 4096
# will take a couple seconds but when finished
# specify a full path (if there is already an existing key) or hit enter to install to the default location ~/.ssh
# when it prompts for a passphrase just hit enter
# and enter again when it asks to confirm the passphrase
# then we copy the public key the remote server (this assumes you don't already have an authorized_keys file)
# copy and paste the contents of the id_rsa.pub file into the authorized_keys file otherwise
scp id_rsa.pub user@yourdomain.com:.ssh/authorized_keys

You’ll need to edit your ssh config file and restart the process to allow for public/private key authentication.

vim /etc/ssh/ssh_config
# add or uncomment these two lines
RSAAuthentication yes
PubKeyAuthentication yes
# ... and restart
/etc/init.d/ssh restart

Troubleshooting

A couple of things to keep in mind. 1) Permissions matter. Make sure that your keys are not world readable (this should be secure) Run chmod 400 on authorized_keys file.

If you had a set of keys already setup in .ssh/ on your local machine and want to install the new keys in another directory so as not to overwrite the old pair, you need to add them to ssh with this command

ssh-add ~/full/path/to/your/new/keys

More information is available here http://www.debian-administration.org/articles/152

7 Dec 2009, 9:34pm
Linux mac os x:
by

1 comment

Updating Your Twitter Status with cURL and a Bash Function

I’m usually at the command line so I wrote a little a bash function so that i can type

tweet this is really neat but kind of pointless

and it will update my twitter status! some characters trip it up but in general it’s useful for most of my tweets. The tweet function just spits out the arguments passed to it for the status parameter for the API call to twitter.

Add the following to the .bash_profile file and reload the terminal (don’t forget to add your email and pwd where appropriate).

tweet() {
   curl -u your_twitter_email_addr:your_twitter_passwd -d status="$*" http://twitter.com/statuses/update.xml
 }
 

*** Twitter still uses http basic authentication for their API. However, they are moving away from it in favor of oAuth. So I’m not sure how long this fun will last :{