Linux: get simple I/O statistics per process

I had a trouble with a long process running and wish to know how much I/O this process is doing, so I wrote this quick and dirty python 2.x script: import time,sys,datetime def read_stat(pid): f=open("/proc/%s/io" % pid ,"r") for line in f: if line.startswith('rchar'): rchar=line.split(':')[1] continue if line.startswith('wchar'): wchar=line.split(':')[1] continue f.close() return int(rchar),int(wchar) pid=sys.argv[1] r0,w0 = read_stat(pid) while 1: time.sleep(1) r1,w1 = read_stat(pid) print "%s\t\tr=%s\t\tw=%s" % (datetime.datetime.now().time(),r1-r0,w1-w0) r0,w0=r1,w1 You must give the process PID number as input to the script....

August 22, 2014 · Andrea Manzini

how to setup disk redundancy with BTRFS filesystem

Starting with a plain old one-disk configuration… # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 5.8G 590M 5.0G 11% /data thanks to the power of btrfs, let’s add a second hard disk, with mirrored data AND without unmounting/reformatting! :) also note the different size…. # fdisk -l Disk /dev/sda: 6 GiB, 6442450944 bytes, 12582912 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xea97ecdc Device Boot Start End Blocks Id System /dev/sda1 2048 526335 262144 82 Linux swap / Solaris /dev/sda2 * 526336 12582911 6028288 83 Linux Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes … state of the filesystem before the change…...

July 9, 2014 · Andrea Manzini

Compress and encrypt your backups

It’s always recommended to backup your data for safety, but for safety AND security let’s encrypt your backups! to compress and encrypt with ‘mypassword’: tar -Jcf - directory | openssl aes-256-cbc -salt -k mypassword -out backup.tar.xz.aes to decrypt and decompress: openssl aes-256-cbc -d -salt -k mypassword -in backup.tar.xz.aes | tar -xJ -f - Another trick with the tar command is useful for remote backups: tar -zcvfp - /wwwdata | ssh root@remote....

June 11, 2014 · Andrea Manzini

dovecot: cleaning old Spam and Trash messages after some days

This script is useful to delete old messages in “Junk” mail folders (Spam, Trash) automatically after some days. adapted from these notes to work on debian/postfixadmin/dovecot #!/bin/bash # # itera sulle mailbox cancellando messaggi vecchi # per default, nel cestino 30gg e Spam 15 gg # # MySQL details HOST="127.0.0.1"; USER="put_here_your_mysql_user"; PWD="put_here_your_mysql_password"; MYSQL="/usr/bin/mysql"; # dovecot details DOVEADM="/usr/bin/doveadm"; TEMPFILE=$(/bin/mktemp) # Output sql to a file that we want to run echo "use postfixadmin; select username from mailbox" > $TEMPFILE # Run the query and get the results (adjust the path to mysql) results=$($MYSQL -h $HOST -u $USER -p$PWD -N < $TEMPFILE); # Loop through each row for row in $results do echo "Purging $row Trash and Junk mailbox....

March 5, 2014 · Andrea Manzini