Joey’s Linux Scams

Publishing Firefox bookmarks to the web in Linux

Posted by: Joey on: August 30, 2006

Here is a little bash script I wrote that takes your Firefox bookmarks and uploads them to your web space.

Tools

For this project you will be using the bash shell and various command line operations. And of course you will need Firefox! Bash is usually the default Linux shell. This will probably work with other shells besides bash with little or no modification.

Caveats

Buyer beware! Whenever you create a script or use one from another source, always test it in a non-production environment to insure it will do no harm. Create some test files on which to run the script and break up complex commands so that you can test each part individually. When you feel confident that you are ready to implement the script, backup all data beforehand. And if you insist on running things as root or sudo, check out this horrid tale.

Method

The first thing to do is to find your bookmarks.html file. This is usually stored in your home directory structure. On my Ubuntu system it is stored at

/home/username/.mozilla/firefox/xxxxxxxx.default/bookmarks.html

where xxxxxxxx is a random set of letters and numbers that is different for each user.

Our next task is to create some directories. I like to store my scripts in /home/username/scripts, but you can store them where ever you wish. For this script we need to create two directories, backups and logs. You can create these directories anywhere you wish, but I created mine like so.

mkdir /home/username/logs
mkdir /home/username/.mozilla/firefox/xxxxxxxx.default/backups

Now we are ready to create the script.

1  #!/bin/sh
2  #move to bookmark directory
3  cd /home/username/.mozilla/firefox/xxxxxxxx.default/
4  #create backup file just in case, backups directory must already exist
5  cp bookmarks.html backups/bookmarks.`date +%m-%d-%y_%H%M`.html
6  #append date to bottom of file
7  echo "`date`" >> bookmarks.html
8  #up the results
9  ftp -in <<EOF
10 open yoursite.com
11 user username password
12 bin
13 cd public_html
14 cd files
15 put bookmarks.html
16 bye
17 EOF
18 #remove date line from end of file
19 echo "`sed '$d' bookmarks.html`" > bookmarks.html
20 #remove old backup files
21 find backups -type f -mtime +8 -exec rm {} ;
22 #move to log file directory
23 cd /home/username/logs/
24 #create log file
25 echo "Your bookmarks were last updated on `date`" > bookmark_backup.log

I’ve commented the file, but I’ll go through the script line by line and talk about what is going on. Bash comments start with the # symbol. I recommend that you comment every script or document you write. Comments not only help other people who are looking at your work, they also help you. Sometimes what seems so clear now, may not seem clear three years down the road. I have also added line numbers for clarity of discussion and will include a non-numbered copy at the bottom of this post.

  • Line 1: Every bash script starts with this line or something similar like #!/bin/bash.
  • Line 3: Move to the directory where bookmarks.html is stored using the cd command. It makes things easier because I can use relative paths.
  • Line 5: Make a backup copy of the bookmarks.html file in the backups directory using the cp command. I name the backup file with the current date using backticks around the date command. The backtick is usually on the key with the tilda (~). Check the date man page for more info on formating a date string.
  • Line 7: Append (>>) the default date string to the end of the bookmarks.html file.
  • Line 9: This line tells bash to start an ftp sesson with no interactive prompting and no auto-login. <<EOF tells bash to hang on a second while we enter more ftp commands. Bash will start executing the commands entered once it sees EOF again on a line by itself.
  • Line 10: Open an FTP session on yoursite.com.
  • Line 11: Log in as username with password. Note here that your password will be stored in plain text, this is not very secure so be careful.
  • Line 12: Put ftp in binary file transfer mode. This helps insure the file gets uploaded correctly.
  • Lines 13 and 14: Change to the directory where you want to put the file. You’ll need to create these directories ahead of time. I find this works best if you only do one directory at a time. If you don’t want your document publicly displayed, store it outside the public_html directory.
  • Line 15: Upload the bookmarks.html file to the server with the put command.
  • Line 16: Close the ftp session with the bye command.
  • Line 17: This is the end of the EOF statement I was mentioning earlier. Bash will now execute all the commands between the two EOFs.
  • Line 19: On line 7 we added a date line to the bottom of the file, now we remove it to put it back like it was. We use sed for this. See the sed man page for more information on its usage.
  • Line 21: Using the find command we find all backups from line 5 that are older than 8 days and remove them. Don’t forget to do this or you’ll end up with hundreds of backup files taking up space.
  • Line 23: Change to the logs directory.
  • Line 25: Update the log file for this script to let us know the script completed successfully.

Conclusion

You should now be able to access the results by going to http://yoursite.com/files/bookmarks.html (or where ever you put it).

For true scripty goodness, create a cron job that automagically runs this script. I run mine twice a day at 9:53 and 21:53. Here is my crontab -l for your reference.

53 9,21 * * * sh /home/username/scripts/bookmark_backup.script

Now as promised, the script with no numbers.

#!/bin/sh
#move to bookmark directory
cd /home/username/.mozilla/firefox/xxxxxxxx.default/
#create backup file just in case, backups directory must already exist
cp bookmarks.html backups/bookmarks.`date +%m-%d-%y_%H%M`.html
#append date to bottom of file
echo "`date`" >> bookmarks.html
#up the results
ftp -in <<EOF
open yoursite.com
user username password
bin
cd public_html
cd files
put bookmarks.html
bye
EOF
#remove date line from end of file
echo "`sed '$d' bookmarks.html`" > bookmarks.html
#remove old backup files
find backups -type f -mtime +8 -exec rm {} ;
#move to log file directory
cd /home/username/logs/
#create log file
echo "Your bookmarks were last updated on `date`" > bookmark_backup.log

Leave a Reply