Just one of those days when a line must be drawn to tell apart the men from the boys…
* Elon Musk – Uber entrepreneur
Just one of those days when a line must be drawn to tell apart the men from the boys…
* Elon Musk – Uber entrepreneur
For my own records. Dump:
pg_dump -U superuser -C -o database > database
And then the restore (postgres should be the template db):
psql postgres superuser < database
My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.
Operations with find.
Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.
find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}
Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.
find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;
Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)
find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”);
print “mv “f” “$0}’|sh
Deep
This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:
Find some string (case insensitive) in a js or php files.
deep find 'mystring' '*.php *.js' --case=0
Replace the string
deep replace 'oldstring' 'newstring' '*.php *.js' --case=0
Convert Windows end of lines CR+LF to Unix LF
Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.
Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.
Just finished reading “Mud, Sweat and Tears” by Bear Grylls. Everything comes with a cost, and he payed with lots of mud and sweat. I believe most of the tears were dropped by happiness. I liked it.
It starts really slow and dull, about his roots, from the ancestors (great grandfathers and so) to parents and sister. All the people that were influential through childhood to manhood… i understand that he wants to give a good background about himself, but it’s rather too much of it, as one actions are much more important that one background.
Anyway down the line it starts to pay off, from the SAS training (the failed first attempt to the successful second go), the parachute accident, his faith and recovery, and of course it climaxes with the climb to the summit of Everest.
It’s an inspirational book, lots and lots of details (the summit ascend alone is around 200 pages) and funny stories. It has a simple life pattern: do what you want/love, work hard, give that extra to the ordinary, take risks, and with a bit of luck it all comes together.
For sure it has a zillion faults, but in other hand it can supply all kinds of material goods, both essential and non essential ones. It has been glorified or vilified by people all over the world.
But i wonder, what other system could provide me with a full spec, technology loaded, big 50″ TV screen for just a tiny fraction more than the country minimal wage (delivery included) ?
I’m really not a JS ninja, but as the defacto language of the Web, it’s rather a unique day if i don’t read or write some javascript code and this a pattern that is well worth to know.
First example, a simple var:
function myFunction() {
var myVar = 'test';
setTimeout('alert(myVar)', 1000);
};
myFunction();
fails with myVar is not defined error, because setTimout works on the window object (the top level object for browsers), and there is no myVar registered as a global var (window.myVar is undefined).
You can easily work this out with a closure:
function myFunction() {
var myVar = 'test';
setTimeout(function() { alert(myVar) }, 1000);
};
myFunction();
simply a closure is a function declared inside other function. We have an anonymous function as the first argument of setTimeout inside myFunction, so it’s a closure. The magic of closures is that they keep state (the local variables remain accessible) after the container function has exited.
The curious case of this keyword… take the code:
function myClass () {
this.myVar = 'test';
this.myFunction = function() {
setTimeout(function() {alert(this.myVar)}, 1000);
}
}
myobj = new myClass();
myobj.myFunction();
and boum! undefined pops-up, WTF? after messing around with call, apply and even with i could only make it work assigning this to a local variable:
function myClass () {
var self = this;
this.myVar = 'test';
this.myFunction = function() {
setTimeout(function() {alert(self.myVar)}, 1000);
}
}
myobj = new myClass();
myobj.myFunction();
this of course is a non elegant solution, so please be my guest of posting a better solution to this issue.
Let’s say you want all rows from a table that have a date between next month and the next 3 months, MySQL provide us (since 4.1) a beautiful LAST_DAY function (for the record there is no FIRST_DAY…. at least yet), and the DATE_ADD function (actually there is also a DATE_SUB).
Getting first day of next month:
SELECT DATE_ADD(LAST_DAY(NOW()), INTERVAL 1 DAY);
Getting the last day of the next third calendar month
SELECT LAST_DAY(DATE_ADD(NOW(), INTERVAL 3 MONTH));
So, from here is pretty simple:
SELECT * FROM table WHERE
date_field > DATE_ADD(LAST_DAY(NOW()), INTERVAL 1 DAY) AND
date_field < LAST_DAY(DATE_ADD(NOW(), INTERVAL 3 MONTH));
Bonus query:
get first day of current month
SELECT CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE)
If you are using FreeBSD jails, or are planning to use (just make sure it’s the right solution as there are some caveats), you should look into ezjail, a powerful set of jail management scripts. It can really help save time and do things better: more secure jails, less space usage, easier upgrades, etc…
Install from ports (that was easy and predictable)
cd /usr/ports/sysutils/ezjail/ make install clean
Then, configure ezjail behaviour at /usr/local/etc/ezjail.conf, the most important thing is to setup the ezjail directory, where all the jails will be written to (moving afterwards is a pain in the ass), so consider your partitions and backups plan when setting this up.
Next thing is to build world and make basejail. Just one command:
ezjail-admin update -b
If you have already a built world (ex: from a system upgrade) just need to install it
ezjail-admin update -i
Now, everything is set to to go!
To create a new jail
ezjail-admin create hostname ip-address
With a 100G file-Backed md filesystem
ezjail-admin create -i -s 100G hostname ip-address
To list jails managed by ezjail
ezjail-admin list
List all running jails (in ezjail scope and others) you can fallback to the normal system command
jls
To get a console inside a runing jail
ezjail-admin console hostname
WARNING! You don’t actually get a tty, so some things work strange, for instance: ssh and sftp to remote machines, mysql imports showing password as you type, etc… if something works strange, to be safe better log in through ssh, you have been warned.
Start/stop all jails
/usr/local/etc/rc.d/ezjail.sh start
/usr/local/etc/rc.d/ezjail.sh stop
Star/stop one jail
/usr/local/etc/rc.d/ezjail.sh start hostname
/usr/local/etc/rc.d/ezjail.sh stop hostname
To start automatic at boot just set the ezjail_enable in /etc/rc.conf. It will run all jails in ezjail scope, except if you specify some jail not to boot automatically:
ezjail-admin config -r norun hostname
of course if you change your mind, you can simply revert it
ezjail-admin config -r norun hostname
About the ports, as i am very happy with the current setup, and it seemed a waste of space to have duplicate ports tree in the same machine… first i simply monted null_fs my existing ports tree of the main host into /ezjail_dir/basejail/usr/ports but i couldn’t access it from within the jails… probably double mount_null problem, as the basejail dir is also null mounted, so i add it to the fstab of each jail. Just look and edit edit /etc/fstab.jailname and:
/usr/ports /usr/jails/jaildir/usr/ports nullfs ro 0 0
Also tweak main host and jails /etc/make.conf to avoid any interference with ports building, files and indexes.
WRKDIRPREFIX=/var/ports
DISTDIR=/var/ports/distfiles
PACKAGES=/var/ports/packages
INDEXDIR=/var/ports
Don’t forget to look into /usr/local/etc/ezjail/ directory, where all the configs for each jail live in separate files. I find it much easier to change or setup things like cpusets and multiple ip here than thru ezjail-admin command.
With a multi core CPU, you can to set a jail cpu affinity to use one particular core, just go to the /usr/local/etc/ezjail directory, then find and open your jail configuration file, should be something like jailhostname_com. Edit the line export jail_iedp_pt_cpuset=”" and set the core you want to assign the jail to.
To get the number of cpu/cores just type as root
sysctl hw.model hw.ncpu
At last, if you want to delete a jail simply type (and say goodbye)
ezjail-admin delete -w hostname
If you are using file based disks (md disks) and need to check them just stop the jail (if it is running). Attach the disk, use fsck, and detach it:
mdconfig -a -f file.img
fsck -t UFS /dev/mdxx
mdconfig -d -u xx
(replace the xx by the number outputed in the first command).
There you go, a very simple guide to ezjail jails management, as always feel free to ask any questions or add up some knowledge. Also, try and make yourself comfortable before stepping into production, and remember if all goes terrible bad you are on your own.
Last days, i had to mess around with the excelent blog and publishing software WordPress. I wanted the frontend localized in portuguese (yes… one or other of my projects are not globe wide), so i downloaded the portuguese version, but the administration backoffice also gets translated, and it looks pretty lame in portuguese.
The localization in WordPress is (more or less) pretty straightforward. It’s gettext based, so a translated version is just the same as the usual with a corresponding .mo and .po file in wp-includes/languages/ directory. Then you set the corresponding WPLANG constant in wp_config.php file. But it’s all or nothing, you get both the frontend and the backoffice translated….
But quickly i found myself a solution and i was happy again with an english backoffice. It just needs a tweak in the apache virtualhost configuration to set up an environment variable fot the backoffice that can be tested in wp-config.php file.
The apache config:
</Directory "/usr/home/wordpress/">
AllowOverride None
Order deny,allow
Allow from all
RewriteEngine On
RewriteRule ^wp-login\.php - [E=BO:true]
RewriteRule ^wp-admin/.* - [E=BO:true]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . %{DOCUMENT_ROOT}index.php [L]
</Directory>
So, all the requests to wp-login and wp-admin (the backoffice administration interface) get the BO variable, now just need to test it and apply the WP_LANG only to all other requests in wp-config.php:
if (! (isset($_SERVER['BO']) && $_SERVER['BO']))
define ('WPLANG', 'pt_PT');
There, frontend translated and backoffice in good old english tech words.
PS
I needed to adjust some translated strings, but found the .po file without translated strings (like a .pot file). But no problem you can generate a .po file from the .mo file, just run:
msgunfmt pt_PT.mo > pt_PT.po
I really enjoyed this TED talk, about western food habits, if you didn’t watched yet, please take 20 minutes and watch it now
I’m really not a tofu/seitan/vegetarian kind of guy, but this talk really had an effect on me and since i’m trying to eat less meat and more plants. Not only because of my own health but also for the healh of the entire planet.