0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pleasanter(Windows Server)SQL Serverの定期バックアップ&定期削除

0
Posted at

基本環境

Pleasanterバージョン:1.5.2.0
Windows Server 2025 Standard 24H2
SQL Server 16.0.1

設定ファイル

ダウンロード

path =       "c:\Backup\MyDataBase_" & _
             Replace(Replace(Replace(Now(), "/", ""), " ", "_"), ":", "") & _
             ".bak"

バックアップ&削除

Option Explicit
Dim conn, timeout, server, db, uid, pwd, path, backupDir, backupPrefix

timeout =      3600
server =       "(local)"
db =           "[データベース名]"
uid =          "[BDユーザーID]"
pwd =          "[BDパスワード]"
backupDir = "[データベースパス:例(c:\directory\directory\)]"
'--- 最後にバックスラッシュ→そのディレクトリ下にファイルが保存される ---
backupPrefix = "[接頭辞]"
'--- バックアップファイルの先頭に固定文字列を入れたい場合 ---

path = backupDir & backupPrefix & _
       Replace(Replace(Replace(Now(), "/", ""), " ", "_"), ":", "") & _
       ".bak"

Set conn =     WScript.CreateObject("ADODB.Connection")
conn.CommandTimeout = timeout
conn.Open (    "Driver={SQL Server};" & _
               "Server=" & server & ";" & _
               "Database=" & db & ";" & _
               "UID=" & uid & ";" & _
               "PWD=" & pwd & ";")
conn.Execute   "BACKUP DATABASE " & _
               "[" & db & "] " & _
               "TO DISK=N'" & path & "' " & _
               "WITH NOFORMAT, " & _
               "NOINIT, " & _
               "NAME=N'" & db & "- FULL BACKUP', " & _
               "SKIP, " & _
               "NOREWIND, " & _
               "NOUNLOAD, " & _
               "STATS=10;"
conn.Close
Set conn = Nothing

'--- 30日以上前のバックアップファイルを削除 ---
Dim fso, folder, file, threshold
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(backupDir)
threshold = DateAdd("d", -30, Now())
'--- 経過日付の設定 ---

For Each file In folder.Files
    If LCase(fso.GetExtensionName(file.Name)) = "bak" Then
        If Left(file.Name, Len(backupPrefix)) = backupPrefix Then
            If file.DateLastModified < threshold Then
                On Error Resume Next
                file.Delete True
                On Error Goto 0
            End If
        End If
    End If
Next

Set folder = Nothing
Set fso = Nothing

VBSファイルとして保存
*****.vbs

動作テスト

PowerShell

cscript //nologo "スクリプトのフルパス.vbs"

タスクスケジューラ設定

Windowsツール > タスクスケジューラ 起動

全般タブ
名前:任意の名前
説明:任意の説明
[✓] ユーザーがログオンしているかどうかにかかわらず実行する
[✓] 最上位の特権で実行する

トリガー
新規 > 
タスクの開始:スケジュールに従う
設定:頻度、開始日時、間隔
(例:今日から毎日午前2時にバックアップ:頻度:毎日、開始日付:今日、開始時間:2:00:00、間隔:1日
詳細設定:
[✓] 有効

操作
新規 >
操作:プログラムの開始
プログラム/スクリプト:設定VBSファイルのフルパス

設定
[✓] タスクを要求時に実行する
[✓] タスクが失敗した場合の再起動の間隔:任意、再起動試行の最大数:任意
[✓] 時間:タスクを停止するまでの時間:テストにかかった時間以上
[✓] 要求時に実行中のタスクが終了しない場合、タスクを強制的に停止する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?