Register for the iXsystems Community to get an ad-free experience and exclusive discounts in our eBay Store.

Backing Up CIFS Shares to USB Drives

Western Digital Drives - The Preferred Drives of FreeNAS and TrueNAS CORE
Status
Not open for further replies.

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
6,631
This is howto is basically in the vein of 'any backup is better than no backup'. I have been messing around with powershell recently and decided someone might find a script to backup samba shares to USB hard drives useful.

Code:
#### VARIABLES YOU CAN CHANGE ####
## Volume Name for External USB Hard Drive. Should be unique.
$VolumeName = 'OFFSITE03'

## [Source Path] You should enter your server name and share name as below
$ServerName = 'SambaServer01'

# Backinging up a directory within a share can be done as follows:
# $ShareName = "MyShare01\Directory"
$ShareName = "MyShare01"

## [Target Directory] Should be a folder name on target Hard Drive.
$TargetDirectory = 'ImportantStuff'

## Alter the LogPath variable to point it to the directory where you want to store logs.
$LogPath = "\\SambaServer02\IT\logs"

#### VARIABLES YOU PROBABLY SHOULDN'T TOUCH ####
$SourcePath = "\\$ServerName\$ShareName"
#Removing backslashes from the share name in case we are only backing up a sub-directory inside the share.
$LogShareName = $ShareName.Replace("\",".")
#automatically generate log file name based on timestamp, server, and share.
$LogFile = "$LogPath\log.$ServerName.$LogShareName.$(get-date -f yyyy-MM-dd).log"
#Disk info for the specified volume
$disk = Get-WmiObject -query "SELECT * from win32_logicaldisk where VolumeName = '$VolumeName'"
#Get drive letter so that we can use it for a robocopy target
$DriveLetter = $disk.DeviceID

########################################
########################################
#### BACKUP SCRIPT ####

if ($disk.DeviceID -eq $null){
    msg * "RoboBackup task not run - $VolumeName is not connected to the computer"
}

else {
    robocopy $SourcePath $DriveLetter\$TargetDirectory /MIR /Z /W:20 /R:15 /PF /LOG:$LogFile
    Write-Host $disk | out-file -Append -filepath $LogFile # append current disk stats to Robocopy Log so that can easily see if drive filling up.
}


The thought behind this is that directly attaching USB storage to a FreeNAS server is annoying. The more annoying something is, the less likely people are to do it. Solution: connect the USB drive to a client computer (connected via gigabit), and use "Task Scheduler" to configure a weekly backup task. On the days when the task isn't running, put the USB drive in a safe place. Don't leave it connected to your computer. The advantage of this method is that it uses built-in windows tools (no software installation required), and detects the appropriate drive letter of your backup target so it is portable between client computers.

tl;dr

  • Connect a USB Hard Drive to a windows client. Format it and name the volume.
  • Change the variables in the above script to appropriate values for your server / network / USB drive.
  • Open "Task Scheduler" and schedule a backup.

Caveats:
  • I haven't tried, but powershell scripts probably don't run on macs.
  • This will not magically increase the size of your external hard drive to hold all the data you are pointing at it. Use common sense and don't try to back up a 30TB dataset to a 4TB drive.
  • I typically don't write powershell scripts so there is a chance your computer will catch on fire if you run this.
[edit: I ate a cookie and my mood improved. I removed mention of how I was writing a crappy script in a crappy scripting language for a crappy OS to copy your crappy files using a crappy file sharing protocol to an uber-crappy storage medium.]
 
Last edited:

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I don't want your crappy script. ;)

Moved to the how-tos.
 
Status
Not open for further replies.
Top