Tag Archives: Linux

A small MySQL backup script using mysqldump and creates one file per database

This is the script I use for all my Amazon RDS instances local backups (called from an EC2). It gives me one db backup file for every database in the RDS instance, and at the end it makes sure that I don’t have any files older then 30 days. I put it here now so I don’t loose it again 🙂

#!/bin/bash

DATE=$(date +"%Y%m%d_%H%M")
DATABASES=`mysql -hsomedbname-someregion.rds.amazonaws.com -udbadmin -pmypassword -e "show databases;"`

for db in $DATABASES; do
        echo $db
        mysqldump --databases $db --single-transaction -hsomedbname-someregion.rds.amazonaws.com -udbadmin -pmypassword | gzip > /opt/backup/mysql.backup.$db.$DATE.sql.gz
done
find /opt/backup/* -mtime +30 -exec rm {} \;

I set this on a two hour cron to make sure that I have fresh backups of my production databases

0 */2 * * * /opt/scripts/mysql_db_backup.sh > /var/log/mysql_db_backup.log

Find all hosts on network with Nmap

To find all pingable hosts on the newtwork you are currently on first find your own ip. In Linux/OSX you can run the command ifconfig (windows uses the ‘ipconfig’ command):

malen@LKGADEFB8:~$ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr 00:1d:7e:ad:ef:b8  
          inet addr:192.168.0.77  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::21d:7eff:fead:efb8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23647854 errors:0 dropped:83 overruns:0 frame:0
          TX packets:31522391 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:320343317 (305.5 MiB)  TX bytes:3340057852 (3.1 GiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1736 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1736 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:159128 (155.3 KiB)  TX bytes:159128 (155.3 KiB)

You here see your IP at ‘inet addr’ on eth0: 192.168.0.77. To see if there are any other hosts on the 192.168.0.x net use:

nmap -v -sP 192.168.0.1/24

This will ping all hosts on 192.168.0.x and show your result in a list

Host 192.168.0.1 appears to be down.
Host 192.168.0.2 appears to be down.
Host 192.168.0.3 appears to be down.
Host 192.168.0.4 appears to be down.
Host 192.168.0.5 appears to be down.
...
Host Slug (192.168.0.77) appears to be up.
...
Host 192.168.0.250 appears to be down.
Host 192.168.0.251 appears to be down.
Host 192.168.0.252 appears to be down.
Host 192.168.0.253 appears to be down.
Host 192.168.0.254 appears to be down.
Host 192.168.0.255 appears to be down.

Tested on OSX 10.7.4 and Debian Lenny

Using rsync to download large files over ssh with resume option

In the last months I have been doing this a lot so it might be best to put it up here so I don’t have to google it every time. There is a lot of cool features to rsync. The one we will concentrate on here is the –partial option that lets us resume a broken download. Have to start over with the download of a large file can be a real pain

Basic syntax

rsync --partial --progress --rsh=ssh user@host:remote_file local_file

I also use the –progress option to output the progress of the download

Example:

rsync --partial --progress --rsh=ssh niklas@niklasottosson.com:.vimrc .

This will download the file ‘.vimrc’ from my home directory on niklasottosson.com and into the folder I’m currently in. If the download is interrupted rsync will keep the partial downloaded file and resume from it when the downloaded is started again

Tested on OSX 10.7.4 and rsync version 3.0.3