Change the default crontab editor – Bash

Posted by | Posted in Bash, Code, Command-Line, Cron, Guides, Linux, OS X | Posted on 30-01-2011

You can change your default crontab (crontab -e) by setting the EDITOR environment variable in BASH:

hostname# which vi
/usr/bin/vi
hostname# export EDITOR=/usr/bin/vi
hostname# set | grep EDITOR
EDITOR=/usr/bin/vi
_=EDITOR
hostname#

Backing up and Restoring your MySQL database – Ubuntu

Posted by | Posted in Bash, Code, Command-Line, Guides, Linux, MySQL, Ubuntu | Posted on 23-07-2009

If you manage any type of databases, you know the importance of backing up your data. Whether you have file corruption from a failing hard drive, a run away script or from a hacking attempt; loosing your databases can and will break your business. If you’ve never backed up your database, now is a good time to start.

The easiest way to backup your MySQL database is with “/usr/bin/mysqldump”. You’ll need to access this application via command. Whether it is from the a console or an SSH connection.

mysqldump – a database backup program
mysqldump [options] [db_name [tbl_name ...]]

There are three general ways to invoke mysqldump:

mysqldump [options] db_name [tables]
mysqldump [options] --databases db_name1 [db_name2 db_name3...]
mysqldump [options] --all-databases

By default, the mysqldump command will dump the contents of your database directly into your terminal and not into a file. This output can be redirected anyway and to anywhere you like. But it is commonly used in this manner:

mysqldump -u [username] -p [password] [databasename] > [backup.sql]
mysqldump -u root -p PASSWORD DATABASE > DATABASE-BACKUP-DATE.sql

[username] – your database username
[password] – the password for your database
[databasename] – the name of your database
[backup.sql] – the file to which the database contents needs to be directed to

If you don’t want to use your password in the command line, you can always prompt for your password by excluding it from your command.

mysqldump -u root -p DATABASE > DATABASE-BACKUP-DATE.sql
Enter password:

If you forgot your password, you’ll get an error like this.

mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect

Or if you didn’t enter a password when one is needed, you’ll get this error.

mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect

If you’d like the convenience of having the database automatically delete the old database before re-writing the content you can use the “–add-drop-table” feature.

 mysqldump --add-drop-table -u root -p PASSWORD DATABASE > DATABASE-BACKUP-DATE.sql

If you need to only backup specific tables of the database you can use this command.

mysqldump --add-drop-table -u root -p PASSWORD DATABASE TABLE> DATABASE-TABLE-BACKUP-DATE.sql

The syntax for the command is:

mysqldump -u [username] -p [password] [databasename] [table1 table2 ....]

[tables] – This is a list of tables to backup. Each table is separated by a space.

If you are a server administrator and need to backup multiple databases at the same time, you can use the next command. You just have to be sure that the “-u” user account has permission to read those databases.

mysqldump -u root -p PASSWORD --databases DATABASE1 DATABASE2 DATABASE3 > DATABASE-1-2-3-BACKUP-DATE.sql

Or if you just want to back up ALL the databases at one, use the “–all-databases” parameter to backup all the databases in the server in one easy step.

mysqldump --all-databases> ALL-DATABASES-DATE.sql

How to back up only the data structure.

While in development, most programmers only need to back up their database structure, not their test data. To do this, use the “–no-data” parameter.

mysqldump --no-data --databases DATABASE1 DATABASE2 DATABASE3 > DATABASE-1-2-3-Structure-Bakcup-DATE.sql

If you have large databases and not enough hard drive space to store them in the regular SQL, text format, you can compressing the backup file on the fly.

mysqldump --all-databases | bzip2 -c >all-databases-backup-date.sql.bz2
 
mysqldump --all-databases | gzip >all-databases-backup-date.sql.gz

If you are away from your servers, or have multiple servers that you manage, you can still backup your databases while you are away. Use the bash script below in conjunction with a cron-tab to automate your backups.

#!/bin/sh
date=`date -I`
mysqldump --all-databases | gzip > /var/mysql_backup/mysql-backup-all-$date.sql.gz

How to restore your databases.

If the unforeseen happens and you need to totally rebuild your databases, you can do so easily from the mysqldump SQL files.

mysql -u root -p PASSWORD DATABASE < DATABASE-BACKUP-DATE.sql

Mysql Synopsis:

mysql -u [username] -p [password] [database_to_restore] < [backup_file.sql]

Restoring from the compressed files.

gunzip < DATABASE-BACKUP.sql.gz | mysql -u root -p PASSWORD DATABASE

Or do 2 at a time.

cat DATABASE-BACKUP-1.sql DATABASE-BACKUP-2.sql | mysql -u root -p PASSWORD

How to change your default shell – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 17-07-2009

If I am not using OS X, then I normally use Ubuntu. On a normal install of Ubuntu, your default command-line shell is “/bin/sh” – command interpreter (shell). To me, a low powered command shell. I normally switch it from “/bin/sh” to “/bin/bash” or “/usr/bin/bash” depending on where bash is installed.

The easiest way to do this is to use the “chsh” command. chsh stands for “change login shell”

/usr/bin/chsh

A typical change shell sequence looks something like this.

1) Find the current location of the shell you want to run. To to this, you use “which”.
2) Use the “chsh” program.
3) Enter your password.
4) Enter the new shell you want to use.

$ which bash
/bin/bash
$ chsh    
Password: 
Changing the login shell for USER
Enter the new value, or press ENTER for the default
	Login Shell [/bin/sh]: /bin/bash
$ exit

In-order to use your new shell, you have to logout and log back in.

Family Commands for chsh are

/usr/bin/chfn - change real user name and information
/etc/login.defs - shadow password suite configuration
/usr/bin/passwd - change user password

OS X’s scutil

Posted by | Posted in Bash, Code, Command-Line, OS X | Posted on 17-07-2009

scutil is OS X’s way of managing system configuration parameters. These parameters not only include HostName, ComputerName, LocalHostName, but basically anything that the configd daemon uses.

Most of those files are located in

/System/Library/SystemConfiguration/
/Library/Preferences/SystemConfiguration/
.../preferences/
.../NetworkInterfaces/
.../VirturalNetworkInterfaces/

To enter the scutil prompt, type

/usr/sbin/scutil

To view all the scutil commands, type

&gt; help

To get a list of all the scutil keys, type “list” or “list + [Pattern]”

&gt; list

To view the contents of a key, type “show + [Pattern”

&gt; show Setup:/Network/Global/IPv4

This will result in a pattern like this.

&lt; dictionary &gt; {
  ServiceOrder :  {
    0 : 59267386-49A6-401F-A23E-BE372AD748EB
    1 : 68719F97-53F0-479E-A497-8A3B809728D1
    2 : E936454A-40CA-4867-A759-61D6C38F5972
  }
}

To get your computer name, local host name or host name type,

/usr/sbin/scutil --get ComputerName
/usr/sbin/scutil --get LocalHostName
/usr/sbin/scutil --get HostName

And setting those names are just as easy.

/usr/sbin/scutil --set ComputerName NEWNAME
/usr/sbin/scutil --set LocalHostName NEWNAME
/usr/sbin/scutil --set HostName NEWNAME

On a side note. ComputerName HostName can be set to blank, but LocalHostName needs at least 1 character.

To quit scutil type,

&gt; quit

Family Commands of scutil are

/usr/sbin/scutil  -- Manage system configuration parameters
/usr/sbin/scselect  -- Select system configuration "location"
/usr/sbin/configd -- -- System Configuration Daemon

Join the mailing list

Check your email and confirm the subscription