I put this into /root/Startup, so i wont have to care about backing up again!
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
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}"
Code: Select all
tar -czf "${des}${date}.tar.gz" "${source}"
Code: Select all
num=`find ${des} -maxdepth 1 -name *.bac | wc -l`
Code: Select all
num=`find ${des} -maxdepth 1 -name *.bac.tar.gz | wc -l`
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
You could add "-placemement top" for example.
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.