Public (requires no auth) and Private Shares with Samba

I just found out how to do this and thought I better blog it straight away as I couldn’t believe how easy it was.

I always just ignored the fact that Samba always asked for a user name and password from a windows machine and just ensured users were added to Samba with the same credentials they use to log onto their windows machine.

This is pretty good practise anyway as it allows users to have a private share that they only see when they log onto Samba.

But its also good to have just a public share that doesn’t require background authentication and acts just like when you share a folder on windows.

So enough of me beating around the bush, the key is adding just one line to your ‘/etc/samba/smb.conf‘ file.

Just add this line to your [Global] section.

[sourcecode language=”sh”]

map to guest = bad user

[/sourcecode]

Then if you haven’t already ensure your public share is the following:

[sourcecode language=”sh”]

[Public Dump]
path = /public_dump
writeable = yes
browseable = yes
guest ok = yes

[/sourcecode]

Finally just restart Samba to make the changes take effect.

[sourcecode language=”sh”]

# service smb restart

[/sourcecode]

Thinking about it this makes perfect sense as windows always sends your user name and password in the background when you access a share, so when you try to access the Samba share without being in the Samba user table, Samba will map you as a guest user leaving you with just being able to view the public shares you have set up.

How to Install and Format a new Linux Hard Drive

Anything in Linux is awalys slightly more taxing but is not that hard once you know how.

So first up you need to know whether you are working with a IDE and PATA (formerly just ATA) drive or a SCSI, SATA and USB drive.

Linux refers to IDE and PATA drives with “hdx” and SCSI, SATA and USB drives with “sdx“. Usually, a number is also put at the end of “hdx” or “sdx” to denote different partitions on the same physical drive, but for the purpose of formatting, you only need to know which letter the drive you want to format is.

To see all the drives attached to your system use the following commands.

[sourcecode language=”sh”]ls /dev/hd*[/sourcecode]

or

[sourcecode language=”sh”]ls /dev/sd*[/sourcecode]

In my case I have installed a new SATA hard drive so I am working with ‘/dev/sdb

First we will use ‘fdisk‘ to erase any old partitions on the drive, if you have a fresh new hard drive you wont need to do this.

[sourcecode language=”sh”]fdisk /dev/sdb[/sourcecode]

Now we can type ‘p‘ to see a partition table of the drive. The first line of output from the “p” command will also tell you the size of the drive. This is a good way to double-check that you are working with the correct drive.

To delete any existing partitions, press “d” and then “Enter.” It will ask you which partition number you wish to delete. The number of the partition is the number that follows ‘sdb‘. You can always view the partition table again with the “p” command.

Type “n” and hit “Enter.” Then press “p” to create a primary partition. It asks you for a partition number; enter “1” Now you are asked which cylinder the partition should start at. The beginning of the drive is the default, so just hit “Enter.” Then, you are asked for the last cylinder. The end of the drive is the default, so you can just press “Enter” again.

Now you are back at fdisk’s command prompt. Use the “p” command to check the partition table. You should now see your new partition at the bottom of the output. In the example, it lists as “/dev/sdb1

You now need to set the filesystem type for your new partition with the “t” command. You are asked for the Hex code of the file system you wish to use. We will use the standard Linux ext2 file system, which is “83

Now just issue the “w” command to write your new partition table and exit ‘fdisk

Finally we need to create the file system on the drive. We use the ‘mkfs‘ command.

[sourcecode language=”sh”]mkfs -t ext2 /dev/sdb1[/sourcecode]

Its good practise to now run a file system check on the new drive, we do this with the following command.

[sourcecode language=”sh”]fsck -f -y /dev/sdb1[/sourcecode]

Last but not least we need to mount the drive at boot. We do this by adding an entry into the ‘/etc/fstab‘ file.

[sourcecode language=”sh”]vi /etc/fstab[/sourcecode]

In my case the I want to mount the drive at ‘/meat‘ so we enter the follwing line in the ‘fstab‘ file.

[sourcecode language=”sh”]/dev/sdb1          /meat          ext2          defaults          0 0[/sourcecode]

Thanks to http://www.ehow.com/how_1000631_hard-drive-linux.html

Custom Backup Tool with Robocopy

Creating backups is an essential task people take for granted. I have lost count the amount of times people have said they lost all their data and I ask ‘did you have a backup?’…. Erm no.

So windows vista does come with its own backup tool, but it is somewhat primitive in features. However if you know a little bit about windows command line and scheduled tasks then ‘robocopy‘ is what you need.

Here is my command I run to back up all my personal data.

[sourcecode language=”sh”]
robocopy F:\Alex’s Z:\BACKUP\Alex’s /MIR /XA:SH /R:5 /V /NP /ETA /LOG:Backup_alexs.log
[/sourcecode]

First of all you choose what folder you want to backup, in this case its ‘F:\Alex’s‘ then you choose where you want to back it up to. In my case I have a mapped network drive to a Linux Samba file share on another computer ‘Z:\BACKUP\Alex’s

Note that ‘robocopy‘ copies the contents of the folder and does not include the top folder name. So you have to write ‘Alex’s‘ again in your backup location in order to copy the exact file structure or you will just get the contents of ‘Alex’s‘ spat out into the ‘BACKUP‘ folder.

Lets go through the parameters I set with my backup command.

  • /MIR – This mirrors the backup, so if you delete a folder it will also delete it on the backup on its next run. This is essential or your backup location will just become a mess of folders as time goes on and will make it very easy to find what you are looking for.
  • /XA:SH – This excludes the hidden system files.
  • /R:5 – Now, one of Robocopy’s features is that if it encounters a file that is in use, it will stop and wait for that file to be closed so that it can continue with the copy operation. It will retry to copy the file ever 30 seconds. The default number of retries is 1 million (no joke!). As this will most likely prevent the backup operation from ever completing, we set it to try only 5 times.
  • /W:15 – This changes the default wait time between retries from 30 seconds to just 15 seconds.
  • /V – This gives a verbose output of what robocopy is up to during the backup.
  • /NP – This will not show the percentage progress of each file in the output as I don’t really need to know that.
  • /ETA – This shows an Estimated Time of Arrival of copied files in the output.
  • /LOG:Backup_alexs.log – Finally I like to keep a log of how the backup went as I will not be staring at the command line during the backup.

Now you can put this command into a text file and save it as ‘backup_alex’s.cmd‘ All you have to do now is schedule when you want your backup to run using the Task Scheduler in windows.

I run my backup script every day at 20:00

Ajax requests with Jquery

Have you ever wanted to push some data to the page to give a better user experince and not require a reload of the page you are on.

This can be achieved very easily with jQuery.

First simply have a div allocated for the response, in this case we will use the id ‘#info’

Then you can simple apply the following code to an on click event, is this case we are using a div with the id ‘#clickHere’

[sourcecode language=”javascript”]

$(‘#clickHere’).click(function() {

$.get(“load_data.php?vars=”+someVar, function(data){

//set inner html with data response from load_data.php
$(“#info”).html(data);

});

});

[/sourcecode]

The file load_data.php can have any logic you want in it, notice how I passed a variable called ‘someVar’ to the script, this may be necessary to output dynamic data.

Whatever is echoed out in load_data.php will be shown inside the div ‘#info’

This is a very useful technique when creating streamlined user experiences.

Editing files with vi Commands

If you administrate servers you will almost definitely need to use vi command from the terminal as a GUI is not always possible, and vi is generally quicker to use I find.

These are my useful commands, ‘esc’ = the escape button.

Open a file:
[sourcecode language=”sh”]
vi filename
[/sourcecode]

Edit the file:
[sourcecode language=”sh”]
esc i
[/sourcecode]

Close the file:
[sourcecode language=”sh”]
esc :q
[/sourcecode]

Close and save the file:
[sourcecode language=”sh”]
esc :wq
[/sourcecode]