Backup savefile every "time interval" and Backup-on-Shutdown

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

Backup savefile every "time interval" and Backup-on-Shutdown

#1 Post by r1tz »

yes... so i made yet another backup thing.

I put this into /root/Startup, so i wont have to care about backing up again! :D
I didnt use the backup-on-shutdown because i turn my computer on at 9am and shutdown at 7pm.
Don't want to potentially lose 8hours(assuming it fails at 6pm, and i took an hour's lunch break ;) ) of work.

You have to edit the script for it to work.

Code: Select all

#!/bin/sh

#This is the time interval between 2 backups.
bacinterval="3h 12m 38s"

#Path to savefile
source=/mnt/home/lupusave-haha.3fs

#For the des, dont forget the "/" at the back!
des=/mnt/sda2/boot/Lupu51/Backup/

#This is the max number of backups. The oldest backup will get deleted.
maxbac=4

#If you are backing up to external hard drive. Uncomment the following 7 commented lines.
#externaldrive=/dev/sda2

for (( ; ; ))
do
##Put this at the buttom if you want to backup on boot. The double "#" is because i said "Uncomment the following 7 commented lines." lol.
	sleep ${bacinterval}

#	if mount|grep ${externaldrive};then
#	echo
#	else
#	mount /dev/sda2 /mnt/sda2
#	fi

	num=`find ${des} -maxdepth 1 -name *.bac | wc -l`
	if [ ${num} -ge ${maxbac} ];then
		rm -f ${des}"$(ls -t ${des} | tail -n 1)"
	fi

#I used "-close box" in case my mouse was on the splash as it appeared and i missed it. Go ahead and remove "-close box" if you want the splash to close when mouse-hover-over-splash. It wont be automatic with "-close box" btw. Add a timeout if your want it to backup while you are afk. I dont do this beause there will be nothing much to backup while i am afk.(ei. no data change)
	gtkdialog-splash -text "Backup is starting. Please do not shutdown till completed" -close box

#You might want to change this to something easier to view(eg. %a for abbreviated name(e.g. Sun)). Make sure you include the time or it will conflict when 2 backup in a day.
	date=`date +%T-%e-%m-%y`.bac

	cp -f "${source}" "${des}${date}"

#Same as above.
	gtkdialog-splash -text "Backup Completed" -close box
done
I added as many comments and spaces as i could to make the script look long :D

Srsly, the spaces are to make the comments readable. If not it will be too messy.

Its only 23 lines... or 17 if you arnt backing up to external drive.

Feel free to remove my stupid comments. ;)
Edit4:Srsly... it looks very nice without the comments.

EDIT:Vladd44 suggested using tar.gz to save space.
This take a little longer than cp(possibily more cpu), but since its only once in a few hours... no harm done.
Since I have space to spare so i do not use this.

Just change

Code: Select all

cp -f "${source}" "${des}${date}"
to

Code: Select all

tar -czf "${des}${date}.tar.gz" "${source}"
And change

Code: Select all

num=`find ${des} -maxdepth 1 -name *.bac | wc -l`
to

Code: Select all

num=`find ${des} -maxdepth 1 -name *.bac.tar.gz | wc -l`
EDIT2: I put "sleep ${bacinterval}" at the top so it wont backup on boot.
Also, if you want your first backup to start at a different timing from the intervals, add "sleep 1h"(this is just an example) before "for (( ; ; ))". Then move "sleep ${bacinterval}" to the buttom(before "done")

EDIT3:
For people who leave thier computer on 24/7 and want to backup once per day at a specified time, you can use this

Code: Select all

#!/bin/sh

#backup interval is 1day
bacinterval="1d"

source=/mnt/home/lupusave-ritchie.3fs
des=/mnt/sda2/boot/Lupu51/Backup/
maxbac=8
externaldrive=/dev/sda2

#Here you can specify what time you want the backup to be. Note that this is a 24h clock where 3pm = 15:00
dailybactime=03:15


currenttime=`date +%s`
sleeptime="$(($(date -d ${dailybactime} +%s) - ${currenttime}))"
if [ ${sleeptime} -le 0 ];then
targettmr="'$(date +%m/$((`date +%d` + 1))/%Y) ${dailybactime}'"
sleeptime="$(($(eval "date -d ${targettmr} +%s") - ${currenttime}))"
fi
sleep ${sleeptime}

for (( ; ; ))
do
	if mount|grep ${externaldrive};then
	echo
	else
	mount /dev/sda2 /mnt/sda2
	fi
	num=`find ${des} -maxdepth 1 -name *.bac.tar.gz | wc -l`
	if [ ${num} -ge ${maxbac} ];then
		rm -f ${des}"$(ls -t ${des} | tail -n 1)"
	fi
	gtkdialog-splash -text "Backup is starting. Please do not shutdown till completed" -close box
	date=`date +%T-%e-%m-%y`.bac
	tar -czf "${des}${date}.tar.gz" "${source}"
	gtkdialog-splash -text "Backup Completed" -close box
	sleep ${bacinterval}
done
Also, i just realize that "gtk-dialog splash" has an agruement, "-placement"
You could add "-placemement top" for example. :D


IMPORTANT EDIT5: Im so sorry. I made a mistake for the compression thing. It should be "tar -czf" not "tar -cvf".
I changed the above one too.


EDIT6: added "Add a timeout if your want it to backup while you are afk." to comment on gtk-dialog splash.
Last edited by r1tz on Sun 20 Feb 2011, 15:20, edited 6 times in total.

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

Backup on shutdown

#2 Post by r1tz »

Making shutdown wait for tar/cp
Doing this will allow you to do without the splash(which tells you when the backup started and is done).
This way, auto backup will truely be "in the background".
At the start of the shutdown script, add

Code: Select all

#change "tar" to "cp" if you are using cp to backup.
pid=$(pgrep '^tar$')
if [ "$pid" != "" ];then
ps -p$pid 2>&1 > /dev/null
status=$?
if [ "$status" == "0" ];then
while [ "$status" == "0" ]
do
sleep 2s
ps -p$pid 2>&1 > /dev/null
status=$?
done
fi
fi


To backup on shutdown

edit /etc/rc.d/rc.shutdown

Find

Code: Select all

 fi
 busybox mount -t $SAVEFS -o remount,ro $SAVEDEV /initrd${SAVE_LAYER} 2>/dev/null
 umount-FULL -i -n -l /initrd${SAVE_LAYER} 2>/dev/null #-l is lazy unmount.
fi
Then put this underneth:

Code: Select all

#Change this to something else if you want to change the default to "skip backup" instead.
doyouwanttobac=y

#You might also want to change this so it makes sense
echo "Backup?[input "y" to start or anything else to skip] Autobackup is 10seconds." >/dev/console

read -t 10 doyouwanttobac
if [ ${doyouwanttobac} = y ];then

#Note that the source is not "/mnt/home"
source=/initrd/mnt/dev_save/lupusave-ritchie.3fs
des=/mnt/sda2/boot/Lupu51/Backup/
maxbac=8

#drive to backup to(if applicable)
externaldrive=/dev/sda2

#drive that pupsave is in
externaldriv=/dev/sda4

	if mount|grep ${externaldriv};then
	echo
	else
	if [ -d /initrd/mnt/dev_save ];then
	mkdir /initrd/mnt/dev_save
	fi
	mount /dev/sda4 /initrd/mnt/dev_save
	fi
	
	if mount|grep ${externaldrive};then
	echo
	else
	if [ -d /dev/sda2 ];then
	mkdir /dev/sda2
	fi
	mount /dev/sda2 /mnt/sda2
	fi
	num=`find ${des} -maxdepth 1 -name *.bac.tar.gz | wc -l`
	if [ ${num} -ge ${maxbac} ];then
		rm -f ${des}"$(ls -t ${des} | tail -n 1)"
	fi
#i havnt test if the echo works... but even it doesnt, it will be obvious that it is doing the backup unless your pupsave is 1mb
	echo "Backup is starting." >/dev/console

	date=`date +%T-%e-%m-%y`.bac
	tar -czf "${des}${date}.tar.gz" "${source}"

#i dunno if this "wait" is needed. Please tell me if not needed.
	wait
fi

To have multiple source to backup from, you can do this

Code: Select all

source=('/mnt/home/folder-to-backup-one' '/mnt/sda2/important-folder' '/mnt/sda3/veryimportant')
sourcenum=${#source[@]}
and add this into the bigger loop.

Code: Select all

date=`date +%T-%e-%m-%y`.bac
for (( n=0;n<$sourcenum;n++)); do
    tar -czf "${des}${source[${n}]}${date}.tar.gz" "${source[${n}]}"
done
Last edited by r1tz on Sun 20 Feb 2011, 17:19, edited 5 times in total.

aarf

#3 Post by aarf »

It is a good idea

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

#4 Post by r1tz »

aarf, thanks
----

Can someone with skill i can only dream of make a GUI.
Something that allows the user to input all the values.
Then it will edit theshutdown script and make a script in /root/Startup accordingly?

Such a GUI will be a great addition to puppy. :D

stu90

#5 Post by stu90 »

Hi r1tz,
for GUI there is Xdialog in puppy Lucid not sure about other puplets? or Zenity / Yad seems to be a bit easier to use here.
http://www.murga-linux.com/puppy/viewtopic.php?t=58306

aarf

#6 Post by aarf »

r1tz wrote:aarf, thanks
----

Can someone with skill i can only dream of make a GUI.
Something that allows the user to input all the values.
Then it will edit theshutdown script and make a script in /root/Startup accordingly?

Such a GUI will be a great addition to puppy. :D
perhaps you could interest zigbert in using/modifying the pschedule gui.

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

#7 Post by r1tz »

aarf wrote:
r1tz wrote:aarf, thanks
----

Can someone with skill i can only dream of make a GUI.
Something that allows the user to input all the values.
Then it will edit theshutdown script and make a script in /root/Startup accordingly?

Such a GUI will be a great addition to puppy. :D
perhaps you could interest zigbert in using/modifying the pschedule gui.
Im not too sure about that. The pschedule looks alot more like a gui for cron and i dont think it really is suited for this purpose.
stu90 wrote:Hi r1tz,
for GUI there is Xdialog in puppy Lucid not sure about other puplets? or Zenity / Yad seems to be a bit easier to use here.
http://www.murga-linux.com/puppy/viewtopic.php?t=58306
yad looks nice. I think i might try it.

However i have not made any gui expect for my crappy opera profile changer http://www.murga-linux.com/puppy/viewtopic.php?p=494789 which has a lot of bugs(becuase i try to do it as a gui).
In other words i have about 0 experence in making gui.

I will need to learn how to use yad.... and hopefully be able to make a decent gui.

Im quite busy atm so it will be sometime before i actually make something useful.

I want to make a gui with as many options as possible.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

learn how to make GUI

#8 Post by don570 »

The method I used to learn to make a GUI was to study the
program mtpaintsnapshot.sh which is installed in most Puppies.

I rewrote the program to localize it
http://www.murga-linux.com/puppy/viewto ... 476#496476

You should take a look at my timer program
http://www.murga-linux.com/puppy/viewto ... 479#496479




___________________________________________________________

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

Re: learn how to make GUI

#9 Post by r1tz »

double post. sorry
Last edited by r1tz on Sun 20 Feb 2011, 12:40, edited 1 time in total.

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

Re: learn how to make GUI

#10 Post by r1tz »

don570 wrote:The method I used to learn to make a GUI was to study the
program mtpaintsnapshot.sh which is installed in most Puppies.

I rewrote the program to localize it
http://www.murga-linux.com/puppy/viewto ... 476#496476

You should take a look at my timer program
http://www.murga-linux.com/puppy/viewto ... 479#496479
Thanks, i thing that will aid me in learning how to make a gui.
Documentation on zenity and yad seems abit hard to find...

btw, I tried yad for the first time yesterday, made a theme changer for geany http://murga-linux.com/puppy/viewtopic.php?t=64946.

PaulBx1
Posts: 2312
Joined: Sat 17 Jun 2006, 03:11
Location: Wyoming, USA

#11 Post by PaulBx1 »

I did something like this a while back. The howto is floating around here somewhere. That was when I had a big, valuable pupsave (full of emails and so forth).

Now I have a small pupsave and all personal stuff is saved outside of it. I back up the pupsave once and then forget it. Easy to recover.

Does my off-pupsave stuff now get backed up regularly? Er, no. :oops:

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

#12 Post by r1tz »

PaulBx1 wrote:I did something like this a while back. The howto is floating around here somewhere. That was when I had a big, valuable pupsave (full of emails and so forth).

Now I have a small pupsave and all personal stuff is saved outside of it. I back up the pupsave once and then forget it. Easy to recover.

Does my off-pupsave stuff now get backed up regularly? Er, no. :oops:
You can always use tar to backup normal folders.(see first post)

Refer at post2 for having more than one source to backup from, i edited it.

This way you can backup multiple normal folders. :D

nooby
Posts: 10369
Joined: Sun 29 Jun 2008, 19:05
Location: SwedenEurope

#13 Post by nooby »

I love this thread. Would be cool to have as small save file as possible and to ahve it backed up regulary and safely without too much delay.

I often screw up the save file with programs that get messed up.

So looking forward to what you try to achieve there.
I use Google Search on Puppy Forum
not an ideal solution though

User avatar
r1tz
Posts: 162
Joined: Thu 09 Sep 2010, 05:19
Location: In #puppylinux (IRC)

#14 Post by r1tz »

Added "how to make shutdown wait for tar/cp" in the second post.
This way, we can do without gtk-dialog splash.

Then it will be really automatic and in the background!


EDIT: 14/4/2011
Blah, i still cant make a good GUI.

Anyone else want to do it? :D

User avatar
Luluc
Posts: 200
Joined: Wed 16 Mar 2011, 07:10

#15 Post by Luluc »

This is very useful, thanks. I just implemented this for myself.

However, instead of the 'cp' or the 'tar' command, I'm using 'rsync -aHxzv --no-whole-file'. That command will use rsync to make an incremental backup. No matter what your file is, rsync will compare individual raw blocks and update only those that have been changed in comparison with an existing target file. So instead of copying the entire file over again, it will likely copy just 10% or 20% of it, just the parts that changed. The rest is not necessary. It is a much faster procedure.

Best regards.

shadower_sc
Posts: 136
Joined: Wed 21 Apr 2010, 23:03
Location: Texas

Re: Backup savefile every "time interval" and Backup-on-Shutdown

#16 Post by shadower_sc »

That looks like something I need to be doing. :-) Thanks for sharing. :D

Post Reply