home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / sbin / bkup2cd.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2006-04-14  |  9.3 KB  |  341 lines

  1. #!/bin/sh
  2. #script to backup /root to CD.
  3. #(c) Copyright Barry Kauler 2004,2005 www.goosee.com/puppy
  4.  
  5.  
  6. if [ ! -f /etc/cdburnerdevice ];then
  7.  xmessage -bg "medium purple" -center -title "Puppy backup to CD" "You must run the CD/DVD drive Wizard first, to select
  8. a burner drive."
  9.  exit
  10. fi
  11. CDR="ATAPI:/dev/`cat /etc/cdburnerdevice`"
  12. DEV="/dev/`cat /etc/cdburnerdevice`"
  13.  
  14.  
  15. #####
  16. echo '#!/bin/sh
  17. echo "Executing cdrecord now..."
  18. LINES1=0
  19. LINES2=0
  20. sleep 1
  21. ps | grep "cdrecord" >/dev/null
  22. RET1=$?
  23. while [ $RET1 -eq 0 ];do
  24.  sleep 1
  25.  LINES2=`wc -l /tmp/cdrlog.txt | tr -s " " | cut -f 2 -d " "`
  26.  if [ $LINES2 -gt $LINES1 ];then
  27.   LINESDIFF=`expr $LINES2 - $LINES1`
  28.   tail -n $LINESDIFF /tmp/cdrlog.txt
  29.   LINES1=$LINES2
  30.  fi 
  31.  ps | grep "cdrecord" >/dev/null
  32.  RET1=$?
  33. done
  34. echo "...finished executing cdrecord."
  35. echo -n "Press ENTER key to continue: "
  36. read nnn
  37. ' > /tmp/bkupx.sh
  38. chmod 755 /tmp/bkupx.sh
  39. #####
  40.  
  41. mount | grep "iso9660" > /dev/null 2>&1
  42. if [ $? -eq 0 ];then
  43.  xmessage -bg "#ffc0c0" -center -title "Puppy Backup to CD" -file -<<FILETXT
  44. Puppy sees that a CD is mounted.
  45. If it is in the CD-burner drive, please
  46. unmount it RIGHT NOW, before clicking
  47. on the OKAY button.
  48.  
  49. HINT: If you have the multi-session backup-CD
  50. mounted, check how much space is left on it
  51. before unmounting it.
  52. FILETXT
  53. fi
  54.  
  55. ##run cdrecord to get the scsibus:target:lun numbers...
  56. ##remove all lines with * in them...
  57. ##v1.0.0 SCSIADDR0="`cdrecord -scanbus 2>/dev/null | grep -v "*" | grep -i "rw" | cut -f 2 | head -n 1`"
  58. #SCSIADDR0="`cdrecord -scanbus 2>/dev/null | grep -v "*" | grep "[0-9],[0-9],[0-9]" | cut -f 2 | head -n 1`"
  59. #if [ "$SCSIADDR0" = "" ];then
  60. # xmessage -bg "#ffc0c0" -center -title "Puppy Backup to CD" -file -<<FILETXT
  61. #ERROR!
  62. #Puppy is unable to find a SCSI or SCSI-emulated
  63. #CD-burner drive.
  64. #FILETXT
  65. #
  66. # exit 0
  67. #fi
  68. ##...example, SCSIADDR0=0,0,0
  69.  
  70.  
  71. NUMDAYSCD=1
  72. if [ ! -e /etc/bkup2cdrc ];then
  73.  #echo -e "CDR=$SCSIADDR0" > /etc/bkup2cdrc
  74.  #echo "DEV=/dev/scd0" >> /etc/bkup2cdrc
  75.  ##echo -e "LASTBKUP=\"\"" >> /etc/bkup2cdrc
  76.  NUMDAYSCD=365
  77. fi
  78.  
  79. #need to see when last backed up, then run backup for intervening period...
  80. SECSFILE=`date -r /etc/bkup2cdrc +%s`
  81. NUMDAYSF=`expr $SECSFILE / 86400`
  82. SECSDATE=`date +%s`
  83. NUMDAYSD=`expr $SECSDATE / 86400`
  84. if [ ! $NUMDAYSCD -eq 365 ];then
  85.  NUMDAYSCD=`expr $NUMDAYSD - $NUMDAYSF`
  86.  if [ $NUMDAYSCD -eq 0 ];then
  87.   NUMDAYSCD=1
  88.  fi
  89.  MSG1="Puppy sees that you backed up $NUMDAYSCD days ago."
  90. else
  91.  MSG1="Puppy sees that you have not backed up before."
  92. fi
  93.  
  94. while :; do
  95.  
  96. xmessage -bg "#e0e0ff" -center -name "pbkup" -title "Puppy backup to CD" -buttons \
  97.  Change_#_days:10,Backup_now:12,EXIT:14 \
  98.  -file -<<FILETXT
  99. Welcome to the Puppy backup-to-CD script!
  100.  
  101. $MSG1
  102.  
  103. Puppy will backup files in the /root folder, which
  104. has all your personal configuration and data files,
  105. including email, address books, histories, accounts.
  106. Puppy will backup all files modified in the last
  107. $NUMDAYSCD days.
  108.  
  109. If you want to change the number of days to be backed
  110. up, click on the "Change_#_days" button.
  111.  
  112. Note, this script currently only burns to a CD, not a DVD.
  113.  
  114. PLACE A CD IN THE CD-BURNER DRIVE,
  115. THEN PRESS "Backup_now"
  116. FILETXT
  117.  
  118. XREPLY=$?
  119.  
  120. if [ $XREPLY -eq 10 ];then #change number of days
  121.  NEWNUMDAYS=`gtk-shell -q -qv "$NUMDAYSCD" -t "#days to backup"`
  122.  if [ $NEWNUMDAYS ];then
  123.   NUMDAYSCD=$NEWNUMDAYS
  124.  fi
  125.  MSG1="Puppy will backup the last $NUMDAYSCD days."
  126. fi
  127.  
  128. if [ $XREPLY -eq 11 ];then #edit config file
  129.  gtk-shell -ef "/etc/bkup2cdrc" -t "Edit /etc/bkup2cdrc config file"
  130. fi
  131.  
  132. if [ $XREPLY -eq 12 ];then
  133.  break
  134. fi
  135.  
  136. #if [ $XREPLY -eq 13 ];then #help
  137. # dillo file:///usr/share/doc/bkup2cd.htm &
  138. #fi
  139.  
  140. if [ $XREPLY -eq 14 ];then #exit
  141.  break
  142. fi
  143. if [ $XREPLY -eq 0 ];then #exit
  144.  break
  145. fi
  146. if [ $XREPLY -eq 1 ];then #exit
  147.  break
  148. fi
  149.  
  150. done
  151.  
  152. if [ ! $XREPLY -eq 12 ];then #backup to cd
  153.  exit
  154. fi
  155.  
  156. #Note,some of this based on bkup2cd by r.f.smith 2001 gpl
  157. cd $HOME
  158. if [ ! -d tmp ]; then
  159.  mkdir tmp
  160. fi
  161. #CDR="`cat /etc/bkup2cdrc | grep "CDR" | cut -f 2 -d "="`"
  162. #DEV="`cat /etc/bkup2cdrc | grep "DEV" | cut -f 2 -d "="`"
  163. LOG=00README.backup
  164. DATE=`date "+%Y-%m-%d"`
  165. BLIST=$HOME/tmp/backup-list-$DATE
  166. LIST2=$BLIST.tmp
  167. FILE=$HOME/tmp/backup-$DATE.raw
  168. USR="puppy"
  169. LASTDATE=`date --date="$NUMDAYSCD days ago" "+%Y-%m-%d" 2>/dev/null`
  170.   # Write logfile header.
  171.   echo "Backing up from ${LASTDATE} to ${DATE}."
  172.   rm -f $LOG $HOME/tmp/backup-*
  173.   echo "# This backup contains all the files that have been changed in" >>$LOG
  174.   echo "# the period from ${LASTDATE} to ${DATE}." >>$LOG
  175.   echo "# "  >>$LOG
  176.   echo "# This file is automatically generated by bkup2cd ${VER}."  >>$LOG
  177.   echo >>$LOG
  178.  echo -n "Searching for files..."
  179.  #busybox grep is very simple...
  180.  cd $HOME
  181.  find . -mtime -$NUMDAYSCD -type f|grep -v "$FILE" |grep -v "00README\\.backup"|grep -v "$LIST2"|grep -v "$BLIST"|grep -vi "cache"|grep -v "my-applications"|grep -v "tmp"|grep -v "vmlinuz"|grep -v "image\\.gz"|cut -c 3-|sort >$BLIST
  182.   # Build the filespec list for mkisofs, with new graft points for the files.
  183.   cat $BLIST|awk '{printf("%s/%s=%s\n", '\"$DATE\"', $0, $0);}' >$LIST2
  184.   # Add the files with new graftpoints to the logfile.
  185.   cat $BLIST|awk '{printf("%s/%s\n", '\"$DATE\"', $0);}' >>$LOG
  186.   echo " done"
  187.  
  188. while :; do
  189. rm -f $BLIST
  190. rm -f $FILE
  191.  
  192.    # Create backup ISO9660 image
  193.    ERRCD=""
  194.    echo -n "Checking for previous tracks on CD..."
  195.    OUTP=`cdrecord -msinfo dev=${CDR} 2>/tmp/cdrerr.txt`
  196.    SECT=`echo ${OUTP}|grep ","`
  197.    OUTP=`cat /tmp/cdrerr.txt|grep "CDB"|awk '{print $2;}'`
  198.    if [ -z $SECT ]; then
  199.     #ERRCD=$OUTP
  200.     # No previous backup found, see codes above.
  201.     case $OUTP in
  202.         52) MSG1="Not a multisession CD";ERRCD="52";;
  203.         43) MSG1="CD is blank";;
  204.         00) MSG1="No CD found";ERRCD="00";;
  205.         *)  MSG1="Unknown value from cdrecord -msinfo";ERRCD="XX"
  206.     esac
  207.     #echo "Creating original backup track ${FILE}..."
  208.     MSG2="original"
  209.     mkisofs -J -R -o $FILE -pad -P "${USR}" -p "bkup2cd" -graft-points -path-list $LIST2 $DATE/$LOG=$LOG
  210.    else
  211.     MSG1="Multisession CD found"
  212.     #echo " found: ${SECT}"
  213.     #echo "Creating additional backup track ${FILE}..."
  214.     MSG2="additional"
  215.     mkisofs -J -R -o $FILE -C $SECT -M $DEV -pad -P "${USR}" -p "bkup2cd" -graft-points -path-list $LIST2 $DATE/$LOG=$LOG
  216.    fi
  217. cp $LOG $BLIST
  218.  
  219. SIZEFILE="`du $FILE`" #in Kbytes + filename
  220. SIZEFILK=`echo -n $SIZEFILE | tr -s " " | cut -f 1 -d " "`
  221.  if [ ! $SIZEFILK ];then
  222.   SIZEFILK=`echo -n $SIZEFILE | tr -s " " | cut -f 2 -d " "`
  223.  fi
  224. NUMTRACKS=`cdrecord -toc dev=$CDR |grep "track"|tail -n 2|head -n 1|tr -s " "|cut -f 2 -d " "`
  225. if [ ! $NUMTRACKS ];then
  226.  NUMTRACKS=0
  227. fi
  228. if [ $NUMTRACKS -gt 98 ];then #max allowed is 99.
  229.  ERRCD="VV"
  230.  MSG1="CD is full"
  231. else
  232. SIZEEXT=`cdrecord -toc dev=$CDR | grep "track" | tail -n 1 | tr -s " " | cut -f 3 -d " "`
  233. if [ ! $SIZEEXT ];then
  234.  SIZEEXT=0
  235. fi
  236. SIZEEXK=`expr $SIZEEXT + $SIZEEXT`
  237. SIZEMEG=`expr $SIZEEXK / 1024`
  238. SIZENEK=`expr $SIZEEXK + $SIZEFILK`
  239. SIZENEW=`expr $SIZENEK / 1024`
  240. if [ $SIZENEW -gt 640 ];then
  241.  ERRCD="ZZ"
  242.  MSG1="CD is full"
  243. fi
  244. FLAGLAST="no"
  245. if [ $SIZENEW -gt 600 ];then #a bit wasteful
  246.  FLAGLAST="yes"
  247. fi
  248. fi
  249. if [ $NUMTRACKS -eq 98 ];then
  250.  FLAGLAST="yes"
  251. fi
  252.  
  253. if [ "$ERRCD" ];then
  254. xmessage -bg "#ffc0c0" -center -name "pbkup" -title "Puppy backup to CD" -buttons \
  255.  Change_CD:13,Erase_CD-RW:12,EXIT:14 \
  256.  -file -<<FILETXT
  257. Puppy has looked at the CD-burner drive
  258. and reports this: "$MSG1"
  259.  
  260. Click "Change_CD" button.
  261.  
  262. However, if a CD-RW (ReWritable) is inserted,
  263. you may press "Erase_CD-RW" button to wipe it.
  264. FILETXT
  265.  
  266. XREPLY=$?
  267. else
  268.  
  269. xmessage -bg "#e0e0ff" -center -name "pbkup" -title "Puppy backup to CD" -buttons \
  270.  Change_CD:13,Erase_CD-RW:12,Burn_track:10,EXIT:14 \
  271.  -file -<<FILETXT
  272. Puppy has looked at the CD-burner drive
  273. and reports this: "$MSG1"
  274.  
  275. Therefore, Puppy has created an $MSG2 track,
  276. size in Kbytes and filename: 
  277. $SIZEFILE
  278.  
  279. If you have a CD-RW (ReWritable) and want to
  280. erase it first, click the "Erase_CD-RW" button.
  281.  
  282. To burn this file to CD, click on "Burn_track"
  283. button.
  284.  
  285. Note that the same CD can be reused for backups,
  286. and each backup is a "track" and will appear on
  287. the CD as a folder, named by date.
  288. The final track, when the CD is full, will close
  289. the CD to prevent further writing.
  290.  
  291. The number of tracks already written:   $NUMTRACKS
  292. The size written to CD so far (Mbytes): $SIZEMEG
  293. (note, max allowed on one CD is 99 tracks,
  294.  or size 650M, whichever comes first)
  295. FILETXT
  296.  
  297. XREPLY=$?
  298. fi
  299.  
  300. #echo -e "CD-R BURN RECORD\n" > /tmp/cdrlog.txt
  301. if [ $XREPLY -eq 13 ];then #change cd
  302.  xmessage -center -title "Puppy backup to CD" "Press Eject button on drive and insert another CD then click OKAY"
  303. else
  304. if [ $XREPLY -eq 12 ];then #erase cd
  305.  #MSG3="`cdrecord speed=4 blank=fast dev=$CDR`"
  306.  #rxvt -bg "#e0c0ff" -e "cdrecord -v speed=4 blank=fast dev=$CDR"
  307.  cdrecord -v speed=4 blank=fast dev=$CDR > /tmp/cdrlog.txt 2>&1 &
  308. else
  309. if [ $XREPLY -eq 10 ];then #burn track
  310.  if [ "$FLAGLAST" = "no" ];then
  311.  cdrecord -v -eject -multi speed=4 dev=${CDR} -data ${FILE} > /tmp/cdrlog.txt 2>&1 &
  312.  else
  313.  cdrecord -v -eject speed=4 dev=${CDR} -data ${FILE} > /tmp/cdrlog.txt 2>&1 &
  314.  fi
  315. else
  316.  break
  317. fi
  318. fi
  319. fi
  320.  
  321. #Xdialog --title "Writing to CD..." --fixed-font --no-cancel --smooth --logbox /tmp/cdrlog.txt 18 75
  322.  
  323. #this displays cdrecord tail...
  324. if [ ! $XREPLY -eq 13 ];then #change cd
  325. /usr/local/bin/rxvt -bg "#e0e0ff" -e /tmp/bkupx.sh
  326. fi
  327.  
  328. if [ ! $XREPLY -eq 12 ];then #erase cd
  329. if [ ! $XREPLY -eq 13 ];then #change cd
  330. break
  331. fi
  332. fi
  333. done
  334.  
  335. sync
  336. rm -f /root/tmp/*
  337. rm -f /tmp/cdrerr.txt
  338. rm -f /tmp/cdrlog.txt
  339. rm -f /tmp/bkupx.sh
  340. rm -f $LOG $LIST2
  341.