Wednesday, March 26, 2025

Rsync User Guide

Rsync is a powerful file synchronization tool used to transfer files and directories between local and remote servers. It supports incremental transfers, compression, encryption, and file attribute preservation, making it ideal for backups and data migrations.
✅ 1. Basic Syntax

rsync [options] source destination

Source: A local or remote path.

Destination: Also a local or remote path.

Remote Path Format:

user@host:/path/to/directory

✅ 2. Common Options

Option Description (English)
-a Archive mode (preserve permissions, timestamps).
-v Verbose mode for detailed output.
-z Enable compression during transfer.
-e ssh Use SSH for secure transfers.
--progress Show progress during transfer.
--delete Remove extra files at the destination.
-P Enable progress display and resume interrupted transfers.
-r Recursively copy directories.
--bwlimit=KB Limit bandwidth (in KB/s).
--exclude='pattern' Exclude specific files or directories.

✅ 3. Use Cases
📍 Local Sync

rsync -av /source/path/ /destination/path/

Synchronize contents between two local directories.
📍 Local to Remote

rsync -avz /local/path/ user@remote:/remote/path/

Transfer local files to a remote server.
📍 Remote to Local

rsync -avz user@remote:/remote/path/ /local/path/

Download files from a remote server to the local machine.
📍 Remote to Remote

rsync -avz -e ssh user_A@host_A:/path/to/source/ user_B@host_B:/path/to/destination/

Transfer files between two remote servers using your local machine.
📍 Specify SSH Port

rsync -avz -e "ssh -p 2222" /local/path/ user@remote:/remote/path/

Connect using a custom SSH port.
📍 Resume Interrupted Transfers

rsync -avzP user@remote:/remote/path/ /local/path/

Resume interrupted transfers with progress display.
📍 Exclude Files

rsync -avz --exclude='*.log' --exclude='cache/' /local/path/ user@remote:/remote/path/

Exclude .log files and cache directory.
📍 Delete Extra Files on Destination

rsync -avz --delete /local/path/ user@remote:/remote/path/

Delete files on the destination that are no longer in the source.
📍 Limit Bandwidth

rsync -avz --bwlimit=1000 /local/path/ user@remote:/remote/path/

Limit transfer speed to 1000KB/s.

Sunday, March 16, 2025

How to Use nginx -vt Instead of /sbin/nginx -vt

1️⃣ Method 1: Add /sbin to $PATH (Temporary)
🔹 For the current session only

export PATH=$PATH:/sbin

✅ Now you can run:

nginx -vt

⚠️ This change will be lost after closing the terminal.
2️⃣ Method 2: Permanently Add /sbin to $PATH
🔹 For all future sessions

Modify ~/.bashrc or ~/.profile (Debian typically uses ~/.profile):

echo 'export PATH=$PATH:/sbin' >> ~/.profile

Then apply the changes:

source ~/.profile

✅ Now nginx -vt will work every time you open a terminal.

📌 This applies to the current user only. To apply it system-wide, modify /etc/profile.
3️⃣ Method 3: Create a Symbolic Link (Recommended)
🔹 The simplest and most effective method

sudo ln -s /sbin/nginx /usr/local/bin/nginx

✅ Now you can run nginx -vt from anywhere:

nginx -vt

📌 Summary
Nginx Path Summary

📌 Summary

Method Persistent? Use Case Notes
Method 1: export PATH=$PATH:/sbin ❌ No (Only for current session) Temporary fix
Method 2: Edit ~/.profile ✅ Yes (For current user) User-specific solution
Method 3: ln -s /sbin/nginx /usr/local/bin/nginx ✅ Yes (For all users) Best and simplest solution

If the reboot command is missing, try these solutions:

1. Use systemctl reboot
On Debian 12 or other systemd-based distributions, reboot is essentially an alias for systemctl reboot. If reboot is not found, try:

systemctl reboot

2. Run /sbin/reboot Directly
Sometimes, reboot might not be in your PATH. Try running it directly:

/sbin/reboot

If this works, your PATH might be misconfigured. Check it with:

echo $PATH

If /sbin is missing, temporarily add it:

export PATH=$PATH:/sbin:/usr/sbin

3. Check If reboot Is Missing
If the command is truly missing, reinstall it with:

apt update
apt install systemd-sysv -y

The systemd-sysv package includes reboot, shutdown, and other essential commands.

4. Use shutdown Instead
If reboot is unavailable, try:
shutdown -r now
-r means reboot
now means execute immediately
If none of these solutions work, check if /sbin/reboot exists:

ls -l /sbin/reboot

If it’s missing, verify whether it was removed or replaced using:

which reboot
type reboot

Raspberry Pi auto-optimizes scripts

This script removes unnecessary packages, disables unneeded services, optimizes the kernel, and improves system performance.
Script: optimize.sh

#!/bin/bash
set -e

echo "🔧 Removing unnecessary packages..."
apt remove --purge -y snapd lxd lxc* cloud-init cloud-guest-utils 
    ubuntu-advantage-tools landscape-common unattended-upgrades 
    multipath-tools apport
apt autoremove -y && apt clean

echo "🛑 Disabling unnecessary systemd services..."
systemctl disable --now systemd-resolved ufw apt-daily{,-upgrade}.{timer,service} systemd-journald.service
rm -f /etc/resolv.conf && echo "nameserver 8.8.8.8" > /etc/resolv.conf

echo "📌 Disabling swap..."
swapoff -a && sed -i '/swap/d' /etc/fstab

echo "🚀 Optimizing kernel parameters..."
cat <<EOF > /etc/sysctl.d/99-optimization.conf
fs.file-max = 2097152
vm.swappiness = 10
vm.vfs_cache_pressure = 50
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = bbr
EOF
sysctl --system

echo "✅ Optimization completed! Please reboot the system."

How to Use
Save the script as optimize.sh, then execute the following commands:

chmod +x optimize.sh
./optimize.sh

After running the script, reboot the system to apply all changes. 🚀