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.
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.
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.
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.
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