Powershell script – create folder and zip – backup

You have a folder that you would like to compress with one click and save the result to a specific folder and file name. To have a regular backup and archive, multiple backup files e.g. every night or on each user logon/logoff should be created.

The following simple powershell scripts allow to compress the content of a folder and create a zip file on another location. This can be used e.g. as a simple backup solution.

Prerequisit

The following scripts require Powershell 4/5 for Windows, and Powershell 6 for Mac/Linux/arm user (using Powershell Core).

Simple script – zip file for directory with same target file name

The following powershell script creates a zip file based from $sourceDir and writes the result to $targetFile .

$sourceDir = 'c:\data';
$targetFile = 'c:\backup\data.zip'

## no changes here
Add-Type -A 'System.IO.Compression.FileSystem';
[IO.Compression.ZipFile]::CreateFromDirectory($sourceDir, $targetFile);

Advanced script – zip backup file with target filename including timestamp

The following advanced powershell script creates a zip file based on the content of the folder $sourceDir. The output is written to a file named “data-” and the current timestamp, located in the $targetDir.

$sourceDir = 'c:\data';
$targetDir = 'c:\backup\'
$targetFilenameBase = 'data-';

## no changes here
$date = get-date;
$dateStr = $date.ToString("yyyy-MM-dd_HH-mm");
$targetFile = $targetDir+$targetFilenameBase+$dateStr+'.zip';

Add-Type -A 'System.IO.Compression.FileSystem';
[IO.Compression.ZipFile]::CreateFromDirectory($sourceDir, $targetFile);

How to use the backup powershell script

Just copy the content of one of the solutions in a text editor or the Windows PowerShell Integrated Scripting Environment (ISE) , save the result to a file as e.g. create_backup.ps1 and run it.

Other solutions

Windows 10 provides further backup application and features – with some advanced features, that require addition settings and hardware.

Powershell 7 provides also features to create and decompress zip files with the functions Compress-Archive and Expand-Archive .

Symfony2: InactiveScopeException and request

Error message: You cannot create a service (“request”) of an inactive scope (“request”).

[Symfony\Component\DependencyInjection\Exception\InactiveScopeException]   
You cannot create a service ("request") of an inactive scope ("request").

This message is written, when you’re invoking a command on the command line, e.g.

php app/console assetic:dump

Reason: you are using a request object, but this is not available on the command line / cli

The error was in my case that I used an object in my service configuration that references the request object (MobileDetectBundle).

Solution: remove reference for command line

Step 1 was to use a setter for the reference

Step 2 was to create a special compiler pass in the bundle configuration

Source code (based on https://github.com/suncat2000/MobileDetectBundle/commit/73e78070ca7340c9774c19dde3c8460435b0e284 – thanks suncat2000!)

<?php

namespace Acm\DemoBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
 * CLI listener
 */
class CLIListenerPass implements CompilerPassInterface
{
    /**
     * If CLI, when remove setMobileDetector method from definition mobile_detect.twig.extension
     * @param ContainerBuilder $container 
     */
    public function process(ContainerBuilder $container)
    {
        if (php_sapi_name() == "cli") {
            $definition = $container->getDefinition('acme_demo.extension.my_service');
            $definition->removeMethodCall('setMobileDetector');
        }
    }
}