Download files from the web via the OS X command line

Terminal IconI am frequently developing web pages and often times I find it frustrating when I can not restart my browser because I’m in the middle of a download. So when I need to download a large file and I don’t want to have to worry about whether or not Safari or Firefox continues to run with out interruption, I turn to the trusty Terminal! Next time you have a file you want to download, option-click it and copy the URL into your clipboard. Now open a Terminal window and type:
cd ~/Desktop
Now that we have changed our directory to the “Desktop” we can start our download. For the download we are going to use a built in utility called “curl”.
curl -O http://www.bluechipx.com/downloads/BlueChip.sit
Read on for a screenshot of curl in action.

Advertisements

Install wget in Mac OS X Without Homebrew or MacPorts

Install wget in Mac OS X
The command line tool wget lets you retrieve a group of files from FTP and HTTP protocols, it’s a very useful utility for web developers and powerusers to have around because it lets you do things like perform quick and dirty site backups and even mirror websites locally.
This approach is going to build and install wget in OS X from source, this means you’ll need Xcode and the Unix dev tools (free @ Mac App Store) installed, but it has the benefit of eliminating the need of a package manager like Homebrew or MacPorts.
Assuming you have Xcode and the command line tools installed, launch Terminal and enter the following commands:
First, use curl to download the latest wget source:
curl -O http://ftp.gnu.org/gnu/wget/wget-1.13.4.tar.gz
Next we use tar to uncompress the files you just downloaded:
tar -xzf wget-1.13.4.tar.gz
Use cd to change to the directory:
cd wget-1.13.4
Configure with the appropriate –with-ssl flag to prevent a “GNUTLS not available” error:
./configure --with-ssl=openssl
Build the source:
make
Install wget, it ends up in /usr/local/bin/:
sudo make install
Confirm everything worked by running wget:
wget --help
Clean up by removing wget source files when finished:
cd .. && rm -rf wget*
You’re all set, enjoy wget in Mac OS X.

Move data from an old Mac to a new Mac

How to use Migration Assistant with a new installation

When you first fire up your new Mac, you’ll be compelled to walk through the setup process. Early in that process you’ll be offered the opportunity to transfer data from your old Mac to the new one via a wired Ethernet connection. This is your first glimpse of OS X’s Migration Assistant.
Specifically, when installing Lion you’ll see the Transfer Information To This Mac window. Within this window you see four options—From Another Mac, From A Windows PC, From Time Machine Or Other Disk, and Don’t Transfer Now. As you’ll likely be moving data from your old Mac to your new one, select From Another Mac and click the Continue button at the bottom of the screen.

Make sure that both your Macs are connected, via Ethernet, to the same network. You can’t use a wireless network because your new Mac isn’t yet configured to use a wireless network. Launch Migration Assistant on your old Mac (found in /Applications/Utilities) and in the window that appears select To Another Mac and click the Continue button on that Mac. Enter your administrator’s password when prompted. You’ll be told that you must quit other applications. Do so by clicking Continue.
On your new Mac, your old Mac will appear in the Select The Source window. Click on it and click the Continue button. A passcode will appear on the new Mac and also, eventually, on your old Mac. Click Continue on the old Mac and a Transfer Your Information window will appear on the new Mac. (The old Mac will display a message that reads “Your other Mac is ready.”) On your new Mac you now have the option to select the kind of data you want to transfer. Your choices include Users, Applications, Settings, and Other Files and Folders onnameofoldMac.

Migration Assistant allows you to broadly choose the kind of data to transfer.

Click the triangle next to the Users entry and you can select specific user accounts you’d like to transfer. Click the triangle next to a user name and you have the further option to choose specific locations to transfer data from—Desktop, Documents, Downloads, Movies, Music, Pictures, Public, and All Other Files and Folders. Uncheck any that you don’t want to transfer and click the Continue button at the bottom of the window. A Transferring Information window appears that provides you with an estimate of how long the transfer will take.
When the transfer is done, you’ll be walked through the rest of the setup process. If you transferred a user account, you’ll see that account name in Lion’s login screen.

Other ways to use Migration Assistant

You can choose to migrate data from another computer, Time Machine backup, or another disk.

But suppose you unpacked, fired up, and configured your new Mac without having access to your old Mac. Or you simply didn’t have the time to wait for all your old data to transfer when you first booted the new Mac. Or, months later, you’ve realized there is a fair hunk of old data that you now want on your Mac. Have you lost your only chance to use Migration Assistant? Absolutely not. You can run it any time you like, provided that your Macs are on the same network. (You can transfer data over a wireless network once both Macs have access to such a network.)
Just navigate to /Applications/Utilities and launch Migration Assistant. In the Migration Assistant window that appears, choose From Another Mac, PC, Time Machine Backup, or Other Disk option and click Continue. Enter your administrator’s password and click OK. In the next window (seen here) choose from the two options—From Another Mac or PC or From a Time Machine Backup or Other Disk.
Regardless of which you choose, you’ll eventually see the same list of data sources you would have seen had you run Migration Assistant after you first installed Lion—Users, Applications, Settings, and Other Files and Folder onnameofmac/drive. Note, however, that if you attempt to move data from a user account on the old Mac with the same name as an existing user account on the new Mac—your user name is Chris on each, for example—you’ll be told that you must create a new user name to transfer the data to.

How to restore data from Time Machine in Mountain Lion

Restore a single file or folder

If you’re looking for a certain file or folder, start by connecting the external drive that you use for Time Machine backups or by making sure that you can connect to your Time Capsule. Click the Time Machine item in the menu bar at the top of your screen (it looks like a clock with an arrow running counterclockwise around it), and choose Enter Time Machine. Here, all of your saved backups will appear in chronological order. Use the visual timeline on the right side to scroll through your backups and look for specific items or folders. Older dates are indicated in pink on the timeline; the most up-to-date data on your Mac is indicated in white. (You’ll see the word ‘Now’ in bold, white letters on the timeline.)
Not sure which backup might hold the last copy of your missing file? Try running a Spotlight search in Time Machine based on keywords. You’ll see a search field in the upper right corner of each Finder window in the Time Machine view. Type in the file name or keywords from the file, and Spotlight will search through your backups to find the latest copy.
Not sure where to find the file you need? You can perform a Spotlight search in Time Machine, using the search field (circled).
Once you think that you’ve found what you’re looking for, use OS X’s Quick Look to make sure: Select the file and then press the spacebar to view the file without having to launch its parent application.
Preview a file before you restore it by using OS X’s Quick Look feature.
Select the file or folder, and press the Restore button. The file will automatically be copied to your desktop or to the file’s original folder. This may take some time, depending on the size of the transfer.

Restore an entire system

Sometimes you may want to restore your entire system from a backup—say, in the event of a crash or when your computer is misbehaving and you’d like to dial the clock back to a kinder, gentler time. If that’s the case, first connect your Time Machine drive. Then start up your Mac from the Mountain Lion recovery partition by pressing (and holding down) Command-R at startup. This launches Recovery Mode, a portion of your drive that Mountain Lion treats as a separate volume. It includes a few essential utilities for restoring files in case of a problem. For this approach to work, you must have a complete Time Machine backup that includes all system files.
The Mac OS X Utilities window appears. Select Restore From Time Machine Backup. This command will erase the destination drive—your Mac—so use it only if you’re restoring an entire volume to its original source or to a replacement drive. (Read the next section for setting up a new Mac or transferring data between Macs.)
Click Continue until you reach the Select a Backup Source window. here, select your Time Machine drive and click Continue. In the Select a Destination window, choose your Mac’s hard drive. (Using Recovery Mode erases your Mac’s hard drive before restoring from Time Machine; but once the process is finished, you’ll be able to log in and use your Mac normally).

Transfer data between Macs

Let’s say that you bought a new Mac and want to transfer all of the data from your old system to it. Or imagine that you simply want to transfer data from one Mac to another. Time Machine can help here, too, but with the assistance of another built-in Mountain Lion utility called Migration Assistant.
Use OS X’s Migration Assistant to transfer data from a Time Machine backup to a another computer.
Once your backup drive is connected, launch Migration Assistant (in your /Applications/Utilities folder). A Migration Assistant window will appear and ask how you want to transfer your information. Choose the From another Mac, PC, Time Machine backup, or other disk option and then click Continue. You may be prompted to enter your administrator’s password. Do so, if necessary, and clickContinue.
Next, you’ll be asked to quit other applications. Do so, and click Continue. In the next window, select the From a Time Machine backup or other disk option and click Continue.
Select your backup drive and enter a password for it, if necessary. Then choose which items you’d like to migrate (chances are, your choice will be to move everything). Click Continue, and your files will begin to transfer.

Get NIC working in Hyper V for CentOS

Create a new VM in Hyper V – 2GB ram, 60GB disk, using CentOS-6.4x86_64-minimal.iso from Public Mirror list from www.centos.org
(If moving VM – you will need to modify MAC address in the /etc/sysconfig/network-scripts/ifcfg-eth0 file) and then:
After completing this now we need to reload udev configuration to reflect the changes:
start_udev
Create the /etc/sysconfig/network file. This may already exist. If not, enter the following:
vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=COMPUTERNAME.DOMAIN
GATEWAY= (your gateway)
Create the network script for the adapter. You’ll need to know the MAC address of the network adapter – you can find this in the VM settings section.
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
NM_CONTROLLED=yes
ONBOOT=yes
DHCP_HOSTNAME=$(hostname -s)
chmod 644 /etc/sysconfig/network
Restart the network service to bring up the adapter:
service network restart

Common MySQL Commands

Common Useful MySQL Commands

Applicable to Centos Versions:
  • Centos 5.x
  • Centos 6.x

Requirements

Explanation of requirements. 
  1. MySQL and MySQL-server installed on a Centos 5.x system
  2. DBA access rights to MySQL on a Centos 5.x system

Doing the Work

Basic description of what will be done and what is expected.
  1. Database file locations:
In Centos 5.x the location where the raw uncompressed MySQL databases are stored is: /var/lib/mysql
  1. Users and DBA’s (Database Admins) create, delete, manipulate:
Note: To create, delete or alter a DBA you'll first need to be logged in as the root mysql DBA or a DBA with full access.

Show all users in the mysql database:
mysql> SELECT * FROM mysql.user;

Delete a null user:
mysql> DELETE FROM mysql.user WHERE user = ' ';

Delete all users who are not root:
mysql> DELETE FROM mysql.user WHERE NOT (host="localhost" AND user="root");

Change root user to a less-guessable name (for security):
mysql> UPDATE mysql.user SET user="mydbadmin" WHERE user="root";

Create a new DBA:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'mypass' WITH GRANT OPTION;

Add a new user with specific privileges on a specific database:
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON mydatabase.* TO 'username'@'localhost' IDENTIFIED BY 'mypass';

Add a new DBA with all privileges to a specific database:
mysql> GRANT ALL PRIVILEGES ON mydatabase.* TO 'username'@'localhost' IDENTIFIED BY 'mypass' WITH GRANT OPTION;

Change a user or DBA password:
mysql> UPDATE mysql.user SET password=oldpass("newpass") WHERE User='username';

Delete a user or DBA from the database:
mysql> DELETE FROM mysql.user WHERE user="username";
  1. Simple database manipulation:
Show all databases:
mysql> SHOW DATABASES;

Create a database:
mysql> CREATE DATABASE mydatabase;

Use a specific database:
mysql> USE mydatabase;

Delete a database:
mysql> DROP DATABASE mydatabase;

Optimize a database:
All Databases:
$ sudo mysqlcheck -o --all-databases -u root -p
Single Database:
$ sudo mysqlcheck -o db_schema_name -u root -p

Rename a database from command line (not from within MySQL), there is purposely no space beween -p and mypass:

mysqldump -u dbauser -pmypass databasename > mybackup.sql

mysql -u dbauser -pmypass newdatabasename < mybackup.sql

Show the last 200 queries to your database with the sample table name "queries" and the sample field "query_id":

mysql> SELECT * FROM queries ORDER BY query_id DESC LIMIT 200;
  1. Simple table manipulation:
Show all tables within a previously selected database 
mysql> SHOW TABLES;

Show data within a specific table in a previously selected database:
mysql> SELECT * FROM tablename;

Rename a table within a specific database:
mysql> RENAME TABLE first TO second;
or
mysql> ALTER TABLE mytable rename as mynewtable;

Drop a table from a specific database:
mysql> DROP TABLE mytable;
  1. Simple column and field manipulation:
Show columns within a table:
mysql> DESC mytable;
or
mysql> SHOW COLUMNS FROM mytable;

Update a field within a previously selected database table:
mysql> UPDATE mytable SET mycolumn="newinfo" WHERE mycolumn="oldinfo";

Select data within a specific table in a previously selected database:
mysql> SELECT * FROM mytable WHERE mycolumn='mydata' ORDER BY mycolumn2;

Insert data into a table and its columns within a specific database:
mysql> INSERT INTO mytable VALUES('column1data','column2data','column3data','column4data','column5data','column6data','column7data','column8data','column9data');

Delete data from specific fields within a column:
mysql> DELETE FROM mytable WHERE mycolumn="mydata";

Update data in a specific table, column, field:
mysql> UPDATE mytable SET column1="mydata" WHERE column2="mydata";
  1. Simple backup options:
Backup all databases uncompressed from the command line (not from within MySQL):
backup: mysqldump -u root -pmypass --all-databases > alldatabases.sql

restore full: mysql -u username -pmypass < alldatabases.sql (no space in between -p and mypass)

restore single: mysql -u username -pmypass mydb < mydb.sql (no space in between -p and mypass)

Backup all databases compressed from the command line (not from within MySQL):
With bzip2: mysqldump --all-databases | bzip2 -c > databasebackup.sql.bz2 (use bunzip2 to uncompress)

With gzip: mysqldump --all-databases | gzip > databasebackup.sql.gz (use gunzip to uncompress)

Mini script to backup all databases and tables using gzip:
#!/bin/sh
date=`date -I`
mysqldump --all-databases | gzip > /var/backup/dbbackup-$date.sql.gz

Backup a specific database only:
mysqldump -u username -pmypass databasename > backupfile.sql

Backup database structure only:
mysqldump --no-data --databases databasename > structurebackup.sql


Backup a specific database and specific tables within that database only:
mysqldump --add-drop-table -u username -pmypass databasename table_1 table_2 > databasebackup.sql

The syntax for the command to issue is:
mysqldump -u [username] -p[password] [databasename] [table1 table2 ....] > backupfilename.sql

Backing up only the database structure of specific databases, not the actual data:
mysqldump --no-data --databases db1 db2 db3 > structurebackup.sql
  1. Improve local and remote access security:
Disable the use of LOAD DATA LOCAL INFILE command, which will help to prevent against unauthorized reading from local files. This matters especially when new SQL Injection vulnerabilities in PHP applications are found. This can be set to 1 temporarily for a local admin to import a csv file into the database and then turned off again as well. The mysqld service will need to be restarted after each change.

For that purpose, the following parameter should be added in the [mysqld] section in /etc/my.cnf:

set-variable=local-infile=0

_____________________

The first change applies to the 3306/tcp port, on which MySQL listens by default. Because, according to the initial assumptions, the database will be used only by locally installed PHP applications, we can freely disable listening on that port. This will limit possibilities of attacking the MySQL database by direct TCP/IP connections from other hosts. Local communication will be still possible throw the mysql.sock socket.

In order to disable listening on the mentioned port, the following parameter should be added to the [mysqld] section of /etc/my.cnf:

skip-networking

SSH Tunneling can be used for remote backup scripts which require access to the machine.

Troubleshooting How to test

Explanation of troubleshooting basics and expectations. 
  1. Make sure mysql and mysql-server are installed:
rpm -q mysql mysql-server
  1. Set mysqld to start on boot in runlevels 2,3,4, and 5, start the service and verify it’s running (you’ll need root privileges for this):
chkconfig --level 2345 mysqld on; service mysqld start; service mysqld status

Common problems and fixes

Common problems described here, includes links to known common problems if located on another site

Use mysqladmin command to change root password

If you have never set a root password for MySQL server, the server does not require a password at all for connecting as root. To setup root password for first time, use mysqladmin command at shell prompt as follows:

$ mysqladmin -u root password NEWPASSWORD
However, if you want to change (or update) a root password, then you need to use the following command:
$ mysqladmin -u root -p'oldpassword' password newpass
For example, If the old password is abc, you can set the new password to 123456, enter:
$ mysqladmin -u root -p'abc' password '123456'