daily backup script?

General discussion about Gargoyle, OpenWrt or anything else even remotely related to the project

Moderator: Moderators

Post Reply
pkkrusty
Posts: 45
Joined: Mon Jan 13, 2020 4:41 pm

daily backup script?

Post by pkkrusty »

I did some searching but the search function ignores the word "script" so nothing came up. Is there a handy script/cron already written to make a backup every x hours and save to an attached USB drive? I assume this would occur in crontab, but I'm mostly familiar with Debian and router command line structure is a bit foreign to me.

Lantis
Moderator
Posts: 6735
Joined: Mon Jan 05, 2015 5:33 am
Location: Australia

Re: daily backup script?

Post by Lantis »

What do you want to backup?
There are backup scripts for BW and Quota data already written that write to /usr/data and /tmp/data, which would be a great template to start.
Do you want to backup the entire configuration? There's a script for that too which could be used as a base.
http://lantisproject.com/downloads/gargoyle_ispyisail.php for the latest releases
Please be respectful when posting. I do this in my free time on a volunteer basis.

pkkrusty
Posts: 45
Joined: Mon Jan 13, 2020 4:41 pm

Re: daily backup script?

Post by pkkrusty »

Entire configuration

Lantis
Moderator
Posts: 6735
Joined: Mon Jan 05, 2015 5:33 am
Location: Australia

Re: daily backup script?

Post by Lantis »

Use the existing script
https://github.com/ericpaulbishop/gargo ... _backup.sh

Code: Select all

sh /usr/lib/gargoyle/create_backup.sh
Then download/transfer the file from /tmp/backup/backup.tar.gz and delete it after.
http://lantisproject.com/downloads/gargoyle_ispyisail.php for the latest releases
Please be respectful when posting. I do this in my free time on a volunteer basis.

pkkrusty
Posts: 45
Joined: Mon Jan 13, 2020 4:41 pm

Re: daily backup script?

Post by pkkrusty »

Awesome thanks!

pkkrusty
Posts: 45
Joined: Mon Jan 13, 2020 4:41 pm

Re: daily backup script?

Post by pkkrusty »

OK, so what I would do is edit the root crontab to execute that default script every x amount of time.

But I'll need to adjust the script to copy the backup file to the USB drive then rename it to include datetime in the filename.

Can you give me some hints on the syntax for that? Or a google search phrase that will get me started?

something like:

Code: Select all

mv /tmp/backup/backup.tar.gz /mnt/backup_$(date +"%FT%H%M".tar.gz

Lantis
Moderator
Posts: 6735
Joined: Mon Jan 05, 2015 5:33 am
Location: Australia

Re: daily backup script?

Post by Lantis »

Yes something like that. I’d use
"$(date +%Y%m%d_%H%M%S)".tar.gz

Your usb drive should have a mount point under /tmp/usb_mount/ as well.

My advice is to not modify the original script as that is part of Gargoyle functionality.
Instead write another script called “mybackup.sh” that does 2 things:
1. Calls the gargoyle backup script
2. Moves the file to your usb drive

Then use cron to execute that one.
http://lantisproject.com/downloads/gargoyle_ispyisail.php for the latest releases
Please be respectful when posting. I do this in my free time on a volunteer basis.

pkkrusty
Posts: 45
Joined: Mon Jan 13, 2020 4:41 pm

Re: daily backup script?

Post by pkkrusty »

Final solution looks like:

script as follows (copied from normal backup script and added the file copy at the end)

Code: Select all

#make sure all settings have been written to file
uci commit

#force write of webmon & bwmon
bwmon_enabled=$(ls /etc/rc.d/*bwmon* 2>/dev/null)
webmon_enabled=$(ls /etc/rc.d/*webmon* 2>/dev/null)
if [ -n "$bwmon_enabled" ] ; then
        /etc/init.d/bwmon_gargoyle stop
fi
if [ -n "$webmon_enabled" ] ; then
        /etc/init.d/webmon_gargoyle stop
fi

backup_locations='/etc/passwd /etc/shadow /etc/config /etc/rc.d /etc/TZ /etc/firewall.user /etc/ethers /etc/hosts /etc/webm
existing_locations=""
for bl in $backup_locations ; do
        if [ -e "$bl" ] ; then
                existing_locations="$existing_locations $bl"
        fi
done

if [ -e /tmp/backup ] ; then
        rm -rf /tmp/backup
fi
mkdir -p /tmp/backup
cd /tmp/backup
tar cvzf backup.tar.gz $existing_locations
chmod 777 backup.tar.gz
garg_web_root=$(uci get gargoyle.global.web_root)
if [ -z "$garg_web_root" ] ; then
        garg_web_root = "/www"
fi

if [ -n "$bwmon_enabled" ] ; then
        /etc/init.d/bwmon_gargoyle start
fi
if [ -n "$webmon_enabled" ] ; then
        /etc/init.d/webmon_gargoyle start
fi
mv /tmp/backup/backup.tar.gz /tmp/usb_mount/dev_sda2/backup_$(date -u +%FT%H%MZ).tar.gz
(note that I have a USB attached that has a FAT partition and a EXT4 partition, so specifically using the second one)

and the cron job:

Code: Select all

01 * * * * sh /usr/lib/gargoyle/user_backup.sh

Works great!

Post Reply