Skip to main content

[PowerShell]: Powershell script uploading backup databases to ftp server

Author: Enrique Puig Nouselles
http://gallery.technet.microsoft.com/scriptcenter/80647f66-139c-40a4-bb7a-04a2d73d423c


Nhu cầu
- Học sử dụng Powershell và áp dụng vào thực tế
- Server chạy MS SQL được backup mỗi ngày và lưu các file backup ở folder mặc định "C:/Program Files/Microsoft SQL Server/MSSQL11.MSSQLSERVER/MSSQL/Backup"
- Upload các file database backup về một ftp server khác để lưu "offsite"

# Begin script
# Thanks to: Enrique Puig Nouselles http://gallery.technet.microsoft.com/scriptcenter/80647f66-139c-40a4-bb7a-04a2d73d423c
# Khai báo folder chứa các file backup database, dưới đây là folder backup mặc định của MS SQL 2012
$Dir="C:/Program Files/Microsoft SQL Server/MSSQL11.MSSQLSERVER/MSSQL/Backup"  

# ftp server
$ftp = "ftp://IPAddres/subfolder/subsubfolder"
$user = "usernamehere"
$pass = "passwordhere"
# DO NOT use $(Get-Date), use $d variable instead, otherwise SQL Agent gets error "The job step contains tokens...."
$d = Get-Date

$webclient = New-Object System.Net.WebClient

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)

# Get files created in the past 24 hours
#list every sql server trace file
foreach($item in (dir $Dir "*.bak")){
if ($item.CreationTime -gt ($d.AddDays(-1)))
{
    "Uploading $item..."
    $uri = New-Object System.Uri($ftp+$item.Name)
    $webclient.UploadFile($uri, $item.FullName)
} }
#End

Popular posts from this blog

Check and install php7.0-mbstring extention on Ubuntu 16.04 and Microsoft Windows

While learning Facebook SDK for PHP I overlooked one of system requirements preventing my code from running on my host. That is The mbstring extension. Facebook SDK for PHP's system requirements Check if mbstring is enabled root@ubuntu001:~# php -a Interactive mode enabled php > echo extension_loaded('mbstring'); php >exit root@ubuntu001:~# apt-cache search php7 | grep mbstring php7.0-mbstring - MBSTRING module for PHP root@ubuntu001:~# Install php7.0-mbstring root@ubuntu001:~# apt-get install php7.0-mbstring Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required:   fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0   libjpeg-turbo8 libjpeg8 libmcrypt4 libtiff5 libvpx3 libxpm4 Use 'apt autoremove' to remove them. The following additional packages will be installed:   libapache2-mod-php7.0 php7.0-cli ph...