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 .

phpOpenEMM now on github

We recently moved the source code for the PHP – wrapper for the email marketing software OpenEMM to github. It is also available via packagist for easier installation. Feel free to follow, fork and send pull requests!

Links

sample code for Google Client API published

Gallery

The sample code for the Google Client API pages has been updated. It now uses the names of the new Google Client PHP API release and also switched from object – reference to arrays. Finally the source code is available … Continue reading

search files by content text using find and grep

Image

This post is about a simple task – but I always forget the syntax. Search recursively for files inside the current directory with a special text in the file content.

find . -name "*.*" -print | xargs grep "SEARCHTEXT" --color=auto

This snippet searches for all files of the current directory that match the name parameter *.* (so you could e.g. search for all .css files with -name “*.css”). The result is displayed with grep – using the nice color-highlighting.

This is especially useful for remove access on linux machines or other unix-based devices.

Adsense widget for Geckoboard

enarion.net released a widget to display the revenues from Google Adsense in your Geckoboard.

Google Adsense widget for GeckoboardAll you have to do is to install the widget code (php-based) on your webserver and add a new custom widget to your Geckoboard widget. Various timespans (including today, yesterday, current month, last month) and values (e.g. ecpm, earnings and clicks) can be selected. Values are extracted in realtime from the Google Adsense account, providing up to the minute information.

The code is released under the GPL license and can be used and modified without charge.

Further information and the download can be found at http://enarion.net/tools/geckoboard-adsense-widget/.

About Geckoboard

You can create, run and share dashboards showing various key / performance values using the webservice Geckoboard. The setup is easy, there are many predefined widgets for various web services including Google Analytics, ZenDesktop, Mailchimp and various more.

About enarion.net

Providing open source tools and development knowledge to the public is the aim of enarion.net. It was founded by Tobias Kluge as home of phpSitemapNG years ago, a tool to generate Google sitemaps.