Jun272008
Trying to prevent myself from another home fiasco, I’ve decided to build a simple script to backup some of my directories. There are a lot of scripts / backup tools out there, but I just wanted to build my own for the fun of it, and since what I really wanted was to create a tar.gz of the contents of a specific directory (no root path), including hidden files, and without the need to support incremental backups. Take a look at the script:
#!/bin/bash
function showHelp() {
if [ "$1" != "" ]; then
echo "ERROR: $1"
echo ""
fi
echo "Usage: $0 <folder> <file.tar.gz>"
echo "where:"
echo " <folder> folder to backup"
echo " <file.tar.gz> where to save the backup (.tar.gz file)"
}
if [ $# -ne 2 ]; then
showHelp
elif [ ! -d "$1" ]; then
showHelp "Directory $1 does not exist"
exit 1
else
echo -n "Creating $2 from $1: "
if [ -f "$2" ]; then
mv "$2" "$2.previous"
fi
tar -cpzf "$2" --exclude="$2" --ignore-failed-read --transform="s/"`echo "$1" | sed -e 's/^\\///g' | sed -e 's/\\/$//g' | sed s/\\\//'\\\\'\\\//g`"//" "$1" &>/dev/null
if [ $? -ne 0 ]; then
echo "ERROR"
exit 1
else
echo "DONE"
if [ -f "$2.previous" ]; then
rm -f "$2.previous"
fi
fi
fi
The line that probably looks scary is:
tar -cpzf "$2" --exclude="$2" --ignore-failed-read --transform="s/"`echo "$1" | sed -e 's/^\\///g' | sed -e 's/\\/$//g' | sed s/\\\//'\\\\'\\\//g`"//" "$1" &>/dev/null
The complicated part comes from the transform expression, which is used to get rid of the absolute path to the directory we are backing up. The tar –directory option here is not useful since it adds the “.” special path to the packaged file, so let’s break it down:
echo "$1": the folder to backup
sed -e 's/^\\///g': remove starting slash, if any
sed -e 's/\\/$//g': remove trailing slash, if any
sed s/\\\//'\\\\'\\\//g`: convert forward slash to escaped slashes (so every / is replaced by \/)
Jun172008
I should’ve probably entitled this post “Learn from your mistakes”. Yesterday I was getting rid of the Windows partition in favor of a VMWare installation of Windows XP on my Ubuntu Hardy Heron (8.04) so I decided it was time to reclaim the space used by Windows and use it for my home partition. No biggie, I did everything right, and fast. Wait, so fast that I didn’t notice a big boo-boo:
$ rm -fR /var/backups/home/mariano; rm -fR /home/mariano.old; rm -fR /home/mariano
“You did what??” You ask.. Yup, that’s right, I wiped not only my Home directory, but every single backup copy I had of it. Granted, I didn’t have *much* on my home folder (except my evolution mail folders and some other documents), but I still wanted to shoot my hands for insisting in adding -fR to my rm commands (-fR means: delete recursively, and no, do not ask me for confirmation.)
So after trying without any luck to recover the deleted inodes, I’ve successfully re-setup my home folder starting fresh, which wasn’t so bad. In order not to mess up again, I’ve decided to add this little alias to my ~/.bashrc file:
# alias to make del send files to trash
alias del='mv -t ~/.local/share/Trash/files --backup=t'
So from now on, I just need to remember to use “del” instead of “rm”, like so:
$ del example.txt
The above command would send example.txt to the trash, instead of wiping it out from the file system. You can later on decide to Empty the trash in your Desktop, or undelete your files. The del alias also works for folders.
Jun092008
After setting up Ubuntu Hardy 64 bits (also known as AMD64) on my PC I had to do what every developer needs to do every now and then: set up my working environment. This is not a hard thing to do on Ubuntu, particularly if you choose to go with precompiled packages, installing them through the friendly Synaptic Package Manager. However, I do like to keep a tight control on what is the base of my day to day work, so instead of using the built-in packages, I chose to build them from source. Once again, this is not hard, but it can bring some complications for 64 bits environments. As I may probably need to do this once again in the future, I’ve decided to blog this guide on how to install the following packages on Ubuntu 8.04 64 bits: Apache 2 (with SSL support), PHP5 with xDebug, MySQL 5, Python through mod_python, SVN (client and server), and Trac integrated with SVN.
Continue Reading »
May282008
As I recently moved to Ubuntu 8.04 (plus CompizFusion + AWN which is rocking my world), I am now back to the good old days where bash was there to save my life. So basically I needed to SVN rename (also known as move) all my thtml views to ctp, recursively. Since this is a short tip, I’ll get down to the details. Just cd to your /views path in CakePHP, and issue:
for file in `find . -name "*.thtml"` ; do svn mv $file `echo $file | sed s/\.thtml/\.ctp/` ; done
After that you can safely commit. Enjoy!