Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Backup bash script to back up data, letsencrypt, and export database with Docker (Raspberry Pi) #3500

msawyer91 started this conversation in Show and tell
Discussion options

I've been wanting to perform a full backup of NPM on my Raspberry Pi, and after googling and finding what needs to be backed up, I thought I'd whip up a little bash script to make it easier. If you have a Raspberry Pi, you know it uses a micro SD card, and these can wear out. I'd hate to lose my NPM implementation and have to rebuild from scratch.

Because files in the "data" and "letsencrypt" folders may have different perms, you should run the provided script with either sudo or set it to run under the root crontab (by running "sudo crontab -e").

When looking at the script I'm providing, please keep in mind these assumptions, and you'll have to tweak path/folder names to suit your configuration. With minor tweaks it should work in Ubuntu, Debian, etc.

  • The user on my Raspberry Pi is "pi" with a home path of /home/pi
  • The nginx folder, including the location of docker-compose.yml, is in "/home/pi/nginx"
  • The "data" and "letsencrypt" folders reside in "/home/pi/nginx"
  • Inside the nginx folder, you'll need to create a folder named "backups"; the backup will fail if it doesn't exist
  • I'm using MariaDB and have a database export command that works for MySQL/MariaDB
  • At the end of the process I move the tarball to /home/pi/Documents/TethlaSync which is a location where I store other backups; I use "rclone" to sync files to my OneDrive so they are saved off of the Pi

How the backup works:

  1. Creates a temporary backup directory based on the current date/time in /home/pi/nginx/backups
  2. Copies all the "data" contents, recursively, to the temporary backup directory
  3. Copies all the "letsencrypt" contents, recursively, to the temporary backup directory
  4. Exports the database (the command provided should work for both MySQL and MariaDB)
  5. Creates a tarball of the temporary backup folder
  6. Removes the temporary backup folder
  7. Moves the tarball to another folder where rclone syncs it to my OneDrive for storage off of the Raspberry Pi

I named the below file backupNginxProxyManager.sh and saved it in /home/pi/scripts. Be sure to run:

chmod 755 /home/pi/scripts/backupNginxProxyManager.sh

That allows the file to be run by bash.

Next, run "sudo crontab -e" -- this edits the root crontab and ensures your script has permissions to access all of the files. Add this line to the bottom of the crontab.

30 23 * * * /home/pi/scripts/backupNginxProxyManager.sh >> /home/pi/crontab.log 2>&1

The above will run the script at 23:30 (11:30pm) daily, generating a daily backup of your NPM implementation. It also creates or appends to a logfile so you can troubleshoot any errors.

backupNginxProxyManager.sh

#!/bin/sh -e
# Back Up Nginx Proxy Manager (NPM)

trap sendError 1 2 3 6 14 15

sendError()
{
  # Do whatever error handling you want, like send a Pushover notification
}

# Build file and path name
now=$(date +"%m%d%Y_%H%M%S")
rootdir="/home/pi/nginx"
backupdir="${rootdir}/backups/nginx_${now}_backup.d"
datadir="${backupdir}/data"
encryptdir="${backupdir}/letsencrypt"
tarball="${rootdir}/backups/nginx_${now}_backup.tar.gz"
onedrive="/home/pi/Documents/TethlaSync"
# The onedrive folder is synced to my OneDrive via "rclone" - use a suitable location that is either transferred off the same SD card or to a different locally attached storage device

# Copy the files

echo "Creating backup directory"
mkdir $backupdir
echo "Backing up data directory"
cp -rp $rootdir/data $datadir
echo "Backing up letsencrypt directory"
cp -rp $rootdir/letsencrypt $encryptdir
echo "Exporting the database"
docker exec nginx-db-1 mysqldump --user=MYSQL_USER --password=MYSQL_PASSWORD MYSQL_DATABASE -h 127.0.0.1 > $backupdir/npm-export.sql
echo "Creating tarball"
tar -czvf $tarball $backupdir
echo "Cleaning up copied files"
rm -r $backupdir
echo "Moving tarball to OneDrive"
mv $tarball $onedrive

Any feedback, suggestions, etc. are appreciated!

You must be logged in to vote

Replies: 2 comments

Comment options

Found this while looking for something else related to NPM. Will definitely be implementing this once I have it running. Thanks!

You must be logged in to vote
0 replies
Comment options

What's the difference to save the data and letsencrypt directories mounted as volume?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.