From 335e6b1311e00edfecf386be2538b4f3eda06905 Mon Sep 17 00:00:00 2001
From: Simone Margaritelli <evilsocket@gmail.com>
Date: Thu, 3 Oct 2019 15:55:03 +0200
Subject: [PATCH] backups

---
 README.md                                     | 10 ++++-
 scripts/backup.sh                             | 42 +++++++++++++++++++
 sdcard/rootfs/root/pwnagotchi/scripts/main.py |  2 +-
 3 files changed, 52 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 scripts/backup.sh

diff --git a/README.md b/README.md
index c568b0b..095c7eb 100644
--- a/README.md
+++ b/README.md
@@ -116,6 +116,14 @@ usage: ./update_pwnagitchi.sh [OPTIONS]
 
 ```
 
+#### Backup your pwnagotchi
+
+You can use the `scripts/backup.sh` script to backup the important files of your unit.
+
+```shell
+usage: ./scripts/backup.sh HOSTNAME backup.zip
+```
+
 ### UI
 
 The UI is available either via display if installed, or via http://pwnagotchi.local:8080/ if you connect to the unit via `usb0` and set a static address on the network interface (change `pwnagotchi` with the hostname of your unit).
@@ -236,7 +244,7 @@ def on_handshake(agent, filename, access_point, client_station):
 - checkout the `ui.video` section of the `config.yml` - if you don't want to use a display, you can connect to it with the browser and a cable.
 - If you get `[FAILED] Failed to start Remount Root and Kernel File Systems.` while booting pwnagotchi, make sure
 the `PARTUUID`s for `rootfs` and `boot` partitions are the same in `/etc/fstab`. Use `sudo blkid` to find those values when you are using `create_sibling.sh`.
-- You can create a `/root/pwnagotchi.yml` configuration file to override the defaults.
+- You can create a `/root/custom.yml` configuration file to override the defaults.
 
 ## License
 
diff --git a/scripts/backup.sh b/scripts/backup.sh
old mode 100644
new mode 100755
index e69de29..8ebf274
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+
+# name of the ethernet gadget interface on the host
+UNIT_HOSTNAME=${1:-10.0.0.2}
+# output backup zip file
+OUTPUT=${2:-pwnagotchi-backup.zip}
+# temporary folder
+TEMP_BACKUP_FOLDER=/tmp/pwnagotchi_backup
+# what to backup
+FILES_TO_BACKUP=(
+  /root/brain.nn
+  /root/brain.json
+  /root/custom.yaml
+  /root/handshakes
+  /etc/ssh
+  /etc/hostname
+  /etc/hosts
+  /etc/motd
+  /var/log/pwnagotchi.log
+)
+
+ping -c 1 $UNIT_HOSTNAME >/dev/null || {
+  echo "@ unit $UNIT_HOSTNAME can't be reached, make sure it's connected and a static IP assigned to the USB interface."
+  exit 1
+}
+
+echo "@ backing up $UNIT_HOSTNAME to $OUTPUT ..."
+
+rm -rf "$TEMP_BACKUP_FOLDER"
+
+for file in "${FILES_TO_BACKUP[@]}"; do
+  dir=$(dirname $file)
+  echo "  $file -> $TEMP_BACKUP_FOLDER$dir/"
+  mkdir -p "$TEMP_BACKUP_FOLDER/$dir"
+  scp -Cr root@$UNIT_HOSTNAME:$file "$TEMP_BACKUP_FOLDER$dir/"
+done
+
+ZIPFILE="$PWD/$OUTPUT"
+pushd $PWD
+cd "$TEMP_BACKUP_FOLDER"
+zip -r -9 -q "$ZIPFILE" .
+popd
diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/main.py b/sdcard/rootfs/root/pwnagotchi/scripts/main.py
index 3bf19c7..2327955 100755
--- a/sdcard/rootfs/root/pwnagotchi/scripts/main.py
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/main.py
@@ -21,7 +21,7 @@ parser = argparse.ArgumentParser()
 
 parser.add_argument('-C', '--config', action='store', dest='config', default='/root/pwnagotchi/config.yml',
                     help='Main configuration file.')
-parser.add_argument('-U', '--user-config', action='store', dest='user_config', default='/root/pwnagotchi.yml',
+parser.add_argument('-U', '--user-config', action='store', dest='user_config', default='/root/custom.yml',
                     help='If this file exists, configuration will be merged and this will override default values.')
 
 parser.add_argument('--manual', dest="do_manual", action="store_true", default=False, help="Manual mode.")