Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make backup / restore use POSIX shell for portability. #580

Merged
merged 1 commit into from
Nov 11, 2019

Conversation

qbit
Copy link
Contributor

@qbit qbit commented Nov 11, 2019

Signed-off-by: Aaron Bieber aaron@bolddaemon.com

Compatibility / Usability fixes for backup / restore scripts.

Description

This removes the hard dependency on bash and adds getopts flags to the backup / restore scripts.

Removal of bash because there are simple scripts (no need for bash specific features), and not all systems ship with bash by default.

Motivation and Context

Personally I can't remember the order of things like $1, $2, so every time I backup I have to view the scripts.

How Has This Been Tested?

Backups and restores have been done.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s

backup.sh:
  - Add use getopts for a more friendly user interface.
  - Add timeout to ping check.
  - Add unix epoch to backup file names.
  - Backup ssh information (/etc/ssh, $USER/.ssh).

restore.sh:
  - Add use getopts for a more friendly user interface.
  - Add timeout to ping check.
  - If user doesn't specify a backup file to restore, attempt to
    find the latest for the host name passed in.

Signed-off-by: Aaron Bieber <aaron@bolddaemon.com>
@evilsocket evilsocket merged commit 3df35ef into evilsocket:master Nov 11, 2019
@kdomanski
Copy link

The ${FILES_TO_BACKUP[@]} array expansion is not POSIX-compliant and on my linux /bin/sh it causes:

./scripts/backup.sh: 57: Bad substitution

Are you on macOS by any chance? Does it work there?

@qbit
Copy link
Contributor Author

qbit commented Nov 12, 2019

Ah crap. Looks like it's broken on macos for different reasons (ping has no -w).

I am on OpenBSD which expands as expected (as does macos once you get pas the -w issue)

Do you know what shell /bin/sh is on your flavor of linux?

*"expands as expected" - maybe "expands as unexpected" is better here :P

@evilsocket
Copy link
Owner

please @qbit send a fix

@qbit
Copy link
Contributor Author

qbit commented Nov 12, 2019

@kdomanski can you confirm that this diff works for you?

diff --git a/scripts/backup.sh b/scripts/backup.sh
index 3ba9fd8..9c58dbd 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -48,10 +48,10 @@ FILES_TO_BACKUP="/root/brain.nn \
   /home/pi/.bashrc \
   /home/pi/.profile"
 
-ping -w 3 -c 1 "${UNIT_HOSTNAME}" > /dev/null 2>&1 || {
+ping -c 1 "${UNIT_HOSTNAME}" > /dev/null 2>&1 || {
   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 ..."
-ssh "${USERNAME}@${UNIT_HOSTNAME}" "sudo find ${FILES_TO_BACKUP[@]} -print0 | xargs -0 sudo tar cv" | gzip -9 > "$OUTPUT"
+ssh "${USERNAME}@${UNIT_HOSTNAME}" "sudo find ${FILES_TO_BACKUP} -print0 | xargs -0 sudo tar cv" | gzip -9 > "$OUTPUT"
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 6303f88..551094e 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -44,7 +44,7 @@ fi
 # username to use for ssh
 USERNAME=${USERNAME:-pi}
 
-ping -w 3 -c 1 "${UNIT_HOSTNAME}" > /dev/null 2>&1 || {
+ping -c 1 "${UNIT_HOSTNAME}" > /dev/null 2>&1 || {
   echo "@ unit ${UNIT_HOSTNAME} can't be reached, make sure it's connected and a static IP assigned to the USB interface."
   exit 1
 }

@kdomanski
Copy link

I'm on Ubuntu, my sh implementation appears to be dash. The patch above fixes it for me.

dadav pushed a commit to dadav/pwnagotchi that referenced this pull request Apr 1, 2020
Make backup / restore use POSIX shell for portability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants