Shell Scripting : Menambahkan Repositori Debian: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
</syntaxhighlight>isikan script berikut ini :<syntaxhighlight lang="linuxconfig" line="1"> | </syntaxhighlight>isikan script berikut ini :<syntaxhighlight lang="linuxconfig" line="1"> | ||
#!/bin/bash | #!/bin/bash | ||
# Script | # Script dengan validasi setelah hapus | ||
echo "=== SMART REPOSITORY REPLACEMENT ===" | |||
# | # Validasi system | ||
if [ ! -f /etc/apt/sources.list ]; then | |||
echo "❌ Error: sources.list tidak ditemukan!" | |||
exit 1 | |||
fi | |||
# | # Backup dengan timestamp | ||
echo " | timestamp=$(date +%Y%m%d_%H%M%S) | ||
backup_path="/root/sources.list.backup.$timestamp" | |||
cp /etc/apt/sources.list $backup_path | |||
echo "📁 Backup created: $backup_path" | |||
# | # Simpan line count sebelum hapus | ||
old_line_count=$(wc -l < /etc/apt/sources.list) | |||
# | # Hapus isi file | ||
sh -c '> /etc/apt/sources.list' | |||
echo "🗑️ Menghapus $old_line_count lines dari sources.list" | |||
# | # Tambah repository optimized | ||
echo "🔄 Menambah repository optimized..." | |||
tee /etc/apt/sources.list > /dev/null << EOF | |||
# Debian 12 Bookworm - Optimized Repository | |||
# Last update: $(date) | |||
# Debian 12 | |||
deb http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware | deb http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware | ||
deb-src http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware | deb-src http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware | ||
Line 45: | Line 41: | ||
EOF | EOF | ||
# | # Validasi penulisan | ||
apt update & | new_line_count=$(wc -l < /etc/apt/sources.list) | ||
if [ $new_line_count -eq 0 ]; then | |||
echo "❌ Error: Repository gagal ditulis!" | |||
echo "🔄 Restoring backup..." | |||
cp $backup_path /etc/apt/sources.list | |||
exit 1 | |||
fi | |||
# Test repository | |||
echo "🔍 Testing repository configuration..." | |||
apt update > /dev/null 2>&1 | |||
echo "✅ | if [ $? -eq 0 ]; then | ||
echo "✅ Repository berhasil diupdate!" | |||
echo "📊 New repository lines: $new_line_count" | |||
echo "📦 Available packages: $(apt list 2>/dev/null | wc -l)" | |||
else | |||
echo "❌ Error: Repository test failed!" | |||
echo "🔄 Restoring backup..." | |||
sudo cp $backup_path /etc/apt/sources.list | |||
apt update && apt upgrade -y | |||
echo "Backup restored!" | |||
fi | |||
</syntaxhighlight>Simpan script tersebut. | </syntaxhighlight>Simpan script tersebut. | ||
Revision as of 22:22, 7 October 2025
Buat file baru dengan nama tambah_repositori.sh
nano tambah_repositori.sh
isikan script berikut ini :
#!/bin/bash
# Script dengan validasi setelah hapus
echo "=== SMART REPOSITORY REPLACEMENT ==="
# Validasi system
if [ ! -f /etc/apt/sources.list ]; then
echo "❌ Error: sources.list tidak ditemukan!"
exit 1
fi
# Backup dengan timestamp
timestamp=$(date +%Y%m%d_%H%M%S)
backup_path="/root/sources.list.backup.$timestamp"
cp /etc/apt/sources.list $backup_path
echo "📁 Backup created: $backup_path"
# Simpan line count sebelum hapus
old_line_count=$(wc -l < /etc/apt/sources.list)
# Hapus isi file
sh -c '> /etc/apt/sources.list'
echo "🗑️ Menghapus $old_line_count lines dari sources.list"
# Tambah repository optimized
echo "🔄 Menambah repository optimized..."
tee /etc/apt/sources.list > /dev/null << EOF
# Debian 12 Bookworm - Optimized Repository
# Last update: $(date)
deb http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm-updates main contrib non-free non-free-firmware
deb http://deb.debian.org/debian/ bookworm-backports main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm-backports main contrib non-free non-free-firmware
deb http://security.debian.org/debian-security/ bookworm-security main contrib non-free non-free-firmware
deb-src http://security.debian.org/debian-security/ bookworm-security main contrib non-free non-free-firmware
EOF
# Validasi penulisan
new_line_count=$(wc -l < /etc/apt/sources.list)
if [ $new_line_count -eq 0 ]; then
echo "❌ Error: Repository gagal ditulis!"
echo "🔄 Restoring backup..."
cp $backup_path /etc/apt/sources.list
exit 1
fi
# Test repository
echo "🔍 Testing repository configuration..."
apt update > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "✅ Repository berhasil diupdate!"
echo "📊 New repository lines: $new_line_count"
echo "📦 Available packages: $(apt list 2>/dev/null | wc -l)"
else
echo "❌ Error: Repository test failed!"
echo "🔄 Restoring backup..."
sudo cp $backup_path /etc/apt/sources.list
apt update && apt upgrade -y
echo "Backup restored!"
fi
Simpan script tersebut. beri hak akses eksekusi
chmod +x tambah_repositori.sh
jalankan script
./tambah_repositori.sh