Utoljára aktív 1787969768

Automatically sync .bashrc for all users for Administrator.

Revízió f62ec2e3f6d0c39e2ce025367b55af0c3d8fd6a1

sync-bashrc-all.sh Eredeti
1#!/bin/bash
2#
3# sync-bashrc-all.sh
4#
5# Skrip all-in-one:
6# 1. Cek /etc/skel/.bashrc vs versi remote (gist) -> update kalau beda
7# 2. Cek /etc/shell-prompt.sh vs versi remote (gist) -> download/update kalau
8# belum ada atau beda
9# 3. Terapkan /etc/skel/.bashrc ke semua user existing di /home yang
10# .bashrc-nya belum sesuai
11#
12# Jalankan sebagai root: sudo bash sync-bashrc-all.sh
13
14set -euo pipefail
15
16BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc"
17PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh"
18
19SKEL_BASHRC="/etc/skel/.bashrc"
20PROMPT_FILE="/etc/shell-prompt.sh"
21
22if [ "$(id -u)" -ne 0 ]; then
23 echo "Skrip ini harus dijalankan sebagai root (pakai sudo)." >&2
24 exit 1
25fi
26
27TMP_DIR="$(mktemp -d)"
28trap 'rm -rf "$TMP_DIR"' EXIT
29
30fetch() {
31 # fetch URL DEST_TMPFILE -> berhenti dengan pesan jelas kalau gagal
32 local url="$1" dest="$2"
33 if ! wget -q -O "$dest" "$url"; then
34 echo "Error: gagal download $url (cek koneksi internet)." >&2
35 exit 1
36 fi
37}
38
39echo "== 1. Cek /etc/skel/.bashrc =="
40tmp_bashrc="$TMP_DIR/.bashrc"
41fetch "$BASHRC_URL" "$tmp_bashrc"
42
43if [ ! -f "$SKEL_BASHRC" ] || ! cmp -s "$tmp_bashrc" "$SKEL_BASHRC"; then
44 cp "$tmp_bashrc" "$SKEL_BASHRC"
45 echo " -> $SKEL_BASHRC diperbarui dari gist."
46else
47 echo " -> $SKEL_BASHRC sudah sesuai dengan gist, tidak ada perubahan."
48fi
49
50echo
51echo "== 2. Cek /etc/shell-prompt.sh =="
52tmp_prompt="$TMP_DIR/shell-prompt.sh"
53fetch "$PROMPT_URL" "$tmp_prompt"
54
55if [ ! -f "$PROMPT_FILE" ] || ! cmp -s "$tmp_prompt" "$PROMPT_FILE"; then
56 cp "$tmp_prompt" "$PROMPT_FILE"
57 chmod 644 "$PROMPT_FILE"
58 echo " -> $PROMPT_FILE diperbarui dari gist."
59else
60 echo " -> $PROMPT_FILE sudah sesuai dengan gist, tidak ada perubahan."
61fi
62
63echo
64echo "== 3. Terapkan template ke user existing di /home =="
65for home_dir in /home/*/; do
66 home_dir="${home_dir%/}"
67 user="$(basename "$home_dir")"
68
69 # skip kalau bukan user valid (mis. lost+found, direktori nyasar)
70 if ! id "$user" >/dev/null 2>&1; then
71 continue
72 fi
73
74 target="$home_dir/.bashrc"
75
76 # kalau .bashrc user sudah identik dengan template skel, skip
77 if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then
78 echo "[SKIP] $user - .bashrc sudah sesuai template"
79 continue
80 fi
81
82 # backup .bashrc lama, hanya sekali (tidak menimpa backup yang sudah ada)
83 if [ -f "$target" ] && [ ! -f "$home_dir/.bashrc.original" ]; then
84 cp -a "$target" "$home_dir/.bashrc.original"
85 echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original"
86 fi
87
88 cp "$SKEL_BASHRC" "$target"
89 chown "$user":"$user" "$target"
90 echo "[APPLY] $user - .bashrc diperbarui dari template skel"
91done
92
93echo
94echo "Selesai."
95echo "Catatan: user yang sedang login perlu 'source ~/.bashrc' atau login ulang"
96echo "agar shell aktifnya ikut memakai .bashrc yang baru."