Monday, March 7, 2011

Comic strip in Kubuntu

This was a fun experiment. I read the dilbert comics using the comic strip widget in Kubuntu. The comics started from 01-01-1994 on the widget while on the website, the first one was on 16-04-1989. Well, I cannot just sit back and miss on the 5 years of comics now, can I??

To change the start date, you will need to modify the start date in ~/.kde/share/apps/plasma/comics/dilbert/contents/code/main.es file.

Open the file in any text editor and search for 1994. The line should be something like -
comic.firstIdentifier = "1994-01-01"
just change it to
comic.firstIdentifier = "1989-04-16"
to get the comics from 1989 onwards. Once you make the change, right click on the widget and click on the "Jump to first strip" option to get the first strip from 1989. Enjoy.

Sunday, February 6, 2011

Setting up periodic mysql backup

I had to setup periodic mysql backups on a ubuntu server recently. Posting the steps here as a guide.

The basic command to take a mysql dump is simple -
mysqldump -u -p > filename.sql

For e.g., if you have your password for root user of mysql is rootpassword and the database that you want to backup is sampledb, you would use -
mysqldump -uroot -prootpassword sampledb > filename.sql

My problem was a little different since I wanted to take the backups by date. I used something like -
mysqldump -uroot -prootpassword sampledb > db_dump_`date '+%d_%m_%y'`.sql

I simply added a date '+%d_%m_%y' in the file name. This command displays the date in dd_mm_yyyy format.

Next step was to deciding how many days worth backup to keep. I decided to keep backups for last 31 days in my case. This would mean that the older backups would be removed if their age exceeds 31 days.

Note that next step we are going to remove files older than 31 days. This being a destructive command, you should take care to run in inside a folder where you know the files are only backups. In my case, I save all my database backups into a separate folder and run this command there.

In unix, you can search all files older than a given age by using the find command. For e.g.
find ~/dumps/ -type f -mtime +31
This command will find all the files under dumps folder in home which were modified more than 31 days ago. I am assuming here that your are not changing anything manually in the file after they have been created by mysqldump.

The above command just did the list but we need a way to delete the found files. There is another option for find that can help us here.
find ~/dumps/ -type f -mtime +31 -exec rm {} \;
The above command will find and delete all files from the dumps folder under your home which are older than 31 days. The exec option tells find to execute the next command (rm in this case) on the searches.

The next step was to create a script so that I need to use only one command to take the backup and remove the older files. If you are in (k)ubuntu, you can create a bin folder under your home and put all your scripts there. This will put the script in the default path for your commands.

Then you can create a backup.sh file and put the commands in it. Make sure to give execute permissions for the backup.sh file.

The final step is to create a cron job to automate the whole process. For this enter crontab -e on the command line. It should open up an editor to enter your cron settings.

I used the following in my crontab
1 0 * * * ~/bin/backup.sh
This tells the cron to run the backup script every night at 12:01 AM.