Server backup script

This topic contains 16 replies, has 3 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 1 year, 4 months ago.

  • Author
    Posts
  • #31647
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    I am trying to code a script to backup a server connected remotely. I just need to copy the "data" directory from "mysql" directory. For example, the file would be in \\test_server\c$\mysql\data. And i want to copy to my own HD. In my own partition "D". My code currently:

    #System variables

    $date = Get-Date -Format dd.MM.yyyy
    $source = "\\teste\c$\teste.txt\"
    $destination = "D:\$date"
    $username = "xxx"
    $password = "xxx"

    #Email variables

    $from = "XX "
    $to = "XX "
    $body = "Backup:"
    $subject = "Backup of $date"

    #Start

    mkdir $date
    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.NetworkCredential($username, $password)
    Copy-Item -Path \\teste\c$\test.txt -Destination D:\$date\test.txt;
    $backup_log = Dir -Recurse $destination | out-File "$destination\backup_log.txt"
    $attachment = "$destination\backup_log.txt"

    #ACK

    send-MailMessage -From $from -To $to -Subject $subject -Attachments $attachment -Body $body -BodyAsHtml
    write-host "Backup Sucessfull"
    cd c:\
    }

  • #31651
    Profile photo of Lauras Juozulynas
    Lauras Juozulynas
    Participant

    Hi Pedro,

    Copy-Item does not work with paths that have '$' in them. You should map a PSdrive, copy from drive and then remove drive. Here is example:

    $sourcefolder = "\\teste\c$\"
    New-PSDrive -Name X -PSProvider FileSystem -Root $sourcefolder
    $file = "X:\teste.txt"
    Copy-item $file -Destination "folder on your drive"
    cd c:\             #make sure you are not on X: drive
    Remove-PSDrive X
    
  • #31654
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    And what about the connection between the machine and the server? How can i do this? I was trying to invoke-command -computername Server test.ps1 but i got the winrm issue.

  • #31675
    Profile photo of Lauras Juozulynas
    Lauras Juozulynas
    Participant

    Let me get it straight:

    1. You are running this script on your PC (may be other serverX).
    2. You access files on server via admin share with address "\\teste\c$\"

    In this scenario you run the script on destination machine (your PC). Script maps remote server admin share. No remote powershell needed for accessing admin share. Then it copies file to your PC.

  • #31681
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    I see.. but it doenst work for me. Can u give me an example, please?
    Look at this code:
    $date = Get-Date -Format dd.MM.yyyy
    $sourcefolder = "\\server\c$\"
    $destfolder = "\\mypc\d$\"
    New-PSDrive -Name Map -PSProvider FileSystem -Root $sourcefolder
    $file = "Map:\test.txt"
    Copy-Item $file -Destination (New-Item "$destfolder\$date\" -Type container -Force) -Force
    cd c:\ #make sure you are not on Map: drive
    Remove-PSDrive Map

    It gives me an error: The access to the path '04.11.2015' was denied. + CategoryInfo : InvalidArgument: (\\mypc\d$\:String) [New
    -Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShel
    l.Commands.NewItemCommand

    The path has an invalid format
    + CategoryInfo : InvalidArgument: (\\mypc\d$:String) [New
    -Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShel
    l.Commands.NewItemCommand

    How can i do this? I know that's simple but i cant do it

  • #31682
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    $date = Get-Date -Format dd.MM.yyyy
    $sourcefolder = "\\server\c$\"
    New-PSDrive -Name Map -PSProvider FileSystem -Root $sourcefolder
    $file = "Map:\teste.txt"
    Copy-Item $file -Destination (New-Item "C:\$date\" -Type container -Force) -Force
    cd c:\ #make sure you are not on Map: drive
    Remove-PSDrive Map

    When i do this im out of errors, but it copies the txt to partition C of the server.. not mine partition C in local machine.
    What can i do about it?

  • #31689
    Profile photo of Lauras Juozulynas
    Lauras Juozulynas
    Participant

    When forming new folder you get a double '\\' in middle of path:

    New-Item "$destfolder\$date\"  
    

    Your code produces : \\mypc\d$\\04.11.2015\

    Why don't you form a destination folder earlier, before copy-item command?
    Also you are using network path to access your local drive? Use just local drive.

    $destfolder = "D:\"+$date+"\"
    

    Your whole code should look like this and should be executed on your PC not server!

    $date = Get-Date -Format dd.MM.yyyy
    $sourcefolder = "\\server\c$\"
    $destfolder = "D:\"+$date+"\"
    New-Item $destfolder -Type container -Force
    New-PSDrive -Name Map -PSProvider FileSystem -Root $sourcefolder
    $file = "Map:\test.txt"
    Copy-Item $file -Destination $destfolder -Force
    cd c:\ #make sure you are not on Map: drive
    Remove-PSDrive Map
    
  • #31712
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    Hi Lauras, when i execute that code i get this error:

    Is not possible find the path 'Map:\test.txt' because he doesnt exists.
    + CategoryInfo : ObjectNotFound: (Map:\test.txt:String) [Copy-Ite
    m], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyI
    temCommand

  • #31713
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    I was missing the path.. But now the problem is: in my pc the partition is C, like server. So when i put $destfolder = "C:\"+$date+"\" it copies the "test.txt" to server's HD. What can i do?

  • #31716
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    stop using double quotes around paths like \c$\ and all be ok, the $ allowed characters, but you need to use single quotes it you not need valiables in paths or mask it with `.

    for example if $date='2015-11-05", then
    c:\$date\' is c:\$date\ and "c:\$date\" is c:\2015-11-05\\

    get-help about_Quoting_Rules

  • #31717
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    i've done but still copying the files to the same directory in the server. I just want to copy the files from the \\server\c$\ to my pc in c:\$date .. how can i do this?
    I think that the problem is the name of partitions. Both server and local have the same name of partition(C:). So when i copy from server to C:\ it thinks that im copying to the own C of the server. Am i wrong?

    it cant be so difficult like that..

    Thanks, anyway!

  • #31718
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    what I'm doing wrong ? 🙂


    PS C:\1> dir
    PS C:\1> dir \\server\c$

    Directory: \\server\c$

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    d---- 14.07.2009 7:20 PerfLogs
    d-r-- 15.12.2012 15:09 Program Files
    d-r-- 14.09.2015 13:25 Program Files (x86)
    d-r-- 20.12.2011 13:18 Users
    d---- 02.01.2012 15:08 Utils
    d---- 12.10.2015 21:48 Windows
    -a--- 19.12.2011 13:07 79 sn

    PS C:\1> copy-item \\server\c$\sn c:\1
    PS C:\1> dir

    Directory: C:\1

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 19.12.2011 13:07 79 sn

    PS C:\1>
    PS C:\1> $date = (get-date).tostring('yyyy-MM-dd')
    PS C:\1> $date
    2015-11-05
    PS C:\1> mkdir $date

    Directory: C:\1

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    d---- 05.11.2015 23:20 2015-11-05

    PS C:\1> dir

    Directory: C:\1

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    d---- 05.11.2015 23:20 2015-11-05
    -a--- 19.12.2011 13:07 79 sn

    PS C:\1> copy-item \\server\c$\sn c:\1\$date
    PS C:\1> dir $date

    Directory: C:\1\2015-11-05

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 19.12.2011 13:07 79 sn

    PS C:\1>

  • #31732
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    But i need to use credentials to log in that Server, so i'm using invoke-command to call the script. Can u put this in script code?

  • #31737
    Profile photo of Lauras Juozulynas
    Lauras Juozulynas
    Participant

    Hi,

    Here is full script with credentials:

    $date = Get-Date -Format dd.MM.yyyy
    $sourcefolder = '\\server\c$\'
    $destfolder = 'D:\'+$date+'\'
    New-Item $destfolder -Type container -Force
    $credentials = Get-Credential -UserName "domain\username" -Message "Enter Password:"
    New-PSDrive -Name Map -PSProvider FileSystem -Root $sourcefolder -Credential $credentials
    $file = 'Map:\test.txt'
    Copy-Item $file -Destination $destfolder -Force
    cd c:\ #make sure you are not on Map: drive
    Remove-PSDrive Map
    
  • #31741
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    if you use Invoke-Comand -Computername Server to start Copy-Item, than for that command the server is local, and your computer is remote!

    but by default remotely executed command have only local acccess, so you can't just do

    Invoke-Command -Computername server -Credentials $cred -Command { $date = (Get-Date).ToString('yyyy-MM-dd'); Copy-Item C:\Backup.zip "\\yourComputer\backup\$date" }
    where \\yourcomputer\backup is shared c:\baclup folder.

    so you can use CredSSP Authentication or New-PSDrive as Lauras suggest

  • #31804
    Profile photo of Pedro Stefano
    Pedro Stefano
    Participant

    Thanks for the answers! So can i just ` Invoke-Command -Computername server -Credentials $cred -Command { $date = (Get-Date).ToString('yyyy-MM-dd'); Copy-Item C:\Backup.zip "\\yourComputer\backup\$date" } `

    where \\yourcomputer\backup is shared c:\baclup folder. <- I dont want to share my backup folder because it's not safe at all, right? So what can i do? And my local machine is mapped on the server, does it make easier? Or must i use the CredSSP Authentication or New-PSDrive?

  • #31843
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    using c$ share is not safer than shared folder with properly assigned credentials 🙂

    I think the most safer and simple method is make properly secured shared folder (backup$ for example) on your server
    and just use this code sample on your computer

    $date = (Get-Date).ToString('yyyy-MM-dd')
    Copy-Item \\server\backup$\backup.zip c:\bakup\$date

    or start opposite version on \\server by task scheduler

    $date = (Get-Date).ToString('yyyy-MM-dd')
    Copy-Item c:\bakup\backup.zip \\backupcomputer\backup$\$date

    this variant includes creation of properly secured backup$ folder on your backup computer

    any variant usable without any special manipulations with remoting
    the key part – create shared folder and set access credentials so remote computer(user) can read it

You must be logged in to reply to this topic.