Using Tar for Backups

July 23rd, 2010

Update file that already exists in tar archive
[root@james ~]# tar -uvf ./test.tar updatefile
Append file that doesnt not exist to archive.
[root@james ~]# tar -rvf ./test.tar newfilesfile
Extract one file from archive.
[root@james ~]# tar -xvf ./test.tar somefile

 

Uncategorized

Working with ‘Apt’ in Ubuntu

July 10th, 2010

Search for a package

apt-cache search <phrase>

Install a package

apt-get install <package>

Upgrade kernel

apt-get dist-upgrade

Uncategorized

Using the ‘touch’ command

July 7th, 2010

Create a new file using touch and modify its access time.

Set the date to Feb 1st, 2009 @ 730am.

[root@stone backups]# touch testfile2 -t 200902010730 testfile2

Verify settings..
[root@stone backups]# ls -la | grep testfile2
-rw-r–r–  1 root katze          0 Feb  1  2009 testfile2

Uncategorized

UFW Firewall HOWTO

May 25th, 2010

Add rule

root@firefly:/data/music# ufw allow from 10.200.80.132 to any port 3689

Delete rule by line number.

root@firefly:/data/music# ufw delete 1
Deleting:
allow 3689
Proceed with operation (y|n)? y
Rule deleted


View current rules

root@firefly:/data/music# ufw status
Status: active

To                         Action      From
–                         ——      —-
22                         ALLOW       Anywhere
21                         ALLOW       Anywhere
3689                       ALLOW       10.200.80.132

Uncategorized

Python: simple salary calc script

May 21st, 2010

#!/usr/bin/python

def main():
salary1 = input(”Enter Salary #1: “)
salary2 = input(”Enter Salary #2: “)

# SET GLOBAL TO SHARE VARS
global x
global y

if(salary1 < salary2):
x=salary1
y=salary2
else:
y=salary1
x=salary2

def make_percent():
global total

total = 100-(float(x)/float(y)*100)
total=int(round(total,0))
print “Total Difference:”,total,”%”

main()
make_percent()

root@donkey:~/tech/core# python calc_number_diff.py
Enter Salary #1: 75
Enter Salary #2: 76
Total Difference: 1 %

Uncategorized

Working with LVM. Or as i like to call, super raid0!

March 16th, 2010

Install packages

apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs

Create partitions

fdisk /dev/sda

-create new partition (’n')
-change system partition type to lvm (’t')
-enter ‘8e’ for hex partition type
-write partition table to disk (’w')

Create physical volume

root@backupserv:~# pvcreate /dev/sdb1 /dev/sda2 /dev/sdc1
Can’t open /dev/sdb1 exclusively.  Mounted filesystem?
Wiping software RAID md superblock on /dev/sda2
Physical volume “/dev/sda2″ successfully created
Wiping software RAID md superblock on /dev/sdc1
Physical volume “/dev/sdc1″ successfully created

root@backupserv:~# pvdisplay
“/dev/sda2″ is a new physical volume of “903.57 GB”
— NEW Physical volume —
PV Name               /dev/sda2
VG Name
PV Size               903.57 GB
Allocatable           NO
PE Size (KByte)       0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               zsP1EO-SlwB-nryo-gEP7-fInc-hJc4-ytVpLX

“/dev/sdc1″ is a new physical volume of “931.51 GB”
— NEW Physical volume —
PV Name               /dev/sdc1
VG Name
PV Size               931.51 GB
Allocatable           NO
PE Size (KByte)       0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               V3RZ2b-FBsM-HoI6-aQED-ouu9-LpR1-m02VLO

Create a Volume Group
root@backupserv:~# vgcreate data /dev/sdc1 /dev/sda2
Volume group “data” successfully created

root@backupserv:~# vgdisplay
— Volume group —
VG Name               data
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                2
Act PV                2
VG Size               1.79 TB
PE Size               4.00 MB
Total PE              469780
Alloc PE / Size       0 / 0
Free  PE / Size       469780 / 1.79 TB
VG UUID               XJVXrA-m5xO-GARp-6WT0-JFlX-7ARj-jxJChV

root@backupserv:~# vgscan
Reading all physical volumes.  This may take a while…
Found volume group “data” using metadata type lvm2fdisk /dev/sda

Create Logical Volume

root@backupserv:~# lvcreate –name share1 –size 100G data
Logical volume “share1″ created
root@backupserv:~# lvcreate –name share2 –size 500G data
Logical volume “share2″ created

*run lvscan for new logical volume scan.

Format partition

root@backupserv:~# mkfs.ext3 /dev/data/share1
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: 192/800

Mount device

root@backupserv:~# mkdir /mnt/share2
root@backupserv:~# mount /dev/data/share2 /mnt/share2
/dev/mapper/data-share2
516061624    202652 489644572   1% /mnt/share2

Deleting an Volume Group

root@backupserv:~# vgremove data
Do you really want to remove volume group “data” containing 2 logical volumes? [y/n]: yes
Can’t remove open logical volume “share1″
root@backupserv:~# umount /mnt/share*
root@backupserv:~# vgremove data
Do you really want to remove volume group “data” containing 2 logical volumes? [y/n]: yes
Do you really want to remove active logical volume “share1″? [y/n]: yes
Logical volume “share1″ successfully removed
Do you really want to remove active logical volume “share2″? [y/n]: yes
Logical volume “share2″ successfully removed
Volume group “data” successfully removed

Delete Physical Volume

root@backupserv:~# pvremove /dev/sdc1 /dev/sdb1
Labels on physical volume “/dev/sdc1″ successfully wiped
Physical Volume /dev/sdb1 not found

ref: http://www.howtoforge.com/linux_lvm_p2

Adding Additional Disk to LVM

create fdisk LVM partition first….

root@backupserv:~# pvcreate /dev/sdb1
Physical volume “/dev/sdb1″ successfully created

root@backupserv:~# vgextend vol1 /dev/sdb1
Volume group “vol1″ successfully extended
root@backupserv:~# vgdisplay vol1
— Volume group —
VG Name               vol1
System ID
Format                lvm2
Metadata Areas        3
Metadata Sequence No  5
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                3
Act PV                3
VG Size               2.70 TB
PE Size               4.00 MB
Total PE              708246
Alloc PE / Size       469238 / 1.79 TB
Free  PE / Size       239008 / 933.62 GB
VG UUID               RS2K3i-SOJ8-a230-fcQL-8PX0-Bpuj-qSUYKz

root@backupserv:~# lvextend -L +930G /dev/vol1/data1 /dev/sdb1
Extending logical volume data1 to 2.70 TB
Logical volume data1 successfully resized

root@backupserv:~# resize2fs /dev/vol1/data1
resize2fs 1.41.9 (22-Aug-2009)
Filesystem at /dev/vol1/data1 is mounted on /mnt/data; on-line resizing required
old desc_blocks = 115, new_desc_blocks = 173
….

*watch more space become available with ‘watch df -h’

Linux, Raid, Uncategorized

Find: xargs -vs- exec

March 16th, 2010

‘-exec’ using trailing ‘\’

[root@monk ~]# tar -cvf test.tar install.log
install.log
[root@monk ~]# time find /tmp/conf-2-xp -name *.xad -exec tar –append –file=/root/test.tar {} +;
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/amd64: Permission denied
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/i386: Permission denied
find: /tmp/conf-2-xp/System Volume Information: Permission denied
tar: Removing leading `/’ from member names

real    0m15.239s
user    0m0.464s
sys     0m2.628s
[root@monk ~]# du -h test.tar
310M    test.tar
[root@monk ~]# rm -f test.tar

*delete fiel and recreate since we are appending and comparing file size after each run.

‘-exec’ using trailing ‘+’

[root@monk ~]# tar -cvf test.tar install.log
install.log
[root@monk ~]# time find /tmp/conf-2-xp -name *.xad -exec tar –append –file=/root/test.tar {} \;
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/amd64: Permission denied
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/i386: Permission denied

real    0m39.015s
user    0m2.739s
sys     0m21.893s
[root@monk ~]# du -h test.tar
310M    test.tar

Using xargs.

*use -print0 option to prevent whitespace issue. ‘xargs -0′ passes first argument.

[root@monk ~]# rm test.tar
rm: remove regular file `test.tar’? yes
[root@monk ~]# tar -cvf test.tar install.log
install.log
[root@monk ~]# time find /tmp/conf-2-xp -name ‘*.xad’ -print0 | xargs -0 tar –append –file=/root/test.tar;
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/amd64: Permission denied
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/i386: Permission denied
find: /tmp/conf-2-xp/System Volume Information: Permission denied
tar: Removing leading `/’ from member names

real    0m15.073s
user    0m0.446s
sys     0m2.774s
[root@monk ~]# du -h test.tar
310M    test.tar

Result: Using ‘+’ is two times faster than using ‘\’. -exec and xargs are about the same speed, when using ‘+’ for -exec.

Uncategorized

Mysql Query Caching

February 9th, 2010

Enabling Caching

*verify caching is on. if need to update, then restart.

jesterj@jesterj-laptop:/etc/mysql$ grep "query" /etc/mysql/my.cnf
query_cache-type = 1 #cache everything
query_cache_limit = 1M
query_cache_size = 16M

*Note that variables in the my.cnf file are reflecteed in the below variables.

mysql> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
6 rows in set (0.00 sec)

View the query cache

mysql> SHOW STATUS LIKE '%qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16612448 |
| Qcache_hits | 1 |
| Qcache_inserts | 2 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 110 |
| Qcache_queries_in_cache | 2 |
| Qcache_total_blocks | 6 |
+-------------------------+----------+
8 rows in set (0.00 sec)

Now run test queries for a database so we can test query cache..

mysql> select * from test.data order by name ASC
mysql> select * from test.data order by name DESC

Notice as you run more and more queries the available free memory descreases and the queries in cache number increases

mysql> SHOW STATUS LIKE '%qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16534728 |
| Qcache_hits | 5 |
| Qcache_inserts | 3 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 110 |
| Qcache_queries_in_cache | 3 |
| Qcache_total_blocks | 8 |
+-------------------------+----------+
8 rows in set (0.00 sec)

Demand Query Caching

You can also specify query cache on demand so only queries that you specify
are cached.

Change query cache type to 2.

[mysql]
….
query-cache-type = 2
….

Restart mysql.

sudo /etc/init.d/mysql restart

Now log back into mysql and verify that DEMAND caching is set

mysql> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | DEMAND |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
6 rows in set (0.00 sec)

Now specify SQL_CACHE when you run a statemnt.

mysql> select SQL_CACHE * from test.data order by name ASC

Note that you can also use SQL_NO_CACHE when query-cache-type=1 to turn off caching for a particular sql stmt.

mysql

Mysql Store Proceedures - The Basics

February 9th, 2010

Create the procedure.

mysql> CREATE PROCEDURE donkey() SELECT * FROM data WHERE name LIKE '%64%';
Query OK, 0 rows affected (0.00 sec)

Execute the procedure.


mysql> CALL donkey();
+-----+------------+
| id | name |
+-----+------------+
| 31 | 1265397164 |
| 99 | 1265402640 |
| 100 | 1265402641 |
| 101 | 1265402642 |
| 102 | 1265402643 |
| 103 | 1265402644 |
| 104 | 1265402645 |
| 105 | 1265402646 |
| 106 | 1265402647 |
| 107 | 1265402648 |
| 108 | 1265402649 |
| 122 | 1265402664 |
| 219 | 1265402764 |
| 316 | 1265402864 |
| 479 | 1265404564 |
| 553 | 1265404640 |
| 554 | 1265404641 |
| 555 | 1265404642 |
| 556 | 1265404643 |
+-----+------------+
19 rows in set (0.00 sec)

Display the query.

mysql> SHOW CREATE PROCEDURE donkey2\G;
*************************** 1. row ***************************
Procedure: donkey2
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `donkey2`()
SELECT * FROM data WHERE name LIKE '%3%'
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
ERROR:
No query specified

Delete the procedure.

mysql> DROP PROCEDURE donkey;
Query OK, 0 rows affected (0.00 sec)

mysql

Apache2 - Overview of configuration and files (draft)

February 8th, 2010

The following is an overview of how to edit standard files, the directory structure and how to enable site features in apache2. Let’s first dig into the folders that comprise apache2.

Files

$tree -d /etc/apache2 #only show dirs
/etc/apache2/
|– conf.d
|– mods-available
|– mods-enabled
|– sites-available
`– sites-enabled

Let’s define the functions of each.

conf.d - configuration files for modules, etc.
mods-available - list of available modules for apache
mods-enabled - list of modules that are loaded into apache config
site-available - configuration files for virtual hosts
sites-enabled - ?

Files also listed in /etc/apache2…

|– apache2.conf
|– envvars
|– httpd.conf
|– magic
`– ports.conf

Enabling and Disabling Modules

First, check the available modules on your system.

/etc/apache2/mods-available/
|– actions.conf
|– actions.load
|– alias.conf
|– alias.load
|– asis.load
|– auth_basic.load
|– auth_digest.load
|– authn_alias.load
|– authn_anon.load
|– authn_dbd.load
…..

To view all mods enabled view the mods-enabled dir. Note that these are sym linked to the mods-avialable directory.

/etc/apache2/mods-enabled
|– alias.conf -> ../mods-available/alias.conf
|– alias.load -> ../mods-available/alias.load
|– auth_basic.load -> ../mods-available/auth_basic.load
|– authn_file.load -> ../mods-available/authn_file.load
|– authz_default.load -> ../mods-available/authz_default.load
|– authz_groupfile.load -> ../mods-available/authz_groupfile.load

Let’s say we want to enable ‘mod-security’. The best way to do this on ubuntu/debian is to use ‘a2enmod’ utility, which stands for apache2 enable module.

jesterj@jesterj-laptop:/etc/apache2$ sudo a2enmod ssl
Enabling module ssl.
See /usr/share/doc/apache2.2-common/README.Debian.gz on how to configure SSL and create self-signed certificates.
Run '/etc/init.d/apache2 restart' to activate new configuration!

Note: You can also just type ‘a2enmod’ to list all available modules.

Now restart apache.

jesterj@jesterj-laptop:/etc/apache2$ sudo /etc/init.d/apache2 restart
* Restarting web server apache2 ... waiting [ OK ]

Now, verify the module is available.

jesterj@jesterj-laptop:/etc/apache2$ ls -la /etc/apache2/mods-enabled/ | grep ssl
lrwxrwxrwx 1 root root 26 2010-02-08 19:20 ssl.conf -> ../mods-available/ssl.conf
lrwxrwxrwx 1 root root 26 2010-02-08 19:20 ssl.load -> ../mods-available/ssl.load

To disable a module, do the same thing but use ‘a2dismod’.

Configuring Sites

Apache