Skip to content

Instantly share code, notes, and snippets.

View serg-kovalev's full-sized avatar
:shipit:
don't ship shit! :)

Sergey Kovalev serg-kovalev

:shipit:
don't ship shit! :)
View GitHub Profile

Sublime Text 2 – Useful Shortcuts (Ubuntu)

General

Ctrl+KB toggle side bar
Ctrl+Shift+P command palette
Ctrl+` python console
Ctrl+N new file

Editing

@serg-kovalev
serg-kovalev / rvm_env_wrapper.sh
Last active August 29, 2015 14:04
RVM wrapper that loads needed environment in CRON tasks
#!/usr/bin/env bash
# usage
# rvm_env_wrapper /path/to/project some_ruby_file.rb
# rvm_env_wrapper /path/to/project some_comand arg1 arg2 arg3 etc
# OR
# rvm_env_wrapper --use ruby_version@gemset_name some_comand_or_file
# Permissions:
# sudo chown user:group rvm_env_wrapper
# sudo chmod 774 rvm_env_wrapper
#
@serg-kovalev
serg-kovalev / check_pid_valid.sh
Last active August 29, 2015 14:04
This shell script checks if process exists and removes pid file if not. I use this script with monit to be sure that process exist before killing it
#!/usr/bin/env bash
# the first argument should be a path to pid file
if [[ -f "$1" ]] ; then
if [ $(ps -p $(cat $1) | wc -l) -gt 1 ] ; then
# process exists - do nothing
#echo "OK"
exit 0
else
#echo "will do rm $1"
rm $1
@serg-kovalev
serg-kovalev / postgres_upgrade_8_4_to_9_1.sh
Created August 1, 2014 08:20
How to upgrade PostgreSQL server from 8.4 to 9.1 or above
sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
# stop apache server if you need
# sudo /etc/init.d/apache2 stop
sudo -i -u postgres
pg_dumpall > dumpall_$(date +"%Y%m%d").pgdump
exit
sudo mv /var/lib/postgresql/dumpall_$(date +"%Y%m%d").pgdump ~/
sudo cp -r /etc/postgresql/8.4 /etc/postgresql/old
sudo apt-get install postgresql-9.1 -y
@serg-kovalev
serg-kovalev / lib_passenger_monitor.rb
Last active August 29, 2015 14:04
Phusion Passenger Monitor library: it allows you to automatically gracefully kill/reload passenger instances, the end user will not see any errors. See another gist that shows how to use it: https://gist.github.com/serg-kovalev/a201e89e69ee5a86ad79
# Finds bloating passengers and try to kill them gracefully.
# @example:
# PassengerMonitor.run
require 'logger'
class PassengerMonitor
# How much memory (MB) single Passenger instance can use
DEFAULT_MEMORY_LIMIT = 450
# Log file name
DEFAULT_LOG_FILE = 'passenger_monitoring.log'
@serg-kovalev
serg-kovalev / script_passenger_monitor.rb
Created August 1, 2014 08:28
Monitor your Phusion passenger instances. This script allows you to automatically gracefully reload passenger instances (if they ate too much memory). See the required library: https://gist.github.com/serg-kovalev/71af1ccd121fba7ef56e
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'passenger_monitor'))
# Set logger to log into Rails project /log directory and start monitoring
PassengerMonitor.run(
:log_file => File.join(File.dirname(__FILE__), '..', 'log', 'passenger_monitor.log'),
:memory_limit => 400, # this is in MB
:wait_time => 10, # this in seconds (timeout before attempts)
:attempts => 5 # number of attempts
)
@serg-kovalev
serg-kovalev / gist:8487870b128b093721e4
Last active August 29, 2015 14:11 — forked from mikegrassotti/gist:2247065
FactoryGirl upgrade from version 2.* to 3.*
# FactoryGirl3ForYouAndMe
# The new syntax: http://robots.thoughtbot.com/post/19412394597/factory-girl-hits-3-0
#
# Where to learn sed?
# http://www.grymoire.com/Unix/Sed.html#uh-6
# http://www.markhneedham.com/blog/2011/01/11/sed-across-multiple-files/
#
# What needs to change?
find . -type f -name "*.rb" -print0 | xargs -0 grep "Factory.create"
find . -type f -name "*.rb" -print0 | xargs -0 grep "Factory.build"
@serg-kovalev
serg-kovalev / postgres_db_backup.sh
Created February 24, 2015 08:13
How to easily backup PostreSQL database. This script automatically deletes old backups
#!/bin/bash
CURRDATE=`date +%Y_%m_%d-%H_%M`
SERVERNAME="localhost"
BAK_FILE=/path/to/backups/$SERVERNAME-production-sql-$CURRDATE.sql.gz # change the PATH here
DB_HOST="localhost"
DB_NAME="DB_NAME_HERE" # Change this
function quit {
ERROR_CODE=$1
ERROR_MSG=$2
# -*- coding: utf-8 -*-
# DBからデータを取り出してYAMLにする。生成したYAMLはtmp/fixturesに保存される
namespace :db do
namespace :fixtures do
desc "Extract database data to tmp/fixtures directory."
task :extract => :environment do
fixtures_dir = "#{Rails.root}/tmp/fixtures/"
skip_tables = ["schema_info", "schema_migrations", "sessions"]
ActiveRecord::Base.establish_connection
FileUtils.mkdir_p(fixtures_dir)
@serg-kovalev
serg-kovalev / backup_via_email.rb
Last active August 29, 2015 14:18
Sending database backup in email using core ruby
require 'net/smtp'
FROM = 'Some name <name@email.me>'
TO = %w(name@email.me)
SUBJ = "DB backup #{Time.now.strftime('%Y-%m-%d')}"
# find the last (in 60 mins) backup
filename = `find /path/to/backups/*.sql.gz -mmin -60`.chomp
exit if filename.nil? || filename.empty?