# TimeZone Date Time for Debian and Ubuntu.

### 🛠️ System Maintenance &amp; Timezone Setup Script

A production-ready, interactive Bash script for **Debian 10–13** and **Ubuntu 18–24** that automates system updates, timezone configuration, and NTP time synchronization — all wrapped in a clean, colorized terminal interface with full logging and error handling.

---

### ✨ Features

- 🎨 **Beautiful TUI** — Colorized output with spinners, progress bars, and ASCII-bordered sections
- 🔍 **Auto OS Detection** — Detects Debian 10–13 and Ubuntu 18–24 automatically; exits gracefully on unsupported systems
- 🔐 **Smart Privilege Handling** — Works as `root` or detects `sudo` availability automatically
- 📦 **System Update** — Shows full list of upgradable packages (`name → new_version`), runs `upgrade`, `dist-upgrade`, `autoremove`, `autoclean` with live progress
- 🌍 **Timezone Configuration** — Interactive menu with common zones, custom input, and full Debian-specific handling
- 🕐 **NTP Synchronization** — Enables and verifies `systemd-timesyncd` with real-time sync confirmation
- 📝 **Full Logging** — Every action logged to `/var/log/system-maintenance.log` with timestamps
- 💾 **Config Backups** — Automatically backs up `/etc/timezone` and `/etc/localtime` before any changes
- 🔁 **Reboot Prompt** — Detects kernel updates and prompts for reboot if needed
- 🛡️ **Error Handling** — Uses `set -euo pipefail` + `trap ERR` for bulletproof execution

---

### 📋 Requirements

<table id="bkmrk-requirement-notes-ba"><thead><tr><th>Requirement</th><th>Notes</th></tr></thead><tbody><tr><td>Bash 4.0+</td><td>Pre-installed on all target distros</td></tr><tr><td>systemd</td><td>Required (all target distros use it)</td></tr><tr><td>apt</td><td>Debian/Ubuntu package manager</td></tr><tr><td>sudo or root</td><td>For system-level changes</td></tr></tbody></table>

> Missing dependencies (`lsb-release`, `pv`, `systemd-timesyncd`) are **automatically installed** by the script.

---

#### 🚀 Usage

##### 🚀 One-Line Download &amp; Execute :

```bash
apt update && apt install -y curl && clear && \
curl -s https://docs.greenhome.stream/attachments/72 -o /tmp/timezone-setup.sh && \
bash /tmp/timezone-setup.sh
```

OR

##### 1. Download and make executable

```bash
chmod +x system-maintenance.sh
```

##### 2. Run as root or with sudo

```bash
# As root
./system-maintenance.sh

# As a sudo-capable user
sudo ./system-maintenance.sh
```

---

### 🔄 What the Script Does (Step by Step)

#### Step 1 — Privilege Check

Detects if running as `root`. If not, verifies `sudo` access before proceeding.

#### Step 2 — OS Detection

Reads `/etc/os-release` or uses `lsb_release` to identify the distro and version. Exits with a clear error message if the OS is unsupported.

#### Step 3 — Dependency Check

Silently refreshes the apt cache and installs any missing required packages (`lsb-release`, `pv`, `systemd-timesyncd`).

#### Step 4 — System Update *(optional, prompted)*

- Prompts: **"Perform full system update? \[Y/n\]"** (defaults to Yes after 30s)
- Displays a formatted list of all upgradable packages with version arrows (`1.2.0 → 1.3.0`)
- Detects kernel updates and flags for reboot
- Runs with live spinners: `apt upgrade` → `dist-upgrade` → `autoremove` → `autoclean`

#### Step 5 — Timezone Configuration *(optional, prompted)*

- Shows the current active timezone
- Prompts: **"Set timezone? Default: Europe/Athens \[Y/n\]"**
- Presents a menu of 9 common timezones + custom input option
- Validates the chosen timezone against `/usr/share/zoneinfo/`
- Backs up existing config before applying changes
- **On Debian**: additionally updates `/etc/localtime` symlink, writes `/etc/timezone`, and runs `dpkg-reconfigure tzdata`
- **On Ubuntu**: `timedatectl set-timezone` alone is sufficient
- Verifies the applied timezone matches the selection

#### Step 6 — NTP Synchronization *(optional, prompted)*

- Prompts: **"Enable NTP sync with systemd-timesyncd? \[Y/n\]"**
- Runs `timedatectl set-ntp true`
- Enables and starts `systemd-timesyncd` via systemctl
- Waits up to 15 seconds for sync confirmation with a live progress bar
- Displays service status and full `timedatectl status` output

#### Step 7 — Final Summary

Prints a formatted table with:

- Detected OS and version
- Active timezone
- NTP enabled/synchronized state
- Current date and time
- Log file path
- Reboot prompt if a kernel update was applied

---

### 📁 Log File

All actions are recorded in: /var/log/system-maintenance.log If the script lacks write permission to `/var/log/`, it falls back to: /tmp/system-maintenance.log

Log entries include timestamps and status for every operation performed.

---

#### 🌍 Supported Timezones (Built-in Menu)

<table id="bkmrk-%23-timezone-offset-1-"><thead><tr><th>\#</th><th>Timezone</th><th>Offset</th></tr></thead><tbody><tr><td>1</td><td>Europe/Athens *(default)*</td><td>UTC+2/+3</td></tr><tr><td>2</td><td>Europe/London</td><td>UTC+0/+1</td></tr><tr><td>3</td><td>Europe/Berlin</td><td>UTC+1/+2</td></tr><tr><td>4</td><td>Europe/Paris</td><td>UTC+1/+2</td></tr><tr><td>5</td><td>America/New\_York</td><td>UTC-5/-4</td></tr><tr><td>6</td><td>America/Chicago</td><td>UTC-6/-5</td></tr><tr><td>7</td><td>America/Denver</td><td>UTC-7/-6</td></tr><tr><td>8</td><td>America/Los\_Angeles</td><td>UTC-8/-7</td></tr><tr><td>9</td><td>UTC</td><td>UTC+0</td></tr><tr><td>0</td><td>Custom</td><td>Manual input</td></tr></tbody></table>

---

#### 🐧 Compatibility Matrix

<table id="bkmrk-os-versions-tested-d"><thead><tr><th>OS</th><th>Versions</th><th>Tested</th></tr></thead><tbody><tr><td>Debian</td><td>10 (Buster), 11 (Bullseye), 12 (Bookworm), 13 (Trixie)</td><td>✅</td></tr><tr><td>Ubuntu</td><td>18.04, 20.04, 22.04, 24.04</td><td>✅</td></tr><tr><td>Other</td><td>Any non-Debian/Ubuntu</td><td>❌ Exits gracefully</td></tr></tbody></table>

---

### ⚠️ Notes

- The script is **idempotent** — safe to run multiple times without side effects
- Config files are backed up with a timestamp suffix before any modification
- All prompts have a **30-second timeout** that defaults to the safe/recommended option
- `systemd-timesyncd` and `chrony` should **not** run simultaneously; this script only manages `timesyncd`

---