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
    # .NOTES
    # Version: 2022-06-11 2022-08-28
    # .LINK
    # http://xahlee.info/powershell/powershell_backup_script.html

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

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

    $null = mkdir $destDirPath;

    Write-Host $linesep;
    Get-Date -Format "yyyy-MM-dd HH:mm";
    Write-Host "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;

    Write-Host $linesep;
    Get-Date -Format "yyyy-MM-dd HH:mm";
    Write-Host "Start backup c drive";

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

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

    Write-Host $linesep;
    Get-Date -Format "yyyy-MM-dd HH:mm";
    Write-Host "Start backup d drive";

    $dDriveDirs =
    "xah_ddrive_2022",
    "xah_archive";

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

    Write-Host $linesep;
    Get-Date -Format "yyyy-MM-dd HH:mm";
    Write-Host "Backup done.";

}