#!/bin/bash # author sudodus alias nio-wiklund at launchpad # # date editor comment # 2015-04-16 sudodus Created #!/bin/bash # author sudodus alias nio-wiklund at launchpad # # date editor comment # 2013-11-08 sudodus upgraded mktbl (make tarball) # 2013-11-08 sudodus xz compression default # 2013-11-08 sudodus parameter for tarball name # 2013-11-08 sudodus own directory for tarballs (plus symlink) # 2014-02-21 sudodus explanation why xz compression in usage text # 2014-09-02 israeldahl added the ability to specify tarball destination # 2015-04-16 sudodus path in filename # 2015-04-16 sudodus inline mktbl 'imktbl' in zmktbl # Global variables partitions="/usr/share/OBI/partitions" prtn="/dev/sda1" cmpr="xz" myid=$(who|grep -m1 -e ' tty'|cut -d ' ' -f1) if [ "$myid" != "" ] && [ "$myid" != "root" ] && test -d /home/"$myid" then home=/home/"$myid" else myid=$(who|grep -m1 -e ' pts/'|cut -d ' ' -f1) if [ "$myid" != "" ] && [ "$myid" != "root" ] && test -d /home/"$myid" then home=/home/"$myid" else home="$HOME" fi fi trgt="$home/ball" function mkmaster { # Write in $HOME and move the tarball to /tarballs if it is writable. mkdir -p "/usr/share/OBI" cd "/usr/share/OBI" echo "$prtn" > "$partitions" ans= while [ "$ans" != "q" ] && [ "$ans" != "x" ] do ans=$(zenity --list --width=760 --height=320 \ --title="zMktbl - make tarball master menu" --cancel-label="Quit" \ --window-icon="/usr/share/icons/zmktbl.png" \ --text="Select an action (a line) from the list below or use the current value" \ --column="Hotkey:" --column="Action: Change ..." --column="Current value" \ "s" "Source partition" "$prtn" \ "c" "Compression, gzip or xz" "$cmpr" \ "t" "Target file name with path " "$trgt" \ "q" "Quit" "" 2> /dev/null ) if [ $? -ne 0 ] then return elif [ "$ans" == "q" ] then return fi ans=${ans:0:1} # fix for buggy(?) zenity --list in trusty # echo "xxx $ans xxx" if [ "$ans" == "s" ] then /usr/share/OBI/select-part2 root mktbl # dialog # save the selected partition (file and static variable) read prtn < "$partitions" echo mkmaster cat "$partitions" pwd ls -l "$partitions" elif [ "$ans" == "c" ] then select-compression # zenity --list --radiolist # save the selected compression (static variable) elif [ "$ans" == "t" ] then select-target-file # zenity --file-selection # save the selected file (static variable) elif [ "$ans" == "" ] then ans="x" else echo "Error in mk-master" fi done # call inline mktbl imktbl imktbl } ####################################################################### function select-compression { ans=$(zenity --list --width=640 --height=200 \ --title="zMktbl - select compression" --cancel-label="Quit" \ --window-icon="/usr/share/icons/zmktbl.png" \ --text="Click the radio button to select compression" \ --radiolist \ --separator=" " \ --print-column="2" \ --column="" --column="Compression" \ "TRUE" "xz - make a 'tar.xz' file" \ "FALSE" "gzip - make a 'tar.gz' file" 2> /dev/null ) if [ $? -ne 0 ] then return fi echo "$ans" if [ "${ans:0:2}" == "xz" ] then cmpr="xz" elif [ "${ans:0:4}" == "gzip" ] then cmpr="gzip" else echo "Error in select-compression" fi } ####################################################################### function select-target-file { ans=$(zenity --file-selection --title "zMktbl - Select target file" \ --window-icon="/usr/share/icons/zmktbl.png" \ --confirm-overwrite --filename="$trgt" --save \ --file-filter=*.{tar*,iso,img,img.?z,tar.?z} 2> /dev/null ) echo "$ans" trgt=${ans/.tar.[gx]z} } ####################################################################### function imktbl { # inline mktbl imktbl for a smoother GUI experience #echo "*** imktbl: $prtn $cmpr $trgt ***" inversvid="\0033[7m" resetvid="\0033[0m" compr="J" ext="xz" tarpath="${trgt%/*}" if [ "$cmpr" == "gzip" ] ||[ "$cmpr" == "z" ] then compr="z" ext="gz" fi /bin/echo -e "${inversvid}These main commands are prepared:${resetvid} mount $prtn /mnt cd /mnt tar -cv${compr}f ${trgt}.tar.$ext ." echo \ "Run sudo -H $0 to make a tarball of the files in $prtn You may need to remove some existing tarballs, '*.tar.gz' and '*.tar.xz' from the tarballs directory to make space for the new tarball These main commands are prepared: mount $prtn /mnt cd /mnt tar -cv${compr}f ${trgt}.tar.$ext ." | zenity --text-info --text="/dev/stdin" \ --width=640 --height=320 \ --title="zMktbl - Do you want to make this tarball?" --cancel-label="Quit" \ --window-icon="/usr/share/icons/zmktbl.png" \ --checkbox="Do you want to make this tarball?" 2> /dev/null if [ "$?" -eq 0 ] then # Remove old tarballs the create space for new tarballs echo "----------------------------------------------------------" str=$(df -h .) str="$str There should be enough space for the new tarball" ls "${tarpath}"* > /dev/null 2> /dev/null if [ $? -eq 0 ] then str="$str Remove some old tarball(s) only if necessary, In that case, please continue the dialogue in the zMktabl text console" echo "$str" | zenity --text-info --text="/dev/stdin" \ --width=640 --height=320 \ --title="zMktbl - Remove some old tarball?" \ --cancel-label="No, continue without removing any tarball" --ok-label="Yes, remove some old tarball" \ --window-icon="/usr/share/icons/zmktbl.png" \ --checkbox="Check this box if you want to remove some old tarball" 2> /dev/null if [ "$?" -eq 0 ] then declare -a obls=( $(ls -1 "${tarpath}"/*.tar.[gx]z|sed -e "s/\ /.'./"g -e 's/^/FALSE /') ) ans=$(zenity --list --width=640 --height=560 \ --title="zMktbl - remove old tarballs" --cancel-label="Quit" \ --window-icon="/usr/share/icons/zmktbl.png" \ --text="Check the box to select tarballs to remove. (spaces are replaced with .'. in the list)" \ --checklist \ --separator=" " \ --print-column="2" \ --column="" --column="Old tarballs" \ --checkbox="Check this box if you want to remove these old tarballs" \ ${obls[@]} 2> /dev/null 2> /dev/null ) if [ "$ans" != "" ] then echo "rm $ans" rm ${ans//.\'./ } fi fi fi # final warning zenity --question --text="FINAL WARNING Do you want to make this tarball? $trgt.tar.$ext" 2> /dev/null if [ "$?" -ne 0 ] then exit fi # mount the source partition and clean log files mount "$prtn" /mnt cd /mnt find /mnt/var/log -type f -exec echo "> {}" \;>doer bash doer rm doer # create the tarball echo tar -cv"${compr}"f "${trgt}.tar.$ext" . tar -cv"${compr}"f "${trgt}.tar.$ext" . echo "$trgt written, syncing now ..." sync cd echo "----------------------------------------------------------" echo "List tarballs at ${tarpath}" ls "${tarpath}"/*.tar.[gx]z umount /mnt read -p "Check the result and press Enter to continue" fi } ####################################################################### # # zmktbl main program # ####################################################################### if [ "$(whoami)" == "root" ] && [ $# -eq 0 ] then mkmaster else echo "Usage: sudo -H $0 # no parameters" fi