Backup Script

By Xah Lee. Date: . Last updated: .

Here's a simple backup script i use to backup my stuff to a external drive.

function xah-backup-drives {

    # .DESCRIPTION
    # backup my drives
    # Version 2022-06-11 2022-08-28
    # .LINK
    # http://xahlee.info/powershell/powershell_backup_script.html

    Get-Date -Format "yyyy-MM-dd HH:mm";
    echo "Start backup";

    $xDate = Get-Date -Format "yyyy-MM-dd_HHmmss";
    $destDirPath = "f:/xahbackup_${xDate}";

    $null = mkdir $destDirPath;

    echo "ssss---------------------------------------------------";
    Get-Date -Format "yyyy-MM-dd HH:mm";
    echo "Start backup xah website git";
    $xahWebGitBackupPath = Join-Path $destDirPath "xahweb_${xDate}.git.zip";
    $gitCommand = "git --git-dir='c:/Users/xah/web/.git/' archive -o '${xahWebGitBackupPath}' HEAD";
    Invoke-Expression $gitCommand;

    echo "ssss---------------------------------------------------";
    Get-Date -Format "yyyy-MM-dd HH:mm";
    echo "Start backup c drive";

    $cDriveDirs = "xdoc", "git", "Desktop", "Documents" ;

    $cDriveDirs.foreach({
            echo "zipping $_";
            7z a (Join-Path $destDirPath "$_.7z") "c:/Users/xah/$_/";
        })

    echo "ssss---------------------------------------------------";
    Get-Date -Format "yyyy-MM-dd HH:mm";
    echo "Start backup d drive";

    $dDriveDirs =
    "xah_ddrive_2022",
    "xah_archive";

    $dDriveDirs.foreach({
            echo "zipping $_";
            7z a (Join-Path $destDirPath "$_.7z") "d:/$_/";
        })

    echo "ssss---------------------------------------------------";
    Get-Date -Format "yyyy-MM-dd HH:mm";
    echo "Backup done.";

}

PowerShell

Practical How-To

list files

list dirs

on dir

on file

Windows config

zip tar

misc


PowerShell in Depth

Programing PowerShell

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples