Pastebin Bash Scripts und Pygments

Einfaches, privates Pastebin in Bash mit Pygments Syntax highlighting für Datenschutzparanoide.

Voraussetzungen:

nginx Konfiguration:

location / {
  default_type text/html;
}

Code (codepaste.sh):

#!/bin/bash

NC='\e[0m'
RED='\e[1;31m'
GREEN='\e[1;32m'

if [ ! -n "$1" ]
then
  echo -e "${RED}==> ${NC}Usage: $(basename "$0") code.c";
  exit 1;
fi

BASE_URL="http://paste.domain.tld/"
SERVER_DIR="/srv/http/paste.domain.tld/"
SERVER="root@server"

FILENAME=$(basename "$1")
CODE=$(pygmentize -O linenos=1,lineanchors=n,anchorlinenos=1 -f html -g "$1")
RSTRING=$(</dev/urandom tr -dc A-Za-z0-9 | head -c8)

HTML="<!DOCTYPE html>
<html lang=\"en\">
<head>
<title>$FILENAME</title>
<meta charset=\"utf-8\">
<link rel=\"stylesheet\" type=\"text/css\" href=\"/static/paste.css\" />
</head>
<body>
<div class=\"top\">
$FILENAME <div class=\"right\"><a href=\"$RSTRING.plain\">plain</a></div>
</div>
$CODE
</body>
</html>"

PLAIN=$(cat "$1")

PASTEFILE="/tmp/$RSTRING"
PLAINFILE="/tmp/$RSTRING.plain"
echo "$HTML" > "$PASTEFILE"
echo "$PLAIN" > "$PLAINFILE"
scp -q "$PASTEFILE" "$PLAINFILE" $SERVER:$SERVER_DIR
rm "$PASTEFILE" "$PLAINFILE"

echo -e "${GREEN}==> ${NC}CodePaste URL: $BASE_URL$RSTRING"

exit 0

Datei (filepaste.sh):

#!/bin/bash

NC='\e[0m'
RED='\e[1;31m'
GREEN='\e[1;32m'

if [ ! -n "$1" ]
then
  echo -e "${RED}==> ${NC}Usage: $(basename "$0") file";
  exit 1;
fi

BASE_URL="http://paste.domain.tld/"
SERVER_DIR="/srv/http/paste.domain.tld/"
SERVER="root@server"
FILENAME=$(basename "$1")

TMPFILE="/tmp/$FILENAME"
cp "$1" "$TMPFILE"
chmod 644 "$TMPFILE"
scp -q -r "$TMPFILE" $SERVER:$SERVER_DIR
rm "$TMPFILE"

echo -e "${GREEN}==> ${NC}FilePaste URL: $BASE_URL$FILENAME"

exit 0

Bild (picpaste.sh):

#!/bin/bash

NC='\e[0m'
RED='\e[1;31m'
GREEN='\e[1;32m'

if [ ! -n "$1" ]
then
  echo -e "${RED}==> ${NC}Usage: $(basename "$0") picture.png";
  exit 1;
fi

BASE_URL="http://paste.domain.tld/"
SERVER_DIR="/srv/http/paste.domain.tld/"
SERVER="root@server"

FILENAME=$(basename "$1")
EXTENSION=${FILENAME##*.}

RSTRING=$(</dev/urandom tr -dc A-Za-z0-9 | head -c8)


TMPFILE="/tmp/$RSTRING.$EXTENSION"
cp "$1" "$TMPFILE"
chmod 644 "$TMPFILE"
exiftool -overwrite_original -q -all= "$TMPFILE"
scp -q -r "$TMPFILE" $SERVER:$SERVER_DIR
rm "$TMPFILE"

echo -e "${GREEN}==> ${NC}PicPaste URL: $BASE_URL$RSTRING.$EXTENSION"

exit 0

Pastes löschen, älter als 30 Tage, per cron:

#!/usr/bin/env sh
find /srv/http/paste.domain.tld/ -type f ! -name paste.css -mmin +43200 -delete