Skip to content

Instantly share code, notes, and snippets.

View andsens's full-sized avatar

Anders Ingemann andsens

View GitHub Profile
@andsens
andsens / sign_s3.gs
Last active August 9, 2016 18:21
Google spreadsheet function for signing S3 URLs (very handy for CSV cost allocation reports when combined with the ImportData() function)
function sign_s3(access_key, private_key, bucket, object_name, validity, base_url) {
if(!base_url) {
base_url = "http://s3.amazonaws.com";
}
if(!validity) {
validity = 60;
}
expires = Math.floor((new Date()).getTime() / 1000) + validity;
object_name = encodeURIComponent(object_name);
stringToSign = "GET\n\n\n"+expires+"\n/"+bucket+"/"+object_name;
@andsens
andsens / convert_db_encoding.php
Created October 3, 2012 10:32
Converts an entire database to utf-8. Handy when you forgot to set default_encoding etc. in my.cnf
#/usr/bin/php
<?php
$mysqli = new mysqli('hostname', 'username', 'password');
$result = $mysqli->query(
"SELECT `TABLE_SCHEMA`, `TABLE_NAME` FROM `information_schema`.`TABLES`
WHERE
`TABLE_COLLATION` != 'utf8_general_ci'
AND `TABLE_SCHEMA` != 'mysql'
AND `TABLE_SCHEMA` != 'information_schema'");
@andsens
andsens / benchmark-ec2-boottime.sh
Created October 1, 2012 19:02
Starts a single instance and measures the time until SSH connectivity
#!/bin/bash -e
# These need to be set.
#export EC2_HOME="/path/to/ec2-api-tools"
#export AWS_ACCESS_KEY='XXXXXXXXXXXXXXXXXXXX'
#export AWS_SECRET_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
#export PATH="$PATH:${EC2_HOME}/bin"
ami_id='ami-123abc12'
availability_zone='eu-west-1a'
keypair="johndoe@example.com"
@andsens
andsens / dump.sh
Last active October 24, 2023 07:04
Backup all MySQL databases into separate files
#!/bin/sh
## backup each mysql db into a different file, rather than one big file
## as with --all-databases. This will make restores easier.
## To backup a single database simply add the db name as a parameter (or multiple dbs)
## Putting the script in /var/backups/mysql seems sensible... on a debian machine that is
## Create the user and directories
# mkdir -p /var/backups/mysql/databases
# useradd --home-dir /var/backups/mysql --gid backup --no-create-home mysql-backup
## Remember to make the script executable, and unreadable by others
@andsens
andsens / little_bobby_tables.sql
Created July 16, 2012 14:11
Creates ordered statements to drop all tables in mysql db avoiding most foreign key conflicts
SELECT CONCAT('DROP TABLE `', `t`.`TABLE_SCHEMA`, '`.`', `t`.`TABLE_NAME`, '`;')
FROM `information_schema`.`TABLES` t
LEFT JOIN `information_schema`.`REFERENTIAL_CONSTRAINTS` rc
ON `rc`.`CONSTRAINT_SCHEMA` = `t`.`TABLE_SCHEMA`
AND `rc`.`REFERENCED_TABLE_NAME` = `t`.`TABLE_NAME`
WHERE `TABLE_SCHEMA` = 'dbname'
GROUP BY `t`.`TABLE_NAME`
ORDER BY COUNT(`t`.`TABLE_NAME`) ASC;
@andsens
andsens / type.sh
Created July 13, 2012 10:20
Keyboard typing tool for AppleTV AirControl
#!/bin/bash
url='http://apple-tv.anders.local'
menu="$url/remoteAction=1"
menuhold="$url/remoteAction=2"
up="$url/remoteAction=3"
down="$url/remoteAction=4"
select="$url/remoteAction=5"
left="$url/remoteAction=6"
right="$url/remoteAction=7"
playpause="$url/remoteAction=10"
@andsens
andsens / bootstrap_homeshick.sh
Last active December 27, 2023 12:47
Script that can set up an entire user account with homeshick automatically
#!/bin/bash -ex
# Paste this into ssh
# curl -sL https://gist.github.com/andsens/2913223/raw/bootstrap_homeshick.sh | tar -xzO | /bin/bash -ex
# When forking, you can get the URL from the raw (<>) button.
### Set some command variables depending on whether we are root or not ###
# This assumes you use a debian derivate, replace with yum, pacman etc.
aptget='sudo apt-get'
chsh='sudo chsh'