Skip to content

Instantly share code, notes, and snippets.

View erikhansen's full-sized avatar

Erik Hansen erikhansen

View GitHub Profile

Opening and closing an SSH tunnel in a shell script the smart way

I recently had the following problem:

  • From an unattended shell script (called by Jenkins), run a command-line tool that accesses the MySQL database on another host.
  • That tool doesn't know that the database is on another host, plus the MySQL port on that host is firewalled and not accessible from other machines.

We didn't want to open the MySQL port to the network, but it's possible to SSH from the Jenkins machine to the MySQL machine. So, basically you would do something like

ssh -L 3306:localhost:3306 remotehost

@erikhansen
erikhansen / createUsefulAssetManifest.js
Last active June 25, 2019 14:44 — forked from mdecorte/createUsefulAssetManifest.js
Script to create a useful asset-manifest file for CRA-3 that only contains paths to assets needed on page load
const fs = require('fs');
const path = require('path');
const assetManifestPath = path.join(__dirname, 'build/asset-manifest.json');
let assetManifest = fs.readFileSync(assetManifestPath);
assetManifest = JSON.parse(assetManifest)['files'];
const indexFilePath = path.join(__dirname, 'build/index.html');
const BUILD_PATH = path.join(__dirname, 'build/useful-asset-manifest.json');
// Filter paths that exists in the create-react-app generated `build/asset-manifest.json` and use the path from that file because that can contain the "homepage" prefix from package.json.
# Magento 2 Sandbox Snippet - You can copy and paste into any shell in a Magento root directory
# Extended: https://docs.classyllama.net/disciplines/engineering/magento/sandbox-file-m2
set +H # disable history expansion
PHP_CODE=$(cat <<'PHP_CODE'
<?php
header('Content-type: text/plain');
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
@erikhansen
erikhansen / capistrano_deployment.md
Created March 15, 2019 15:56
Capistrano Deployment From Start to Finish

Prerequisites

  • Have SSH access to the target server(s) using PKA (public key authentication)
  • Either your public key or a deploy key on the target server(s) has been added to the source repository as a deploy key
  • The system you are running deployments from has Ruby 2.2 or later installed (RHEL/CentOS users may reference this gist as a means of meeting this requirement)

Setting Up the Deployment Toolchain

  1. Install bundler (http://bundler.io/):
@erikhansen
erikhansen / rsync_examples.sh
Last active March 1, 2019 19:00 — forked from mttjohnson/rsync_examples.sh
rsync examples (two remotes)
# rsync with sudo
rsync -av \
--rsync-path="sudo -u www-data rsync" \
-e 'ssh -o StrictHostKeyChecking=no'
path_to_local_data/ user_name@example.com:/var/www/
# rsync media between two remotes that can not talk to each other
# this creates a port (50000) on the origin server that establishes a tunnel between the origin and destination through
# your computer, as long as your computer can access both servers you can rsync the files between the two
@erikhansen
erikhansen / load_all_sitemap_urls.sh
Last active May 20, 2024 03:21
Load all urls from a sitemap.xml file
#!/bin/bash
# This script crawls all urls in a /sitemap.xml file and loads them, effectively priming the cache
# Usage: ./warm_cache.sh www.example.com
time wget --quiet https://$1/sitemap.xml --output-document - | \
egrep -o "https?://[^<]+" | \
grep $1 | \
grep -v "jpg" | \
xargs -i -d '\n' curl --output /dev/null --silent --write-out '%{http_code} %{time_total}ms %{url_effective} \n' {}
@erikhansen
erikhansen / brew_vagrant_version.sh
Created February 8, 2019 18:22 — forked from mttjohnson/brew_vagrant_version.sh
Installing older version of vagrant/ansible with brew
# You can find the current list of casks on github, and the one for vagrant here:
# https://github.com/Homebrew/homebrew-cask/blob/master/Casks/vagrant.rb
# From there you may be able to load the history of that file and lookup previous versions of
# the cask which would be related to previous versions of vagrant as indicated in the cask definition file.
# If the github history of the file fails you can search through recent commit history and find a commit prior
# to the version change you are trying to avoid and get the commit hash before the change and use it to locate
# a previous version of the cask file.
# current (master branch) casks:
@erikhansen
erikhansen / robots.txt
Last active April 2, 2020 09:11
Recommended Magento 2 robots.txt
# TODO: Update this with appropriate url
Sitemap: https://www.example.com/sitemap.xml
User-agent: *
# Paths (clean URLs)
Disallow: /index.php/
Disallow: /catalog/product_compare/
# Need to allow these urls paths to be indexed, since M2 commonly includes these links in sitemap.xml
# Disallow: /catalog/category/view/
@erikhansen
erikhansen / daily_errors_report.sh
Last active August 2, 2021 11:41
Magento 2 - Send nightly error email, grouped and sorted by count
#!/bin/bash
# stop on errors
set -e
# turn on debugging if running into issues
#set -x
# This script sends a nightly email, showing errors, grouped and sorted by count.
# Getting these errors emailed can be especially useful for the first few weeks after launching a new Magento site.
# Assumptions:
@erikhansen
erikhansen / sync_prod_to_stage.sh
Last active November 11, 2022 23:31
Magento 2 script to push DB and `pub/media` changes from prod>stage
#!/bin/bash
# stop on errors
set -e
# turn on debugging if you're running into issues
#set -x
# Static variables
ENVIRONMENT=$1
RED='\033[0;31m'
NC='\033[0m' # No Color