What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Backuping Entware on a regular basis

gjf

Senior Member
I am going to automate backuping Entware partition on a regular basis.

To perform it I have installed cron from Entware and now I am going to create a script to put in cron folder to be started every week for instance.

The script should tar and gzip Entware folder in another place (Entware is on flash drive, backup should be on HDD). The number of backups can be set also.

The problem is I have no experience with dash, but bash (and a little). Unfortunately I didn't find any documentation about Merlin dash syntax. Can somebody help me to edit the script? As for now the problem is with for loop variable.

Code:
#!/bin/sh

SOURCEDIR=/mnt/swapusb/entware
OUTPUTDIR=/mnt/VERBATIM_HD/Download2/Backup
NUMBERBCKS=1

if [ -f ${OUTPUTDIR}/entware.tar.gz.bck${NUMBERBCKS} ]
  then
     rm -f ${OUTPUTDIR}/entware.tar.gz.bck${NUMBERBCKS}

count=1
while [ $count<$NUMBERBCKS ]
  do
    mv ${OUTPUTDIR}/entware.tar.gz.bck${count} ${OUTPUTDIR}/entware.tar.gz.bck${$count+1}
    {count++}
  done

if [ -f ${OUTPUTDIR}/entware.tar.gz]
  then
     mv ${OUTPUTDIR}/entware.tar.gz ${OUTPUTDIR}/entware.tar.gz.bck1

tar -czf ${OUTPUTDIR}/entware.tar.gz ${SOURCEDIR}
 
Last edited:
Just saying, cron is part of Asuswrt-Merlin.
Enter cru for the commands.
cru l to list saved jobs.
 
Thanks but whatever - the question was about the script, it's not a problem to put it in cru instead of cron :)
 
Changed "for" loop into "while", but anyway something wrong in
Code:
mv ${OUTPUTDIR}/entware.tar.gz.bck${count} ${OUTPUTDIR}/entware.tar.gz.bck${$count+1}
 
Last edited:
@gif Thank you for initiate the topic. I would like also to setup automated Backuping Entware on a regular basis. Unfortunately I cannot help in dash syntax or while loop bot hope somebody here could and will do it.

All the best!
 
OK, after a day of reading and debugging the final script was created.
Here it is:
Code:
#!/bin/sh

# Author is Gray Jack the Fixxxer

# SOURCEDIR is the path to Entware/Optware installation place
SOURCEDIR=/mnt/swapusb/entware

# OUTPUTDIR is the path where backups should be created
OUTPUTDIR=/mnt/VERBATIM_HD/Download2/Backup

# NUMBERBCKS means how much old copies of backup should be left
# NUMBERBCKS=0 means backup will be overwritten every time and no old copy will be left
NUMBERBCKS=2

if [ -f ${OUTPUTDIR}/entware.tar.gz.bck${NUMBERBCKS} ]
   then
       rm -f ${OUTPUTDIR}/entware.tar.gz.bck${NUMBERBCKS}
   fi

count=$(($NUMBERBCKS-1))
while [ $count -gt 0 ]
   do
       if [ -f ${OUTPUTDIR}/entware.tar.gz.bck${count} ]
           then
               mv ${OUTPUTDIR}/entware.tar.gz.bck${count} ${OUTPUTDIR}/entware.tar.gz.bck$(($count+1))
           fi
       count=$(($count-1))
   done

if [ -f ${OUTPUTDIR}/entware.tar.gz ] && [ $NUMBERBCKS -ne 0 ]
   then
       mv ${OUTPUTDIR}/entware.tar.gz ${OUTPUTDIR}/entware.tar.gz.bck1
   fi

tar -czf ${OUTPUTDIR}/entware.tar.gz -C ${SOURCEDIR} .

You can place the script in /opt/bin, don't forget to set execution permissions.
Then you can add it in cron with the following command (just an example):
Code:
cru a EntBck week /opt/bin/entwarebackup.sh

Feedbacks are welcome.
 
Last edited:
OK, after a day of reading and debugging the final script was created.
Here it is:
Code:
#!/bin/sh

# Author is Gray Jack the Fixxxer

# SOURCEDIR is the path to Entware/Optware installation place
SOURCEDIR=/mnt/swapusb/entware

# OUTPUTDIR is the path where backups should be created
OUTPUTDIR=/mnt/VERBATIM_HD/Download2/Backup

# NUMBERBCKS means how much old copies of backup should be left
# NUMBERBCKS=0 means backup will be overwritten every time and no old copy will be left
NUMBERBCKS=2

if [ -f ${OUTPUTDIR}/entware.tar.gz.bck${NUMBERBCKS} ]
   then
       rm -f ${OUTPUTDIR}/entware.tar.gz.bck${NUMBERBCKS}
   fi

count=$(($NUMBERBCKS-1))
while [ $count -gt 0 ]
   do
       if [ -f ${OUTPUTDIR}/entware.tar.gz.bck${count} ]
           then
               mv ${OUTPUTDIR}/entware.tar.gz.bck${count} ${OUTPUTDIR}/entware.tar.gz.bck$(($count+1))
           fi
       count=$(($count-1))
   done

if [ -f ${OUTPUTDIR}/entware.tar.gz ] && [ $NUMBERBCKS -ne 0 ]
   then
       mv ${OUTPUTDIR}/entware.tar.gz ${OUTPUTDIR}/entware.tar.gz.bck1
   fi

tar -czf ${OUTPUTDIR}/entware.tar.gz ${SOURCEDIR}

You can place the script in /opt/bin, don't forget to set execution permissions.
Then you can add it in cron with the following command (just an example):
Code:
cru a EntBck week /opt/bin/entwarebackup.sh

Feedbacks are welcome.
Please cron can be set with "week" in code? 10q

Sent from my ONE A2003 using Tapatalk
 
@amplatfus Sorry, I cannot understand you.
The code itself does not contain a regularity - it simply do the job.
You can use either built-in cru or install cron.
How to setup a period - it depends on what you will use.
In my example cru will install a task named "EntBck" to perform every week. You can name it in any other way as well as setup to run every day for instance.
The place where you will place the script as well as the name of the script is up to you - but you should mention it correctly when creating a cru/cron task.
 
In cru statement you used "week" word. And I far as I know it should be something like * * 7 *.

I will try it. I started to dream about a restore script.

BR

Sent from my ONE A2003 using Tapatalk
 
Remember that the cron job is volatile with cru. You have to add it again at next boot in a jffs script.
 
Changed tar command a little to comply to jffs vbackup mechanism.
As for cru... damn, I'd better install cron from Entware.
 
I started to dream about a restore script.
As I said I am not experienced enough in dash scripting.
To restore you actually need the following:
  1. Run:
    Code:
    tar -xzf <full-source-path>/entware.tar.gz -C <full-destination-path>
    for instance:
    Code:
    tar -xzf /tmp/mnt/HDD/Backup/entware.tar.gz -C /tmp/mnt/usbstick/
  2. Create /jffs/scripts/post-mount or parse the existing file with the string:
    Code:
    if [ "$1" = "<full-destination-path-should-be-here>" ] ; then
      ln -nsf $1/entware /tmp/opt
    fi
    in out example it should contain
    Code:
    if [ "$1" = "/tmp/mnt/usbstick" ] ; then
      ln -nsf $1/entware /tmp/opt
    fi
  3. Create or edit "opt" symlink in /tmp to point to <full-destination-path>/entware in our example it should be: "/tmp/mnt/usbstick/entware"
  4. Create or edit "opt" symlink in / to point to /tmp/opt
I believe it's quite easy to do, but taking into account my noobism in scripting I won't try. And please take into mind the backup script can work both with Entware and Optware - but I have no idea how to change all restoration process for Optware, because I don't use it. Also restoring is not necessary on a regular basis, so one can perform this actions manually.

Anyway if someone develops the script - I will be happy!
 
P.S. Actually if you want just to restore Entware in it's original path the following simple script will work:

Code:
#!/bin/sh

# Author is Gray Jack the Fixxxer

# SOURCEDIR is the path to backup
# you can use command line: "entwarerestore.sh /mnt/VERBATIM_HD/Download2/Backup/entware.tar.gz"
# or hardcode the path right in the script
# below the command line parameter will be used
SOURCEDIR=$1
#SOURCEDIR=/mnt/VERBATIM_HD/Download2/Backup/entware.tar.gz

# OUTPUTDIR is the path where Entware was installed and should be restored
OUTPUTDIR=/mnt/swapusb/entware

rm -rf ${OUTPUTDIR}
tar -xzf ${SOURCEDIR} -C ${OUTPUTDIR}
sleep 10
reboot

It simply performs i.1 mentioned above without parsing ant symlinking assuming it was done already when Entware was installed.
 
Thank you for input. In the meantime I tested and it works with number of backups also, but I got the archive but with two errors in terminal when running the script:

Code:
tar: removing leading '/' from member names
tar: /tmp/mnt/entware-ng.arm/var/run/mysqld.sock: socket ignored
I believe that mysqld.sock exist because mysql was running, so I improved the script with one line before and one after tar command like this:

Code:
services stop
sleep 10
tar -zcvf ${OUTPUTDIR}/entware.tar.gz -X /tmp//exclude.txt ${SOURCEDIR}
sleep 10
services start

PS: I excluded from tar the swap file with -X paramter and put -z to tar.
 
Tar issue was solved already - see above:
Code:
...
tar -czf ${OUTPUTDIR}/entware.tar.gz -C ${SOURCEDIR} .
"v" option is not necessary because script is not intended for verbose output. Same for exclusion - actually /tmp//exclude.txt should not come in archive at all :)
As for services - yes, maybe, but I don't use mysqld personally and I don't want to change something in system work.
 
Tar issue was solved already - see above:
Code:
...
tar -czf ${OUTPUTDIR}/entware.tar.gz -C ${SOURCEDIR} .
"v" option is not necessary because script is not intended for verbose output. Same for exclusion - actually /tmp//exclude.txt should not come in archive at all :)
As for services - yes, maybe, but I don't use mysqld personally and I don't want to change something in system work.
Exclude.txt is a file where you can put the files you do not want to be archive. For instance in my case the swap file from /tmp/Partition/entware-ng.arm/swap

Exclude.txt
Code:
swap
any other files to be excluded from archive

Thank you!
 
Exclude.txt is a file where you can put the files you do not want to be archive. For instance in my case the swap file from /tmp/Partition/entware-ng.arm/swap
Yes, but it is very strange way of placing swap file. I would place it out of Entware partition, one level higher for example.
Also in your example /tmp/exclude.txt will work, but exclude.txt will be volatile so you should re-create it every reboot.
But sure it's up to you.
 
Swap file is located there after installing process from here https://www.hqt.ro/how-to-install-new-generation-entware/
I know I can move it after, but I see this option with exclusion easier. I moved the file from /tmp/exclude.txt to the /tmp//mnt/backup/exclude.txt in order to have it permanently.
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top