Page 1 of 1

daily backup script?

Posted: Tue Oct 11, 2022 10:19 am
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.

Re: daily backup script?

Posted: Wed Oct 12, 2022 12:44 am
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.

Re: daily backup script?

Posted: Wed Oct 12, 2022 1:58 am
by pkkrusty
Entire configuration

Re: daily backup script?

Posted: Thu Oct 13, 2022 4:22 am
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.

Re: daily backup script?

Posted: Sat Oct 15, 2022 3:51 am
by pkkrusty
Awesome thanks!

Re: daily backup script?

Posted: Sat Oct 15, 2022 7:03 am
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

Re: daily backup script?

Posted: Sat Oct 15, 2022 6:39 pm
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.

Re: daily backup script?

Posted: Thu Feb 22, 2024 5:26 pm
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!