viernes, 26 de abril de 2013

MySQLDUMP Import - Export a data base



phpMyAdmin can be used to export or backup MySQL databases easily. However, if the database size is very big, it probably won’t be a good idea. phpMyAdmin allows users to save database dump as file or display on screen, which involves exporting SQL statements from the server, and transmitting the data across slower network connection or Internet to user’s computer. This process slow the exporting process, increase database locking time and thus MySQL unavailability, slow the server and may simply crash the Apache HTTPD server if too many incoming web connections hogging the system’s resources.
The better way to backup and export MySQL database is by doing the task locally on the server, so that the tables’ data can be instantly dumped on the local disk without delay. Thus export speed will be faster and reduce the time MySQL database or table is locked for accessing. This tutorial is the guide on how to backup (export) and restore (import) MySQL database(s) on the database server itself by using the mysqldump and mysql utilities. There are basically two methods to backup MySQL, one is by copying all table files (*.frm, *.MYD, and *.MYI files) or by using mysqlhotcopyutility, but it only works for MyISAM tables. Below tutorial will concentrate on mysqldump which works for both MyISAM and InnoDB tables.
How to Export or Backup or Dump A MySQL Database
To export a MySQL database into a dump file, simply type the following command syntax in the shell. You can use Telnet or SSH to remotely login to the machine if you don’t have access to the physical box.
mysqldump -u username -ppassword database_name > dump.sql
Replace username with a valid MySQL user ID, password with the valid password for the user (IMPORTANT: no space after -p and the password, else mysqldump will prompt you for password yet will treat the password as database name, so the backup will fail) and database_name with the actual name of the database you want to export. Finally, you can put whatever name you like for the output SQL dump file, here been dump.sql.
The while data, tables, structures and database of database_name will be backed up into a SQL text file named dump.sql with the above command.
How to Export A MySQL Database Structures Only
If you no longer need the data inside the database’s tables (unlikely), simply add –no-data switch to export only the tables’ structures. For example, the syntax is:
mysqldump -u username -ppassword –no-data database_name > dump.sql
How to Backup Only Data of a MySQL Database
If you only want the data to be backed up, use –no-create-info option. With this setting, the dump will not re-create the database, tables, fields, and other structures when importing. Use this only if you pretty sure that you have a duplicate databases with same structure, where you only need to refresh the data.
mysqldump -u username -ppassword –no-create-info database_name > dump.sql
How to Dump Several MySQL Databases into Text File
–databases option allows you to specify more than 1 database. Example syntax:
mysqldump -u username -ppassword –databases db_name1 [db_name2 ...] > dump.sql
How to Dump All Databases in MySQL Server
To dump all databases, use the –all-databases option, and no databases’ name need to be specified anymore.
mysqldump -u username -ppassword –all-databases > dump.sql
How to Online Backup InnoDB Tables
Backup the database inevitable cause MySQL server unavailable to applications because when exporting, all tables acquired a global read lock using FLUSH TABLES WITH READ LOCK at the beginning of the dump until finish. So although READ statements can proceed, all INSERT, UPDATE and DELETE statements will have to queue due to locked tables, as if MySQL is down or stalled. If you’re using InnoDB, –single-transaction is the way to minimize this locking time duration to almost non-existent as if performing an online backup. It works by reading the binary log coordinates as soon as the lock has been acquired, and lock is then immediately released.
Syntax:
mysqldump -u username -ppassword –all-databases –single-transaction > dump.sql
How to Restore and Import MySQL Database
You can restore from phpMyAdmin, using Import tab. For faster way, upload the dump file to the MySQL server, and use the following command to import the databases back into the MySQL server.
mysql -u username -ppassword database_name < dump.sql
The import and export of MySQL database not only is important to recover the data when disaster strikes, but also provides an easy way to migrate or move to another server, such as when switching web hosting providers. However, do note that one common problem – character set encoding. Newer release of mysqldump uses UTF8 as its default charset if nothing is specified, while older versions (older than 4.1 typically) use Latin1 as default characterset. If you database charset is Latin1 and dump in UTF8 collation, the data may ends up become simply rubbish, garbled, or unreadable (frequently happen with WordPress blog). If this case, use –default-character-set=charset_nameoption to specify the character set or convert the database to UTF8.

How do I import data stored in a .sql file (created by mysqldump command) under UNIX or Linux operating systems?

The syntax is as follows to import the data created by mysqldump command:
 
mysql -u {DB-USER-NAME} -p {DB-NAME} < {db.file.sql}
mysql -u {DB-USER-NAME} -h {MySQL-SERVER-HOST-NAME} -p {DB-NAME} < {db.file.sql}
 
In this example import a file called sales.sql for salesdb1 user and sales db, enter:
$ mysql -u sales -p salesdb1 < sales.sql
If database salesdb1 does not exists, first create it and than import it as follows:
$ mysql -u root -p -e 'create database salesdb1'
$ mysql -u sales -p salesdb1 < sales.sql



Mount a pendrive !!

Assume the pendrive is /dev/sda1 and you want to mount it at /media/pendrive, then you should be able to mount it as a priveleged user with
Code:
mount /dev/sda1 /media/pendrive
There is a wrapper called pmount that, IIRC, the Debian folks created to allow non-priveleged users to mount things such as pendrives. This might be something you want to install for mom. If you use pmount, the command would be
Code:
pmount /dev/sda1 /media/pendrive
Additionally, you might want to create and alias so mom doesn't need to type so much
Code:
alias pen='mount /dev/sda1 /media/pendrive'
Then mom just executes the command pen to mount the pendrive.

SCP copy files from Mac to Linux


scp command line to securely copy files over ssh, between Linux, Mac or Windows



Written by Guillermo Garron .
Date: 2012-04-25 16:59:35 -0400

SCP Introduction

scp stands for secure cp (copy), which means you can copy files across ssh connection. That connection will be securely encrypted, it is a very secure way to copy files between computers
You can use scp to copy files from or to a remote server. You can also copy files from one remote server to another remote server, without passing traffic through your PC.
You can use scp on Linux, Mac and Windows (using WinSCP).

SCP Usage

scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]
from-host
Is the name or IP of the host where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command
user
Is the user which have the right to access the file and directory, that is supposed to be copied in the case of the from-host, and the user who has the rights to write in the to-host
source-file
Is the file or files that are going to be copied to the destination host, it can be a directory but in that case you need to specify the -r option to copy the contents of the directory
destination-file
Is the name that the copied file is going to take in the to-host, if none is given all copied files are going to keep its names

SCP Options

-p
Preserves the modification and access times, as well as the permissions of the source-file in the destination-file
-q
Do not display the progress bar
-r
Recursive, so it copies the contents of the source-file (directory in this case) recursively
-v
Displays debugging messages

SCP Examples

scp *.txt user@remote.server.com:/home/user/
That is going to copy all files with .txt extension to the folder /home/user in the remote.server.com host
scp -r miguel@10.1.2.2:/home/miguel/ miguel@10.1.2.3:/home/miguel/
That is going to recursively copy all files from Miguel’s home directory on 10.1.2.2 to his home folder in 10.1.2.3 host.
As have been told before, scp copies files between computers using ssh, and there are three types of usage:
Copy files from a local computer to a remote computer
scp somefile username@server:/home/username/
Copy files from a remote server to your local computer
scp username@server:/home/username/file_name /home/local-username/file-name
Copy files from a remote server to another remote computer
This is really interesting and very useful, as the files copied from one server to the other, are not going to pass through your computer. The traffic is going to pass from one server to the other directly.
scp user_name1@server1:/home/user_name1/file_name user_name2@server2:/home/user_name2/

SCP Tricks

Bandwidth limit
You may limit the bandwidth used by scp command
scp -l limit username@server:/home/uername/* .
Where limit is specified in Kbit/s.
Increase scp speed
scp uses AES-128 to encrypt data, this is very secure, but also a litle bit slow. If you need more speed and still have security, you can use Blowfish or RC4.
To increase scp speed change chipher from the default AES-128 to Blowfish
scp -c blowfish user@server:/home/user/file .
Or use RC4 which seems to be the fastest
scp -c arcfour user@server:/home/user/file .
This last one is not very secure, and it may not be used if security is really an issue for you. You can also increase security while decreasing speed. Everything has its cost.
scp -c 3des user@server:/home/user/file .
That is maybe the slowest, but also maybe the more secure one (I may be wrong, I’m not an expert in encryption).

Final notes

It is very important to consider that scp encrypts the data before sending it over the internet. So, if you can use it over ftp or rcp, you better use it.
Finally, as I said before, you can use WinSCP to copy to and from Windows to Linux or Mac. For the Mac OS Xscp is supported by default, just like with Linux.

Source: http://www.garron.me/linux/scp-linux-mac-command-windows-copy-files-over-ssh.html

jueves, 25 de abril de 2013

Open as Administrator in Ubuntu


Open As Administrator in Ubuntu 12.04 & 12.10


This tutorial is going to show you how to add ‘Open As Administrator’ in Nautilus’ context menu in Ubuntu 12.04 & 12.10. This will allow you to edit or modify files or folders with root or administrator permission.
For Ubuntu 10.10 and 10.04, you can just install nautilus-gksu package from Ubuntu Software Center, then you get the ‘Open As Administrator’ option at next login. For Ubuntu 11.04 and 11.10, install nautilus-gksu and copy and paste libnautilus-gksu.so file from /usr/lib/nautilus/extensions-2.0/ to /usr/lib/nautilus/extensions-3.0/
Method 1: (not working for Ubuntu 12.10)
nautilus-gksu package has been dropped in Ubuntu 12.04 and 12.10 since “gksu” 2.0.2-6ubuntu1 release, so you can not install it easily. Here I’m going to use nautilus script to add Open As Administrator functionality.
First download the libnautilus-gksu.so file:
Click to Download libnautilus-gksu.so
Then, run gksudo nautilus in terminal (Ctrl+Alt+T) to open file browser with root privilege, copy and paste this file to/usr/lib/nautilus/extensions-3.0/. Or do it with this command:
sudo cp ~/Downloads/libnautilus-gksu.so /usr/lib/nautilus/extensions-3.0/
Log out and back in, or run this command to take effect:
nautilus -q
Enjoy!