How to Backup Your Files Over SMB in Linux

Posted by Jonquil on Jul 13, 2012 2:35 AM EDT
xjonquilx | Sabayon, Ubuntu, Fedora, Linux, Oh My!; By Jonquil McDaniel
Mail this story
Print this story

I just recently acquired a Zyxel media server, which I wanted to use to backup my files over the network. The only problem was that there are no Linux backup programs (that I know of) capable of backing up over SMB, which meant I would have to write a script that could accomplish this. In my research I found a lot of hints at what could be done to backup over SMB but no real step by step guides on how to do it. That's why, now that I have gotten it accomplished thanks to some help from the guys over at LinuxQuestions, I'm writing an article on how you can backup your files over SMB automatically in Linux. Hopefully this will help someone out there that may not be tech savvy but can follow some simple instructions.

I just recently acquired a Zyxel media server, which I wanted to use to backup my files over the network. The only problem was that there are no Linux backup programs (that I know of) capable of backing up over SMB, which meant I would have to write a script that could accomplish this. In my research I found a lot of hints at what could be done to backup over SMB but no real step by step guides on how to do it. That's why, now that I have gotten it accomplished thanks to some help from the guys over at LinuxQuestions, I'm writing an article on how you can backup your files over SMB automatically in Linux. Hopefully this will help someone out there that may not be tech savvy but can follow some simple instructions.

To start out, you will need to know the following things: The IP address of the server you want to save your backups to The directory on the server you want to save your backups to The username/password of an account on the server that has full read/write access to the server

First of all we will need to mount the directory you want to save your backups to locally so the system can access it easily. Start out by making a directory in the /mnt directory (you will have to be root to do this):

mkdir /mnt/backup

Next you will want to mount the remote server's directory you want to save the backups to. This will be mounted inside the /mnt/backup directory we created. You will need to specify the IP address of the server, the directory you want to mount, the directory you want to copy over to the mounted directory, and of course you will also need to specify the username and password that you use to upload files to the server.

First specify the username and password:

mount -o username=admin,password=password

Next specify the server IP address along with the path to the shared directory:

mount -o username=admin,password=password //192.168.1.9/music /

Now specify the directory you want to use to mount the server's directory. In this case we are using the directory we created, /mnt/backup:

mount -o username=admin,password=sedated1 //192.168.1.9/music /mnt/backup

Now all you have to do is specify what directory you want to backup and what directory you want to send it to, which in this case is the /mnt/backup directory we created:

mount -o username=admin,password=password //192.168.1.9/music /mnt/backup && rsync -av /home/jonquil/Music /mnt/backup

When you are finished you should have a command string like what's shown above. Enter that and make sure the files will begin to be backed up.

Once you have ensured that command works, you'll want to create a script that uses it. Open up a text editor and put in the following:

#!/bin/sh

Then after that put in the command you want to use:

#!/bin/sh mount -o username=admin,password=password //192.168.1.9/music /mnt/backup && rsync -av /home/jonquil/Music /mnt/backup

Save the script in your home folder as backup.sh. Since this script will contain your password, you don't want just anyone to be able to open and read it. So as root issue the following command from your home directory in terminal:

chmod 500 backup.sh

Now all we have to do is set up this script to run in crontab at a specified time. In terminal as root use the following command to edit crontab:

crontab -e

It will ask you what text editor you want to use. I recommend nano as it is the easiest.

It is impossible for me to go over all the options you have for running scripts in crontab. So I will set you up with an example line and link to an article that explains crontab in depth:

0 1 * * 3 root run-parts /home/jonquil/backup.sh

This line tells crontab to run the script as root at 1 a.m. every Friday. If you would like to customize the time the script runs, look here for more information on how to schedule tasks in crontab.

Full Story

  Nav
» Read more about: Story Type: Tutorial; Groups: Linux

« Return to the newswire homepage

This topic does not have any threads posted yet!

You cannot post until you login.