DEV Community

Luke Liukonen
Luke Liukonen

Posted on

Automating a Backup Server and Gaming PC with Wake on Lan, PowerShell, Bash Shell Scripts, and a Raspberry Pi

Efficiency has always fascinated me as a software engineer. I'm constantly seeking ways to optimize existing technologies. In 2020, I built a gaming PC equipped with a mid-tier Zen 3 CPU, 16 GB of RAM, and an Nvidia 3060. However, it often took a back seat to my Framework Laptop due to its compatibility with my kitchen workstation setup, and the ease of hot-swapping using USB-C with my work laptop, an M1 Apple MacBook.

With a decent setup in my kitchen area, because of the whole, work from home privilege many of us have seen over the last few years, my gaming PC seldom saw use. When it did, it was usually for video transcoding, light gaming, or overdue Windows updates. Meanwhile, my home lab, running on Ubuntu Server, saw substantial power consumption reduction over the years, thanks to moving from a AMD 5350 to an Intel J1900 CPU/motherboard combo and Raspberry Pi devices.

I knew about Wake on LAN but never explored it until I had two idle PCs: the old home lab and Windows Gaming PC.

Problems to Solve:
Going through the above, I had three problems I wanted to solve:

  1. How to keep my backup Linux PC in sync with my home lab so if my machine ever went down, cutover would be nearly seamless.
  2. How to keep my Windows PC up to date with software updates and apps.
  3. How to automate video transcoding seamlessly.

This led me to utilize the "efficiency cores" of a Raspberry Pi 3 for automation. My goals, driven by cron jobs, were clear:

  1. Every day at 6:30 am, check for videos to encode on my network share. If found, wake up the gaming PC, run the encoding script, and shut down when finished.
  2. Once a week, wake up my backup Linux server, perform a backup, and shut down.
  3. Once a week wake up the gaming PC, execute system updates, and shut down.

Wake on LAN played a crucial role in achieving these goals, offering substantial energy savings. (just the AMD server alone, Im looking at just over $42 a year by not running it 24/7)

Script Description:

This script serves as the core component of our automation process. Its primary objective is to initiate the wake-up and execution sequence for a target machine, such as a Windows PC, using a Raspberry Pi. Here's how it works:

Setting the Target: We begin by specifying the target machine's IP address, which allows us to precisely identify the system we want to awaken and interact with.

Checking the Samba Share: Our script first verifies the contents of a designated Samba share folder. This step is critical for determining whether there are any pending tasks or files to process. If the folder contains subfolders or files, it signals the need for the target machine's activation.

Waking Up the Target: To wake up the target machine, we employ the Wake-on-LAN (WoL) mechanism. This involves sending a "magic packet" to the machine, causing it to power up from a sleep or powered-off state. The unique MAC address of the target machine is used to identify it within the local network.

Ensuring Connectivity: Once the target machine is awakened, we establish a connection check loop. We repeatedly ping the machine to confirm its availability. This process continues for a predefined duration, ensuring that we don't proceed until a successful connection is established.

Executing Tasks Remotely: Upon successful connection, we leverage SSH to establish a secure connection to the target machine. From there, we execute a PowerShell script (typically used for tasks like video encoding). You can customize this step by replacing 'mainuser' with the correct username and '~\encodevideo.ps1' with the actual script path you want to run.

Exit and Reporting: If the connection and task execution are successful, we exit the script gracefully. In the event of a failed connection within the designated time frame, the script provides a notification that the connection attempt has failed.

This script streamlines the process of waking up and automating tasks on a target machine remotely. It is particularly useful for scenarios where you need to conserve power by keeping the target machine in a low-power state until specific tasks require execution. To use this script effectively, ensure you've installed the 'wakeonlan' app and 'sambaclient' on your Raspberry Pi.

Feel free to customize the script to suit your specific requirements and automate a wide range of tasks on your target machine.


# Set the target IP address of the machine you want to wake up.

# Define the Samba share you want to check for files.

# List the contents of the Samba share folder for video backups.
# Replace 'videos/backup' with the correct path if needed.
samba_contents=$(smbclient -A smb.conf -c "cd videos/backup; ls" //NAS/HDD | grep D | grep -vF \.| grep -v -e '^$')
echo "Contents of the Samba share folder $samba_share:"
echo "$samba_contents"

# Check if there are subfolders in the Samba share.
if [ -n "$samba_contents" ]; then
  echo "Subfolders exist in the Samba share folder $samba_share."
  echo "No subfolders found in the Samba share folder $samba_share. Exiting."
  # Exit the script, as there's no need to wake up the PC.
   exit 1

# Wake up the target PC using its MAC address.
# Replace 'AA:AA:AA:AA:AA:AA' with the actual MAC address.
/usr/bin/wakeonlan -i 1$target_ip AA:AA:AA:AA:AA:AA

# Set a timeout for checking if the target PC is awake (5 minutes).

while [ $SECONDS -lt $timeout ]; do
    if ping -c 1 -W 1 $target_ip &> /dev/null; then
        echo "Connection to $target_ip successful."
        # Connect to the Windows PC over SSH and run a PowerShell script.
        # Replace 'mainuser' with the correct username and '~\encodevideo.ps1' with the actual script path.
        ssh mainuser@$target_ip "pwsh ~\encodevideo.ps1"
        exit 0
        sleep 20

echo "Connection to $target_ip failed after 5 minutes."
Enter fullscreen mode Exit fullscreen mode

My experience with Wake on LAN has been fascinating. The ability to put PCs to sleep or shut them down, only to wake them when needed, aligns with my vision of efficiency. I'd love to hear others' ideas or projects in this realm. In the back of my mind, I can envision small-scale data centers with empty Kubernetes clusters benefiting from this approach. Has anyone used K8ts or something similar to auto-scale on-demand locally? Share your thoughts and experiences!

Top comments (0)