Archive for the 'SQL' Category

A very simple offsite backup over ssh/scp

Wednesday, August 27th, 2008

I've been setting up a very simple backup of one site's MySQL database to another server today.

What I needed to be done is to have the MySQL database files to get archived, compressed and transferred to my other server, and be named after the actual backup date and time. And that operation should happen every night.

Here's the full code for those who are in hurry (a cool one-liner heh :), and more detailed explanation of steps taken will follow.

ssh root@remoteserver.example.com 'mysqladmin flush-tables –socket=/tmp/mysql.sock; rm -f remote_db.tar* && tar cf remote_db.tar /var/lib/mysql && bzip2 remote_db.tar' && scp root@remoteserver.example.com:~/remote_db.tar.bz2 /home/mike/remote-backups/`date +%y%m%d_%H%M%S`.tar.bz2

And the explanation.

First, the server I'm backup up from is "remoteserver.example.com" and the forementioned command is invoked from the server I am backing up to. Also, I have my public ssh key installed for root account on the remote server, so I don't need to enter password to log into the remote server. You can read more about setting up SSH keys here.

First step is to log into the remote server, which is achieved simply by running

ssh root@remoteserver.example.com

However, actually instead of logging into the server, I only need to execute some commands on the remote server. This can be accomplished by giving a string of commands to execute as a second (last parameter to ssh command), for example:

ssh root@remoteserver.example.com 'ls -lh'

will give you a listing of files of remote server root's home directory.

So, now that we are connected to the remote server, we actually just need to prepare backup files which we will later transfer from remote to local server. Though the command is actually a one-liner, I'll split the lines for easier understanding, and give them numbers. Also, please note that the command concatenation with the && symbol does the following - it runs next command in chain only of previous command executed successfully.

1. mysqladmin flush-tables –socket=/tmp/mysql.sock
2. rm -f remote_db.tar*
3. tar cf remote_db.tar /var/lib/mysql
4. bzip2 remote_db.tar

#1 flushes mysql tables (so everything that is possibly in memory cache is writted to disk).

#2 removes any previous backup files (that's easy)

#3 this archives the whole MySQL data directory (this path can differ from mine, depending on your installaion parameters!). Also, please note that this approach is NOT SAFE! You can easily get a corrupted backup if any data changes during the archival process. The reason why I'm doing it myself is because I have 100% guarantee that the database will not be updated (the backup is for some inter-corporate system, and I have 100% guarantee that nobody is accessing the database at 3 o'clock in the morning when my backup task is running). You might want to lock tables during backup and unlock them once it is complete. Also, I'm backing everything up this way because I need a drop-in backup - anything happens, and I can just drop the backed up DB in place of the old one.

#4 and the final step is just to compress the backup archive to lessen the time required for transfer. You can compress it in one step actually, using 'tar cjf yourfile.tar ….'. The reason I'm running it in two steps is in order to lessen the time require for creating a database snapshot, so there's even less probability of database being modified during archivation process (tar takes ~5 seconds, tar with bzipping takes almost a minute).

Now, we have the backup file prepared on remote server, and all we need to do is to transfer it to our local server. That's an easy task.

Using the scp command for that task (you can read a little more about it here)

scp root@remoteserver.example.com:~/remote_db.tar.bz2 /home/mike/remote-backups/`date +%y%m%d_%H%M%S`.tar.bz2

There's a little trick here though! I am naming backup files after the date and time I copied them to the local server.

Well that's all. Hopefully somebody find info here helpful :)

PS: Also notice, there is no error checking and notifications if something goes wrong in this script! So feel free to enchance the functionality yourself.

PPS: Oh and I almost forgot! Of course in order to do daily (or hourly, or any periodic backups for that matter), you need to add this one-liner to crontab on your backup server!

How to install Ruby, Rails, mySQL and Apache from (almost) the scratch

Tuesday, April 3rd, 2007

I've been installing everything that is needed to run Ruby on Rails application onto a host (which is CentOS 4.x), which didn't even have Ruby installed, mySQL was too old and Apache didn't have required modules for running Mongrel clusters. So I had to install everything from the scratch, and the whole process was documented so I could repeat it in the future :) But may be this will helpful to someone else too, so here it is, right from my Yojimbo's note :)
Just one thing to notice. CentOS already has some of the required libs installed (zlib, libpng, libjpeg and freetype), so if you don't have them installed, do it before proceeding. I think there are plenty of info on this matter so I'll omit it.

Now, to the business.

RUBY

wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.5.tar.gz
tar zxvf ruby-1.8.5.tar.gz
cd ruby-1.8.5
./configure
make
sudo make install

Warning! If you're on Mac, you have to use the following string to configure Ruby install:

./configure –prefix=/usr/local –enable-pthread

On Mac OS X, once you have installed Ruby, you have to fix default paths for your default shell, because Ruby installs itself to /usr/local/bin by default, and Apple's default binaries location is in /usr/bin. Do this in Terminal:

pico ~/.bash_login

# and paste the following text:
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
# save the file (Control-W) and relogin, or just re-open terminal session before proceeding.

RAILS : GEMS

wget http://rubyforge.rubyuser.de/rubygems/rubygems-0.9.2.tgz
tar zxvf rubygems-0.9.2.tgz
cd rubygems-0.9.2
ruby setup.rb

RAILS: CORE (possible to use –include-dependencies, but i was getting some 'not found' error with this option)

#install the specific version, which happens to be 1.2.2 in this case.
gem install -v=1.2.2 rails

RAILS: STANDARD STUFF (possible to use –include-dependencies)

gem install capistrano
gem install mongrel
gem install mongrel_cluster
gem install gettext
gem install unicode

APACHE 2 with load balancer (for Mongrel cluster)

wget http://ftp.kddilabs.jp/infosystems/apache/httpd/httpd-2.2.4.tar.gz
tar zxvf httpd-2.2.4.tar.gz
cd httpd-2.2.4
./configure –enable-rewrite –enable-proxy –enable-load-balancer –enable-mods-shared=ALL
make
sudo make install

GHOSTSCRIPT & FONTS

# we need to install/update ghostscript and install ghostscript-fonts in order to get rid of the "`get_type_metrics': unable to read font `(null)' (Magick::ImageMagickError)" error:

wget http://jaist.dl.sourceforge.net/sourceforge/ghostscript/ghostscript-8.56.tar.gz
tar zxvf ghostscript-8.56.tar.gz
cd ghostscript-8.56
./configure
make
sudo make install

wget http://jaist.dl.sourceforge.net/sourceforge/gs-fonts/ghostscript-fonts-std-8.11.tar.gz
tar zxvf ghostscript-fonts-std-8.11.tar.gz
mv fonts /usr/local/share/ghostscript/8.56/

IMAGEMAGICK (required by RMagick)

wget ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/ImageMagick-6.3.3-10.tar.gz
cd ImageMagick-6.3.3
./configure
make
sudo make install

RAILS: RMAGICK (manual install - I have always had multi-systems compatibility problems with the gem install)

wget http://files.rubyforge.mmmultiworks.com/rmagick/RMagick-1.15.4.tar.gz
tar zxvf RMagick-1.15.4.tar.gz
cd RMagick-1.15.4
./configure
make
sudo make install

MYSQL

wget http://ftp.iij.ad.jp/pub/db/mysql/Downloads/MySQL-5.0/mysql-5.0.37.tar.gz
tar zxvf mysql-5.0.37.tar.gz
cd mysql-5.0.37
./configure –with-extra-charsets=all –prefix=/usr/local/mysql
make
sudo make install

#in /etc/my.cnf
[mysqld]
set-variable = max_allowed_packet=32M # I upload large images into DBs, therefore need a bigger allowed packet size

(also need to change encodings to utf8)
default-character-set=utf8

SUBVERSION (required to capistrano deployment to work)

wget http://subversion.tigris.org/downloads/subversion-1.4.3.tar.gz
wget http://subversion.tigris.org/downloads/subversion-deps-1.4.3.tar.gz
tar zxvf subversion-1.4.3.tar.gz
tar zxvf subversion-deps-1.4.3.tar.gz
cd subversion-1.4.3
./configure
make
sudo make install

That all folks :) Hope this was helpful.

Integration of Spry and PHP/MySQL

Wednesday, July 5th, 2006

Adobe have recently released its Spry Ajax framework for public beta-test by developers and I've been playing with this baby for a while. And here's some kind of report of what I have learned.

(more…)

Brain bug which is unfixable ;)

Friday, June 16th, 2006

Well it was fun for the first time, it was OK for 10th.. But after first hundred times it seems to become clear enough that I need some patch applied to my poor brain ;)

Here's how you try (unsuccessfully) to get a list of tables in a database ;)
(more…)

ASP for PHP programmers

Friday, February 17th, 2006

I have started a simple page about problems and solutions to them for PHP programmers who need to move their PHP codebase to ASP. Still a bit too simple, but oh well better something that nothing

ASP for PHP Programmers entry