How to backup Ubuntu to a Windows share

If you need to back up your Ubuntu servers to a Windows share on your network,  then on this tutorial I will show you how you can accomplish that. This tutorial is based on Ubuntu, but the procedure should work in all Linux distributions. I have a Centos server that I backup using this script as well.

Create the Windows share

The first thing you need to do is to create the Windows share where the Ubuntu backup files will be dumped to. For example, I created a shared folder in my windows server called “LinuxBackup” for this backup.

Create the user account for the Windows share

You need to create either an AD or local user account to use for this backup, and add that user to the Windows share you created above with give it at least read/write permissions. I created a username called “lbackups”

Setting up your Ubuntu server

On your Ubuntu server run this command  sudo apt-get install cifs-utils if you are running Ubuntu 12.04 or an older version,  run this command sudo apt-get install smbfs now create a mounting point for the Windows share. I have my mounting point at “/mnt/backup” if you want to use the same type sudo mkdir /mnt/backup now add this string to the fstab in the /etc folder.

//windowsshareserver/LinuxBackup /mnt/backup cifs domain=domainname,username=username,password=password 0 0

Make sure you enter all the proper information on that string. After you do that mount the drive by typing this command sudo mount –a  that should mount the Windows share. to see all the mounted partitions on your server type df –h

Creating the script

The script I use to automate the backup is this:

 
#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################

# What to backup. 
backup_files="/var/www /var/lib/mysql /etc/apache2"

# Where to backup to.
dest="/mnt/backup"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

Modify the script to fit your needs. After you are done with the changes, copy the script to the /bin folder on your server. I named my script backup.sh so the full path to my script is /bin/backup.sh make sure the script is executable chmod +x backup.sh

Setting up the cronjob

Now to automate the backup, we need to setup the cronjob. Type sudo crontab –e on terminal and add this to the end of the cron file:

00 19 * * * bash /bin/backup.sh

That cronjob will run the backup file every day at 7 PM. you can change it to run any other time.

That’s it.