Skip to content

Instantly share code, notes, and snippets.

View andsens's full-sized avatar

Anders Ingemann andsens

View GitHub Profile
@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'
@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 / 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 / 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 / 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 / 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 / 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 / debian-init.sh
Last active October 5, 2021 09:38
This is a generic init-script, easily modifiable to suit your needs. It uses quite a lot of lsb init-functions and adheres to the lsb standards.
#!/bin/sh
### BEGIN INIT INFO
# Provides: generic-prog
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Generic Program
# Description: Generic Program is a generic program to do generic things with
### END INIT INFO
@andsens
andsens / chrooted.sh
Created April 28, 2014 12:32
Configure server for chrooted sftp users and create those users
#!/bin/bash
# source this file in /root/.bashrc or /root/.zshrc
# Sets up the server for chrooted users
configure_chrooted() {
grep ^chrooted: /etc/group
if [[ $? == 0 ]]; then
printf "The chrooted group already exists, aborting...\n"
exit 1
@andsens
andsens / jjrender.py
Last active August 29, 2015 14:06
Renders a jinja2 template and outputs the result
#!/usr/bin/env python
# Dependencies: jinja2, pyyaml, docopt
import docopt
usage = """jjrender
Renders a jinja2 template and outputs the result
Pipe the variables in yml format into stdin
Alternatively you can simply paste the tpl after starting jjrender, terminate input with Ctrl+D
Usage: jjrender TEMPLATE