Scripts

Notes for scripts, automation, cron jobs, Docker Compose, backups, WSL startup, and useful command snippets.

Do not store secrets in this page.

Do not include:

passwords
tokens
private keys
real internal URLs
real production hostnames
real customer names

Script safety rules

Before running a script, check:

1. Do I understand what it does?
2. Does it delete, move, overwrite, or stop anything?
3. Does it use sudo?
4. Does it affect production?
5. Does it have a backup or rollback plan?
6. Did I test it in a safe place first?

Basic Bash script template

#!/bin/bash

set -euo pipefail

echo "Starting script..."

# Commands here

echo "Done."

Meaning:

set -e = exit if a command fails
set -u = error on undefined variables
set -o pipefail = fail if one command in a pipe fails

Bash script with date

#!/bin/bash

set -euo pipefail

DATE=$(date +"%Y-%m-%d_%H-%M-%S")

echo "Current date: $DATE"

Backup script template

#!/bin/bash

set -euo pipefail

DATE=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_DIR="/path/to/backups"
SOURCE_DIR="/path/to/source"
RETENTION_DAYS=14

mkdir -p "$BACKUP_DIR"

tar -czf "$BACKUP_DIR/backup-$DATE.tar.gz" \
  -C "$SOURCE_DIR" \
  .

find "$BACKUP_DIR" -type f -name "backup-*.tar.gz" -mtime +"$RETENTION_DAYS" -delete

echo "Backup completed:"
echo "$BACKUP_DIR/backup-$DATE.tar.gz"

Cron basics

Edit user cron

crontab -e

List user cron

crontab -l

Run every day at 02:00

0 2 * * * /path/to/script.sh >> /path/to/script.log 2>&1

Cron time format

minute hour day-of-month month day-of-week command

Example:

0 2 * * * = every day at 02:00
*/5 * * * * = every 5 minutes
0 * * * * = every hour
0 8 * * 1 = every Monday at 08:00

Docker Compose commands

Start containers

docker compose up -d

Stop containers

docker compose down

Show logs

docker compose logs

Follow logs live:

docker compose logs -f

Show running containers

docker ps

Cleaner view:

docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'

Pull newer images

docker compose pull

Recreate after update

docker compose up -d

Restart one service

docker compose restart SERVICE_NAME

Otter Wiki commands

Go to Otter Wiki folder

cd /mnt/wsl/PHYSICALDRIVE0p2/home/randy/otterwiki

Start Otter Wiki

docker compose up -d

Stop Otter Wiki

docker compose down

Show Otter Wiki logs

docker compose logs -f

Check Otter Wiki container

docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'

Test Otter Wiki locally

curl -I http://localhost:8088

Homeserver startup script

Windows startup script location:

C:\Scripts\start-homeserver.ps1

From WSL path:

/mnt/c/Scripts/start-homeserver.ps1

Run it from WSL SSH with:

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy Bypass -File C:\\Scripts\\start-homeserver.ps1

Check log:

tail -n 120 /mnt/c/Scripts/start-homeserver.log

Current homeserver services

Startup script starts:

Radicale
Immich
Mastodon
Jellyfin
Otter Wiki

Otter Wiki backup script

Script location:

/mnt/wsl/PHYSICALDRIVE0p2/home/randy/scripts/backup-otterwiki.sh

Manual test:

/mnt/wsl/PHYSICALDRIVE0p2/home/randy/scripts/backup-otterwiki.sh

Check backups:

ls -lh /mnt/wsl/PHYSICALDRIVE0p2/home/randy/otterwiki/backups

Useful search commands

Find files by name

find /path -name "filename"

Example:

find /etc -name "*.conf"

Search text inside files

grep -R "search_text" /path

Case-insensitive:

grep -Ri "search_text" /path

Search command history

history | grep "search_text"

Safe script notes

When saving scripts here, remove:

real passwords
real tokens
real internal domains
real production hostnames
real IP addresses
customer names
ticket numbers

Use placeholders:

USERNAME
PASSWORD_HERE
TOKEN_HERE
server01
example.com
/path/to/file

Personal notes

Add useful scripts and snippets here over time.