Friday, August 22, 2008

v 1.4b Sys. Back-Up/Restore & Clean-Up Script - Windows Too!

Greetings,

For those of you that have been looking for a simple, but easy to use back-up routine, this might be your answer. I had not found a utility that met my needs, that I liked, so I wrote my own little bash script to do what I needed.

I tried to keep it simple so it could be easily understood and modified, but still do the things I wanted.

If you are willing to spend a few min. to configure it to your system, it may be a solution for you too.

I have been using it for a several weeks without issue. Hopefully it doesn't have too many bugs left in it. :-)

Copy the contents of the code box below and paste into an editor.

There are detailed instructions in the file on how to set it up and run it.

EDIT:
At the request of several people I upgraded the script to also selectively back-up and restore your Windows install and/or your Windows Recovery partition and give more options.

Main Features:

1.) Version 1.4b does a better job of initially cleaning up a Linux system.

2.) I fixed some of the menus, and did some code clean-up to make it easier to understand how it works.

3.) The script now has the ability to selectively back-up and/or restore your Windows partition, C:drive, or another Linux install.

4.) The script now has the ability to selectively back-up and/or restore your Windows Recovery partition or another /home partition.

5.) The script now backs-up most of the files in your Linux install and gives you the ability to restore the whole system back to the state it was in at the last back-up if you break it. This can be done with one selection or you can sort through the folders and only restore piece meal if you prefer. It should be about 85% effective at fixing the system.

6.) It isn't recommended, but you can now choose to skip the clean-up portion of the script if you need to for some reason.

7.) You can back-up to or restore from an internal or external drive partition.

8.) The script is not DE specific. It can be used with KDE or Gnome.

9.) The restore portion of the script can be run independently from the back-up schedule selected.

10.) I clarified some of the instructions in the set-up section to make it more complete and easier to understand.

11.) You can now select to have the script make the back-up folders of your choice on your back-up partition for you.

12.) The script uses rsync for fast and efficient back-up / restore of large data stores.

bakclean version 1.4b

#!/bin/sh

# Written in May of 2008: by - Fred - bakclean version 1.4b

# Any code that I can legally lay claim to in this script
# is released under a dual license. GPL version 2 OR GPL
# version 3. Any derivative code must also be released on
# the same above dual licensing terms.

# I know it is silly to have to do this for a simple little
# script, but these days you just never can tell what might
# happen.

# The clean-up portion of this script removes unused files,
# and other cruft, that collects on your system over time.
# This cruft not only takes up space but can cause other
# system problems. You can choose to not use it below but
# I recommend you leave it active unless you need to keep
# your package download stores intact for later use.

# NOTE!: This script is set up for a single user account.
# Don't attempt to use it if you have multiple user
# accounts. Contact me if you need to be able to deal with
# multiple user accounts. Tell me what your needs are. If
# there is enough interest I will modify it.

# The back-up portion of this script provides for back-up
# and selective restore of your /home data files, your
# /home config files, and most of your Linux system files.
# If you choose to back-up your Linux system files you will
# have the ability to put your system back like it was when
# you last backed it up. This system restore function
# should be about 85% effective in fixing a broken system.

# It will also pick up your data partition files if they
# are mounted in your /home directory and you leave them
# mounted in the script. It also now provides the ability
# to selectively back-up and restore the C:drive of your
# Windows install and it's recovery partition, if you have
# one.

# The C:drive of your Windows install is where all your OS
# system files are stored, as well as your Windows data
# files if you don't have additional drives assigned. This
# too can be deselected.

# The Windows recovery partition makes it possible to
# recover a damaged Windows install. This is usually
# placed on the drive by the computer manufacturer and
# contains all the drivers specific to your hardware. This
# partition can also be deselected.

# This script uses rsync for back-up. This is a very
# efficient method, as it only transfers data that has
# been changed, added or deleted since the last back-up
# was done to maintain a mirror image. This makes
# subsequent back-ups fast and efficient. The same is true
# for the restore process.

# No compression is used so it is relatively easy to go to
# the back-up and retrieve individual files if needed.

# You must have "dialog" and "deborphan" installed to use
# this script. They will be automatically installed if not
# available on your system.

# I used portions of the below clean-up script. I modified
# and rewrote it to make it safer and more user friendly.
#
# Script basado en ubucleaner.
# Modificado por Mario Hidalgo | Inacap -Antofagasta
# -Chile | 24 de marzo del 2008

# Cut and paste this script into a text editor and call it
# "bakclean".

# Enter the config data about your system, below.

# Create a hidden folder in your /home folder called
# .ShellScripts :
# mkdir /home/*/.ShellScripts

# Save this script as /home/*/.ShellScripts/bakclean

# Set permissions with:
# chmod 755 /home/*/.ShellScripts/bakclean

# Copy to /usr/bin with:
# sudo cp -a /home/*/.ShellScripts/bakclean /usr/bin

# Execute manually by typing in a terminal: bakclean

# The first time you run this script, it may time-out,
# stop, and require you to re-enter your sudo password.
# This occurs if you have a lot of data to back-up and/or
# a slow computer. It only transfers data that has been
# added or changed since the last run, so it shouldn't
# be a problem after the initial clean-up and back-up
# cycles.

# Depending on your desktop, set this script to start in a
# terminal each time you boot, set it up on a kron tab, or
# use some other scheduling software.

# As an example: In KDE use the auto start module and use
# the start command: konsole -e bakclean

# If you have a partition you are using as a back-up
# partition that is mounted in your /home directory, make
# sure you also have an entry in /etc/fstab for a sdxx
# mount point in /media also. You should have two fstab
# entries for that partition. Example: One /etc/fstab
# entry for mount point /media/sdb2 and one for
# /home/fred/Backup.

# Correct the uncommented lines below for your system.
# Do not change anything after the next # # Begin - Begin
# line.

# Be careful entering your system config data below. Entry
# errors are not allowed. If you do this accurately you
# should have a nice clean-up, back-up and restore system.
# Otherwise you may have a mess.

# Change the folders mounted to other partitions below to
# indicate the data partitions and folders in your /home
# directory that you don't want backed up. You can exclude
# up to 5 folders/partitions from back-up. Always exclude
# the back-up folder if mounted in your /home. If you do
# not have data or back-up partitions mounted in your
# /home this doesn't apply to you. Put "no" in all 5
# positions.

fldr1="Backup"
fldr2="no"
fldr3="no"
fldr4="no"
fldr5="no"

# Set the mounting partitions that correspond to any data
# folders in your /home above, that are being excluded.
# If you do not have data or back-up partitions mounted in
# your /home this doesn't apply to you. Put "no" in all 5
# positions.

prtn1="sdb2"
prtn2="no"
prtn3="no"
prtn4="no"
prtn5="no"

# If you wish to disable the clean-up portion of this script,
# change the "no" entry for skip to "yes".

skip="no"

# If you have a Windows install or another Linux install
# you want to back-up, enter the partition designator, sdxx,
# of your other install below. Enter "no" if you don't have
# another install or you don't wish to back it up.

win="sda7"

# If you are backing up another install above and wish to
# back-up a recovery partition or a /home partition. Enter
# the partition designator below in the form "sdxx" or "no".

winr="sda8"

# Do you want to automatically back-up your another install
# every time the back-up script runs? Enter "yes" below for
# automatic or "no" to be given a choice each time, or if
# you aren't backing up another install.

winrun="yes"

# Do you want to automatically back-up your Windows Recovery
# partition or another /home partition every time the back-up
# script runs? Enter "yes" below for automatic or "no" to be
# given a choice each time, or if you aren't backing up
# Windows Recovery or another /home partition.

winrrun="yes"

# NOTE!: Make sure your Linux system has read/write access
# to your Windows partition and your Windows Recovery
# partition through /media/sdxx for both partitions before
# running the script and attempting to back-up or restore
# Windows or Windows Recovery.

# Set the 3 days of the week you wish to run the backup.
# I use Monday, Thursday, and Saturday. You can set all
# 3 to the same day if you only want to run it once a week.

bak1="Mon"
bak2="Thu"
bak3="Sat"

# If you want it to run anytime the script is called, put
# "anytime" in bak4 below. "stop" will prevent the back-up
# and clean-up portion of this script from running except
# on the 3 days a week specified above.

bak4="stop"

# The Restore portion of the routine will run whenever
# bakclean is run, regardless of the settings above.

# Create folders on your back-up partition to hold your
# back-up data and files. You can create a master folder
# to hold all of these folders if you want. Create folders
# on your back-up partition sdxx. Change them if you wish.
# Note! -- No spaces allowed in folder names. You must
# back-up and create folders for dataBF and configBF. Enter
# "none" for any folders you choose not to back-up. If you
# are not backing up Windows or Windows recovery, put "none"
# in those two entries also. I called mine:

dataBF="BackUpData"
configBF="BackUpConfig"
usrBF="BackUpUsr"
etcBF="BackUpEtc"
bootBF="BackUpBoot"
binBF="BackUpBin"
sbinBF="BackUpSbin"
libBF="BackUpLib"
devBF="BackUpDev"
varBF="BackUpVar"
winBF="BackDenRoot"; # winBF could be "WindowsXP";
winrBF="BackDenHome"; # winrBF could be "WinRecovery";

# You only have to create folders for dataBF and configBF.
# All the others are optional. If you don't wish to back
# other folders up enter "none" for each.

# Do you have or want a master back-up folder on your back-up
# partition that contains all the back-up folders above?
# If so, what is it's name? Enter "no" or "folder_name".

bakM="MasterBackUp"

# Would you like the script to make these back-up storage
# folders for you? Enter "yes" or "no" below.

makeBF="yes"

# Put your back-up partition designator in below.

bakP="sdb2"

# NOTE!: Make sure you have allowed enough space in your
# back-up partition to hold all the data you will be
# duplicating there.

# # Begin - Begin --- Don't change anything below this line.
#
# ==========================================
#
# # Begin - Begin --- Start of Restore Utility v 1.4b by Fred

day=$(date +%a)

RESPUESTA=$(dpkg --get-selections | grep -w gnome-terminal | grep -w install)
if [ "$RESPUESTA" != "" ]; then
pid=$(pidof gnome-terminal)
fi

RESPUESTA=$(dpkg --get-selections | grep -w konsole | grep -w install)
if [ "$RESPUESTA" != "" ]; then
pid=$(pidof konsole)
fi

# ||

RESPUESTA=$(dpkg --get-selections | grep -w dialog | grep -w install)
if [ "$RESPUESTA" = "" ]; then
sudo aptitude -y install dialog
fi

hm="/home/*"

umt1="$hm/$fldr1"
umt2="$hm/$fldr2"
umt3="$hm/$fldr3"
umt4="$hm/$fldr4"
umt5="$hm/$fldr5"

rmnt1="/dev/$prtn1 $hm/$fldr1"
rmnt2="/dev/$prtn2 $hm/$fldr2"
rmnt3="/dev/$prtn3 $hm/$fldr3"
rmnt4="/dev/$prtn4 $hm/$fldr4"
rmnt5="/dev/$prtn5 $hm/$fldr5"

if test $bakM != "no"; then
bakPP="$bakP/$bakM"
else
bakPP="$bakP"
fi

dataB="/media/$bakPP/$dataBF/$dataBF"
configB="/media/$bakPP/$configBF/$configBF"
usrB="/media/$bakPP/$usrBF/$usrBF"
etcB="/media/$bakPP/$etcBF/$etcBF"
bootB="/media/$bakPP/$bootBF/$bootbBF"
binB="/media/$bakPP/$binBF/$binBF"
sbinB="/media/$bakPP/$sbinBF/$sbinBF"
libB="/media/$bakPP/$libBF/$libBF"
devB="/media/$bakPP/$devBF/$devBF"
varB="/media/$bakPP/$varBF/$varBF"
winB="/media/$bakPP/$winBF/$winBF"
winrB="/media/$bakPP/$winrBF/$winrBF"

dialog --title "Alert! : Do you wish to continue with the Restore utility?" --backtitle "bakclean v 1.4b Script: by - Fred" --yesno "\n Do you wish to Restore your files?" 8 70
answer2=$?
sleep 1
if test $answer2 -gt 0; then
if test $day != $bak1; then
if test $day != $bak2; then
if test $day != $bak3; then
if test $bak4 != "anytime"; then
kill $pid
exit
fi; fi; fi; fi; fi

answer=1

if test $answer2 -gt 0; then
#
dialog --title "Alert! : Do you wish to continue with the Clean-Up and Back-Up utility?" --backtitle "bakclean v 1.4b Script: by - Fred" --yesno "\n Do you wish to Clean-Up and optionally Back-Up your files?" 8 70
answer=$?
sleep 1
fi

if test $answer -gt 0; then
if test $answer2 -gt 0; then
kill $pid
exit
fi; fi

if [ $USER != root ]; then
dialog --title "Be patient, Operations may take a while!" --backtitle "Back-Up / Restore & Clean-Up Utility v 1.4b cobbled together by - Fred" --infobox "
Enter sudo password, if asked, below. Restart bakclean." 5 75;
sudo -i
exit 0
fi

if test $prtn1 != "no"; then
sudo umount $umt1
fi

if test $prtn2 != "no"; then
sudo umount $umt2
fi

if test $prtn3 != "no"; then
sudo umount $umt3
fi

if test $prtn4 != "no"; then
sudo umount $umt4
fi

if test $prtn5 != "no"; then
sudo umount $umt5
fi

# Select and Restore partitions/folders/files

Rall=1

if test $answer2 -lt 1; then
if test $devBF != "none"; then
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\nDo you wish to Restore all your backed-up Linux System files except your Data files at the same time." 8 80
Rall=$?
sleep 1
#
if test $Rall -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $configB/ $hm

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $usrB/ /usr

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $etcB/ /etc

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $bootB/ /boot

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $binB/ /bin

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $sbinB/ /sbin

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $libB/ /lib

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $devB/ /dev

sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $varB/ /var

fi; fi; fi

if test $answer2 -lt 1; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\nSelect Yes to Restore Data files to your /home.
No for the next choice or to exit." 8 80
Rdata=$?
sleep 1
#
if test $Rdata -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $dataB/ $hm
fi; fi

if test $answer2 -lt 1; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\nSelect Yes to Restore Config files to your /home. No for the next choice." 8 82
Rconfig=$?
sleep 1
#
if test $Rconfig -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $configB/ $hm
fi; fi; fi

if test $answer2 -lt 1; then
if test bootBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\nSelect Yes to Restore your /boot folder.
No for the next choice." 8 70
Rboot=$?
sleep 1
#
if test $Rboot -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $bootB/ /boot
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test etcBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /etc. No for the next choice." 8 70
Retc=$?
sleep 1
#
if test $Retc -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $etcB/ /etc
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test usrBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /usr. No for your next choice." 8 70
Rusr=$?
sleep 1
#
if test $Rusr -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $usrB/ /usr
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test binBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /bin. No for your next choice." 8 70
Rbin=$?
sleep 1
#
if test $Rbin -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $binB/ /bin
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test sbinBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /sbin. No for your next choice." 8 70
Rsbin=$?
sleep 1
#
if test $Rsbin -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $sbinB/ /sbin
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test libBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /lib. No for your next choice." 8 70
Rlib=$?
sleep 1
#
if test $Rlib -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $libB/ /lib
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test devBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /dev. No for your next choice." 8 70
Rdev=$?
sleep 1
#
if test $Rdev -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $devB/ /dev
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test varBF != "none"; then
if test $Rall -gt 0; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\n Select Yes to Restore /var. No for your next choice." 8 70
Rvar=$?
sleep 1
#
if test $Rvar -lt 1; then
#
sudo rsync -arE --progress --timeout=120 --include "*" --include "*/**" $varB/ /var
fi; fi; fi; fi

if test $answer2 -lt 1; then
if test $winBF != "none"; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\nSelect Yes to Restore Windows or another install. No for your next choice." 8 70
Rwin=$?
sleep 1
#
if test $Rwin -lt 1; then
#
sudo rsync -arE --progress --timeout=120 $winB/ /media/$win
fi; fi; fi

if test $answer2 -lt 1; then
if test $winrBF != "none"; then
#
dialog --title "Select files to Restore." --backtitle "Restore" --yesno "\nSelect Yes to Restore Windows Recoveryor another /home partition. No to Exit." 8 70
Rwinr=$?
sleep 1
#
if test $Rwinr -lt 1; then
#
sudo rsync -arE --progress --timeout=120 $winrB/ /media/$winr
fi; fi; fi

if test $prtn1 != "no"; then
sudo mount -a $rmnt1
fi

if test $prtn2 != "no"; then
sudo mount -a $rmnt2
fi

if test $prtn3 != "no"; then
sudo mount -a $rmnt3
fi

if test $prtn4 != "no"; then
sudo mount -a $rmnt4
fi

if test $prtn5 != "no"; then
sudo mount -a $rmnt5
fi

if test $answer2 -lt 1; then
dialog --title "Restore has successfully completed!" --backtitle "Restore" --infobox "
The files you have selected have been successfully Restored!" 5 65; sleep 10
kill $pid
exit
fi

# End of Restore Utility v 1.4b - by Fred
#
# ==========================================
#
# # Begin - Begin --- Start of Clean-Up Utility v 1.4b

if test $skip = "no"; then

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)

dialog --title "Clean-Up will be done first!" --backtitle "A clean system is more compact and trouble free." --infobox "
The Clean-Up routine will begin in 5 seconds!" 5 60; sleep 5

RESPUESTA=$(dpkg --get-selections | grep -w deborphan | grep -w install)
if [ "$RESPUESTA" = "" ]; then
sudo aptitude -y install deborphan
fi

sudo apt-get -y autoremove
aptitude -y autoclean
aptitude -y clean
sudo dpkg --configure -a

sudo aptitude update

sudo aptitude -y purge $OLDCONF
sudo aptitude -y purge $OLDKERNELS

RESPUESTA=$(dpkg --get-selections | grep -w deborphan | grep -w install)
if [ "$RESPUESTA" = "" ]; then
dialog --title "Deborphan failed to load properly." --backtitle "Deborphan" --infobox "
Restart the bakclean Script." 5 60;
exit 0
else
for i in `deborphan --guess-all`
do
paquetes="$paquetes $i"
done
echo $paquetes
read
echo "Please wait. I am working hard..."
sudo aptitude -y remove --purge $paquetes
fi

sudo apt-get -y autoremove
aptitude -y autoclean
aptitude -y clean

sudo aptitude -y purge $OLDCONF
sudo aptitude -y purge $OLDKERNELS

for a in $(seq 1 2); do
OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
for i in `deborphan --guess-all`
do
paquetes="$paquetes $i"
done
echo $paquetes
read
echo "Please wait. I am working hard..."
sudo aptitude -y remove --purge $paquetes

sudo apt-get -y autoremove
aptitude -y autoclean
aptitude -y clean

sudo aptitude -y purge $OLDCONF
sudo aptitude -y purge $OLDKERNELS
done

echo "Please wait. I am working hard..."

cd /usr
find . -type f -name "*~" -exec rm -f {} \;

cd /etc
find . -type f -name "*~" -exec rm -f {} \;

cd /boot
find . -type f -name "*~" -exec rm -f {} \;

cd $hm
find . -type f -name "*~" -exec rm -f {} \;

mkdir Clean-Out

# You can empty the trash of any data folders you have
# mounted in your /home by uncommenting however many
# lines you need below and putting the appropriate folder
# names in place of mine.

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Backup/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Backup/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Documents/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Documents/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Downloads/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Downloads/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Pictures/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Pictures/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Projects/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Projects/.Trash-0

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/.local/share/Trash

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ /root/.local/share/Trash

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ /trash

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ /.trash

rmdir Clean-Out

rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null

sudo apt-get -y autoremove
aptitude -y autoclean
sudo dpkg --configure -a
aptitude -y clean

dialog --title "Alert! : Continue with Back-Up utility?" --backtitle "Clean-Up has completed successfully!" --yesno "\n Do you wish to continue with the Back-Up process?" 8 70
answer=$?
#
if test $answer -gt 0; then
kill $pid
exit
fi

fi

#
# End of Clean-Up v 1.4b utility, originally by: Modificado
# por Mario Hidalgo | Inacap -Antofagasta -Chile - Modified
# and rewritten by Fred
#
# ===============================================
#
# Start of Back-Up Utility v 1.4b - by Fred
#

if test $skip = "yes"; then
dialog --title "Alert! : Continue with Back-Up utility?" --backtitle "You are now ready to Back-Up your system!" --yesno "\n Do you wish to continue with the Back-Up process?" 8 70
answer=$?
#
if test $answer -gt 0; then
kill $pid
exit
fi; fi

if test $makeBF = "yes"; then
if test $$bakM != "no"; then
sudo mkdir /media/$bakPP
fi; fi

if test $makeBF = "yes"; then
sudo mkdir /media/$bakPP/$dataBF
sudo mkdir /media/$bakPP/$configBF
if test $usrBF != "none"; then
sudo mkdir /media/$bakPP/$usrBF; fi
if test $etcBF != "none"; then
sudo mkdir /media/$bakPP/$etcBF; fi
if test $bootBF != "none"; then
sudo mkdir /media/$bakPP/$bootBF; fi
if test $binBF != "none"; then
sudo mkdir /media/$bakPP/$binBF; fi
if test $sbinBF != "none"; then
sudo mkdir /media/$bakPP/$sbinBF; fi
if test $libBF != "none"; then
sudo mkdir /media/$bakPP/$libBF; fi
if test $devBF != "none"; then
sudo mkdir /media/$bakPP/$devBF; fi
if test $varBF != "none"; then
sudo mkdir /media/$bakPP/$varBF; fi
if test $winBF != "none"; then
sudo mkdir /media/$bakPP/$winBF; fi
if test $winrBF != "none"; then
sudo mkdir /media/$bakPP/$winrBF; fi
fi

answer3=1

if test $win != "no"; then
if test $winrun = "no"; then
#
dialog --title "Select whether to Back-Up your Windows partition." --backtitle "Back-Up System" --yesno "\n Do you wish to back-up your Windows C:drive this time?" 8 70
answer3=$?
sleep 1
fi; fi

if test $winrun = "yes"; then
answer3=0
fi

answer4=1

if test $winr != "no"; then
if test $winrrun = "no"; then
#
dialog --title "Select whether to Back-Up your Windows Recovery partition." --backtitle "Back-Up System" --yesno "\n Do you wish to back-up your Windows Recovery partition this time?" 8 70
answer4=$?
fi; fi

if test $winrrun = "yes"; then
answer4=0
fi

if test $prtn1 != "no"; then
sudo umount $umt1
fi

if test $prtn2 != "no"; then
sudo umount $umt2
fi

if test $prtn3 != "no"; then
sudo umount $umt3
fi

if test $prtn4 != "no"; then
sudo umount $umt4
fi

if test $prtn5 != "no"; then
sudo umount $umt5
fi

# Back-Up Files/Folders

if test etcBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /etc/ $etcB
fi

if test bootBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /boot/ $bootB
fi

sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --exclude ".*" --exclude ".*/**" --include "*" --include "*/**" $hm/ $dataB

sudo rsync -arE --progress --timeout=120 --delete --exclude "*~" --include ".*" --include ".*/**" --exclude "*" $hm/ $configB

if test usrBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /usr/ $usrB
fi

if test binBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /bin/ $binB
fi

if test sbinBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /sbin/ $sbinB
fi

if test libBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /lib/ $libB
fi

if test devBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /dev/ $devB
fi

if test varBF != "none"; then
sudo rsync -arE --progress --delete --timeout=120 --exclude "*~" --include "*" --include "*/**" /var/ $varB
fi

if test $answer3 -lt 1; then
if test $win != "no"; then
sudo rsync -arE --progress --delete --timeout=120 /media/$win/ $winB
fi; fi

if test $answer4 -lt 1; then
if test $winr != "no"; then
sudo rsync -arE --progress --delete --timeout=120 /media/$winr/ $winrB
fi; fi

if test $prtn1 != "no"; then
sudo mount -a $rmnt1
fi

if test $prtn2 != "no"; then
sudo mount -a $rmnt2
fi

if test $prtn3 != "no"; then
sudo mount -a $rmnt3
fi

if test $prtn4 != "no"; then
sudo mount -a $rmnt4
fi

if test $prtn5 != "no"; then
sudo mount -a $rmnt5
fi

dialog --title "Back-up has successfully completed!" --backtitle "bakclean v 1.4b by Fred" --infobox "
That wasn't so bad was it?" 5 60; sleep 10

kill $pid
exit
#
# End



I pulled the clean-up portion of the above script out and made it stand-alone. You can now use the below script to clean-up your system without having to go to the trouble of configuring the back-up script and having to click through the menus.

It turned out that it was a good move because I found a bug that prevented it from emptying the trash cans when very large files and folder trees were in them. I should have known not to use the rm command but I did and it choked on large amounts of data. I fixed it in the 1.4b version.

cleanout version 1.4b


#!/bin/sh

# Written in May of 2008: by - Fred - cleanout version 1.4b

# Any code that I can legally lay claim to in this script
# is released under a dual license. GPL version 2 OR GPL
# version 3. Any derivative code must also be released on
# the same above dual licensing terms.

# I know it is silly to have to do this for a simple little
# script, but these days you just never can tell what might
# happen.

# Cut and paste this script into a text editor and call it
# "cleanout".

# Create a hidden folder in your /home folder called
# .ShellScripts :
# mkdir /home/*/.ShellScripts

# Save this script as /home/*/.ShellScripts/cleanout

# Set permissions with:
# chmod 755 /home/*/.ShellScripts/cleanout

# Copy to /usr/bin with:
# sudo cp -a /home/*/.ShellScripts/cleanout /usr/bin

# Execute by typing in a terminal: cleanout

# This script uses "Deborphan" and "Dialog." These routines
# will be automaticly downloaded and installed if they are
# not availiable on your system.

# I used portions of the below clean-up script. I modified
# and rewrote it to make it safer and more user friendly.
#
# Script basado en ubucleaner.
# Modificado por Mario Hidalgo | Inacap -Antofagasta
# -Chile | 24 de marzo del 2008

# # Begin - Begin --- Don't change anything below this line.
#
# ==========================================
#
# # Begin - Begin --- Start of Clean-Out Utility

hm="/home/*"

if [ $USER != root ]; then
dialog --title "Be patient, Operations may take a while!" --backtitle "Clean-Out Utility v 1.4b cobbled together by - Fred" --infobox "
Enter sudo password, if asked, below. Restart cleanout." 5 75;
sudo -i
exit 0
fi

RESPUESTA=$(dpkg --get-selections | grep -w gnome-terminal | grep -w install)
if [ "$RESPUESTA" != "" ]; then
pid=$(pidof gnome-terminal)
fi

RESPUESTA=$(dpkg --get-selections | grep -w konsole | grep -w install)
if [ "$RESPUESTA" != "" ]; then
pid=$(pidof konsole)
fi

# ||

RESPUESTA=$(dpkg --get-selections | grep -w dialog | grep -w install)
if [ "$RESPUESTA" = "" ]; then
sudo aptitude -y install dialog
fi

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)

dialog --title "Clean-Out will now begin!" --backtitle "A clean system is more compact and trouble free." --infobox "
The Clean-Out routine will begin in 5 seconds!" 5 60; sleep 5

RESPUESTA=$(dpkg --get-selections | grep -w deborphan | grep -w install)
if [ "$RESPUESTA" = "" ]; then
sudo aptitude -y install deborphan
fi

sudo apt-get -y autoremove
aptitude -y autoclean
aptitude -y clean
sudo dpkg --configure -a

sudo aptitude update

sudo aptitude -y purge $OLDCONF
sudo aptitude -y purge $OLDKERNELS

RESPUESTA=$(dpkg --get-selections | grep -w deborphan | grep -w install)
if [ "$RESPUESTA" = "" ]; then
dialog --title "Deborphan failed to load properly." --backtitle "Deborphan" --infobox "
Restart the cleanout Script." 5 60;
exit 0
else
for i in `deborphan --guess-all`
do
paquetes="$paquetes $i"
done
echo $paquetes
read
echo "Please wait. I am working hard..."
sudo aptitude -y remove --purge $paquetes
fi

sudo apt-get -y autoremove
aptitude -y autoclean
aptitude -y clean

sudo aptitude -y purge $OLDCONF
sudo aptitude -y purge $OLDKERNELS

for a in $(seq 1 3); do
OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
for i in `deborphan --guess-all`
do
paquetes="$paquetes $i"
done
echo $paquetes
read
echo "Please wait. I am working hard..."
sudo aptitude -y remove --purge $paquetes

sudo apt-get -y autoremove
aptitude -y autoclean
aptitude -y clean

sudo aptitude -y purge $OLDCONF
sudo aptitude -y purge $OLDKERNELS
done

echo "Please wait. I am working hard..."

cd /usr
find . -type f -name "*~" -exec rm -f {} \;

cd /etc
find . -type f -name "*~" -exec rm -f {} \;

cd /boot
find . -type f -name "*~" -exec rm -f {} \;

cd $hm
find . -type f -name "*~" -exec rm -f {} \;

mkdir Clean-Out

# You can empty the trash of any data folders you have
# mounted in your /home by uncommenting however many
# lines you need below and putting the appropriate folder
# names in place of mine.

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Backup/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Backup/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Documents/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Documents/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Downloads/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Downloads/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Pictures/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Pictures/.Trash-0

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Projects/.Trash-1000

# sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/Projects/.Trash-0

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ $hm/.local/share/Trash

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ /root/.local/share/Trash

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ /trash

sudo rsync -r --progress --delete --timeout=120 $hm/Clean-Out/ /.trash

rmdir Clean-Out

rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null

sudo apt-get -y autoremove
aptitude -y autoclean
sudo dpkg --configure -a
aptitude -y clean

dialog --title "Clean-Out has successfully completed!" --backtitle "cleanout v 1.4a by Fred" --infobox "
That wasn't so bad was it?" 5 60; sleep 10

kill $pid
exit
#
# End


Any suggestions or feed-back you are willing to give me, good or bad, will be greatly appreciated.

Enjoy,

Fred

EDIT: Make sure the partition you are using for backup, whether internal or external, is large enough to hold all the data you will be duplicating there.

1 comment:

Anonymous said...

Thank You Fred,

Very Nice Clean-Out utility!

I haven't tried the Backup portion yet but have little doubt that, once customized to my system, it will save a great deal of time and perform an ideal backup.

I've learned a great deal from just reading your bash script, it works flawlessly in Mint 8, Gnome ... I bet it's great for Karmic as well, I'll give that a try later.

Thanks for sharing this gem!
It's just what the doctor ordered for my fresh Mint install.

Dave