OUTDATED - Here's a method and script for automated flash zip backup!


Recommended Posts

EDIT: 12/16/2020, a new and cleaner script based in part on suggestions in this forum has been outlined my updated post here.

 

After looking and not finding anything, i've figured out a clunky way to automate the flash zip backup process using the user scripts plugin, so i thought i'd share it. It also has an option to remove old backups that i'll explain. There are a few steps involved to set it up, and the script itself is rudimentary, but ultimately it seems to get the job done. i'll lay it out in steps below for anyone interested. i'm still new to scripting and linux in general, so I invite anyone to offer input, if they find problems, refine, polish, make changes, etc. 

 

***Full disclosure, i only have this running on my test server right now, but so far seems to be successful without any issues! for anyone that decides to try, MANUALLY BACKUP YOUR FLASH DRIVE FIRST BEFORE TRYING. if you are at all nervous or unfamiliar, please don't try this - i don't assume any liability here for data loss. Use at your own risk, this is still a work in progress. Here are the steps:

 

1. the unraid flash zip script is located in 

#/usr/local/emhttp/webGui/scripts

open with proper text editor, then simply copy and paste into a new user script (via the user script plugin) and save it as something like "unraid flash to zip".

 

2. (If you're running unraid version 6.9b30, skip this step and go down to 2A) in the user script plugin, create another new script called "Flash Zip Backup" and paste in the following:

#!/bin/bash

#dir = WHATEVER FOLDER PATH YOU WANT TO SAVE TO
dir=/insert/your/path/here

echo 'Remove symlink from emhttp'
find /usr/local/emhttp/ -maxdepth 1 -name '*flash-backup-*.zip' -delete

sleep 5

echo 'Move Flash Zip Backup from Root to Backup Destination'
mv /*-flash-backup-*.zip "$dir"

sleep 5

echo 'Deleting Old Backups'

#ENTER NUMERIC VALUE OF DAYS AFTER "-MTIME +"
find $dir* -mtime +30 -exec rm -rfv {} \;

echo 'All Done'

2A. 

#!/bin/bash

#dir = WHATEVER FOLDER PATH YOU WANT TO SAVE TO
dir=/insert/your/path/here

echo 'Remove symlink from emhttp'
find /usr/local/emhttp/ -maxdepth 1 -name '*flash-backup-*.zip' -delete

sleep 5

echo 'Move Flash Zip Backup from Root to Backup Destination'
mv /mnt/user/system/*-flash-backup-*.zip "$dir"

sleep 5

echo 'Deleting Old Backups'

#ENTER NUMERIC VALUE OF DAYS AFTER "-MTIME +"
find $dir* -mtime +30 -exec rm -rfv {} \;

echo 'All Done'

 

Before saving here, make sure you change the "dir" path to your backup destination. i'd advise something off of the array to a remote share or unassigned disk (via the unassigned device plugin), but it can be anywhere you like.

 

3. This script also removes backup files after a certain number of days. On the second to last line, the "+30" stands for "30 days or more", so the script will remove files in the given directory that are 30 days or older. Adjust this number to anything you like, i.e. 14, 60, etc. (I have this set to run once a week, so 30 works for me). Once you've entered the dir path and the number of days, you can save the script.

 

4. Let's set a cron time for these two scripts to run a little smoother. For the first script, "unraid flash to zip", in the user scripts page i scheduled it for Mondays at 1:00pm, so 0 13 * * mon is the entry for that. For the second script, "Flash Zip Backup", i have it set to run five minutes later, so it's 5 13 * * mon. These times can be adjusted to run whenever you like, but obviously the "unraid flash to zip" script must run first before " Flash Zip Backup". Be sure to hit apply on the bottom left of the user scripts page to save before exiting.

 

 

And that's it! You can manually run the scripts if you want, verify the flash.zip in your backup directory, make sure everything is working. Again, if you have any questions or suggestions, let me know. I know there's more than one way to skin a cat with linux commands, this is just how i landed with it.

 

 

EDIT: It seems @nitewolfgtr was able to combine steps 1 & 2 into a single script here. I haven't tried it yet but it looks good

 

EDIT 2: @sreknob helpfully pointed out on 6.9b30 that backups are now stored in /mnt/user/system/. An alternative Step 2A has been provided for users running this script. Have to point out again, that nothing is definite in this script and great care should be taken when implementing new steps. Thanks so far for everyone contributing!

 

Edited by Cpt. Chaz
outdated, posted link to updated post
  • Like 1
  • Thanks 1
Link to comment

Thank you Cpt. Chaz for this solution. It was very helpful in getting my automated backup cron job set-up.

I did however make a small tweak so that it calls the native unraid zip script directly as part of a backup script within the user script plugin. This way I only have to set-up 1 cron job. 🙂

 

#!/bin/bash
#backup directory - update to target backup directory
BACKUP_DIR=/insert/your/path/here

echo "Creating backup directory if it doesn't exist"
mkdir -p $BACKUP_DIR

# unraid flash drive backup to zip via native backup script
# backup saved to dir /usr/local/emhttp
echo 'Executing native unraid backup script'
/usr/local/emhttp/webGui/scripts/flash_backup 

echo ""
echo 'Remove symlink from emhttp'
find /usr/local/emhttp/ -maxdepth 1 -name '*flash-backup-*.zip' -delete

sleep 5

echo 'Move Flash Zip Backup from Root to Backup Destination'
mv /*-flash-backup-*.zip $BACKUP_DIR

sleep 5

echo 'Deleting Old Backups'

#ENTER NUMERIC VALUE OF DAYS AFTER "-MTIME +"
find $BACKUP_DIR* -mtime +30 -exec rm -rfv {} \;

echo 'All Done'

***Full disclosure -  I don't assume any liability here for data loss or anything else. Use at your own risk!
 

  • Like 2
Link to comment
3 hours ago, nitewolfgtr said:

Thank you Cpt. Chaz for this solution. It was very helpful in getting my automated backup cron job set-up.

I did however make a small tweak so that it calls the native unraid zip script directly as part of a backup script within the user script plugin. This way I only have to set-up 1 cron job. 🙂

 


#!/bin/bash
#backup directory - update to target backup directory
BACKUP_DIR=/insert/your/path/here

echo "Creating backup directory if it doesn't exist"
mkdir -p $BACKUP_DIR

# unraid flash drive backup to zip via native backup script
# backup saved to dir /usr/local/emhttp
echo 'Executing native unraid backup script'
/usr/local/emhttp/webGui/scripts/flash_backup 

echo ""
echo 'Remove symlink from emhttp'
find /usr/local/emhttp/ -maxdepth 1 -name '*flash-backup-*.zip' -delete

sleep 5

echo 'Move Flash Zip Backup from Root to Backup Destination'
mv /*-flash-backup-*.zip $BACKUP_DIR

sleep 5

echo 'Deleting Old Backups'

#ENTER NUMERIC VALUE OF DAYS AFTER "-MTIME +"
find $BACKUP_DIR* -mtime +30 -exec rm -rfv {} \;

echo 'All Done'

***Full disclosure -  I don't assume any liability here for data loss or anything else. Use at your own risk!
 

nice work @nitewolfgtr! I had originally tried to modify the actual zip script so that it would just backup and zip to $dir, but after a couple of failed attempts, i was out of my depth with it. So then i took to writing the second script, and never occurred to me to call the original script within it. sometimes you can't see the forest for the trees. hopefully the rest of it works well for you!

Link to comment
2 hours ago, Neo_x said:

i am using CA appdata backup

 

it does however seem to be focused on appdata, but includes backup options for flash as well as vm's

works very well :)

i think most do. however, this method calls the native (and manual) zipped flash backup script. it let's you save multiple dated, zipped files instead of a single unzipped backup of the files. i too use the CA appdata backup, and it's handy if you ever just need to restore a single file from the flash backup. But trying to reinstall fresh from an unzipped backup has it's own challenges. I recently found myself in a bind with this, hence the inspiration for the script to save "snapshots" of zipped flash backups 😀

Link to comment
  • 1 month later...

Thanks @Cpt. Chaz and @nitewolfgtr for this.

Just one minor thing - might want to double quote $BACKUP_DIR in the code to prevent globbing and word splitting in case of backup directories with spaces.

 

EDIT: Does not appear to work on 6.9b30 as the backups are being stored on /mnt/user/system rather than /

The GUI backup appears to also not work due to this as well. I've posted on the beta thread to see if it's just me....

 

EDIT 2: For 6.9b30 the move line needs to be changed to reflect the new location, then works nicely.

echo 'Move Flash Zip Backup from Root to Backup Destination'
mv /mnt/user/system/*-flash-backup-*.zip "$BACKUP_DIR"

 

Edited by sreknob
Link to comment
On 11/9/2020 at 9:59 AM, sreknob said:

Thanks @Cpt. Chaz and @nitewolfgtr for this.

Just one minor thing - might want to double quote $BACKUP_DIR in the code to prevent globbing and word splitting in case of backup directories with spaces.

 

EDIT: Does not appear to work on 6.9b30 as the backups are being stored on /mnt/user/system rather than /

The GUI backup appears to also not work due to this as well. I've posted on the beta thread to see if it's just me....

 

EDIT 2: For 6.9b30 the move line needs to be changed to reflect the new location, then works nicely.


echo 'Move Flash Zip Backup from Root to Backup Destination'
mv /mnt/user/system/*-flash-backup-*.zip "$BACKUP_DIR"

 

thanks for this @sreknob! i've updated the original post to reflect your contributions 🙂

Link to comment
  • 11 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.