Automating backups with RoboCopy and PowerShell

Backups are important and you should live by the 3-2-1 rule.

That is: 3 copies of a file; 2 copies on site, 1 copy off site

Backups should be automatic where possible. If you are manually backing up data, there is a good chance data wont get backed up as often as you hoped.

You should also test your backups regularly!

RoboCopy, short for “Robust File Copy”, is a command line utility included within Windows. It can be used to create backups of files and directories. There are many tools and programs for backing up data, but sometimes the free tools that come with the operating system are all you need.

We are going to backup files from an internal hard drive onto an external hard drive that is always plugged in. This will work well for hard drive failure, but wont be a great backup against ransomware.

Automating tasks is great for productivity. Especially if you can reuse scripts from other tasks.

To get started, first open PowerShell as an administrator. We need to create a folder to store our basic script. In your PowerShell window enter the following command:

New-Item -Path "c:\" -Name "scripts" -ItemType "directory"

And now change into the new directory:

cd c:\scripts\

Now we are going to create the batch file to run RoboCopy. Do that with the following commands:

New-Item -Name "backup.bat" -ItemType "file"
Add-Content -Path .\backup.bat -Value "robocopy D:\ E:\ /B /MIR"

We have now created a script to backup drive D to drive E – of course you may need to adjust drive letters to suit your system.

RoboCopy with copy drive D to drive E using backup mode. This allows RoboCopy to override the file and folder permissions that may prevent a file being copied. MIR copies the complete directory tree. This keeps the second drive as an exact mirror of the first.

Now we want to make sure this script runs daily. We can do that using PowerShell to configure a scheduled task. Enter the following commands:

$action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument '/c start "" "C:\scripts\backup.bat'
$trigger = New-ScheduledTaskTrigger -Daily -At 1pm
$principal = New-ScheduledTaskPrincipal -LogonType S4U

You will be asked to enter the username for the Administrator account, then follow on with the next command:

Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName "Backup D Drive" -Description "Daily Backup of Drive D"

You now have a scheduled task that copies the content of drive D to drive E every day at 1pm – leaving you more time to browse my blog!

Holiday Season maintenance for Family and Friends

The holiday season is quickly approaching – during which, many of us will visit (or be visited by) family and friends. If you’re employed in any role that involves using a computer you will no doubt be bombarded with technical questions over the silly season.

This is just a few simple tasks I’ve put together to help family and friends have a better computing experience and be more secure – and possibly save you from having to remove the same junkware from their machine again next holidays.

Windows

  • Remove the junk: AdwCleaner seams to clean up most of the rubbish that users manage to install. It is offered for free by Malwarebytes.
  • Run Disk-Cleanup: If it hasn’t been done for a while, it will free up quite a bit of disk space.
  • Ensure operating system is up to date: No doubt there will be many friends or family members saying their computer “isn’t going to work” after January. Time to upgrade to Windows 10. If Windows 10 is out of their price range check out Ubuntu (Or Lubuntu for older hardware).
  • Install Google Chrome (If you are Privacy conscious maybe look at Mozilla Firefox).
  • Install uBlock Origin & HTTPS Everywhere
  • Modify the HOSTS file using the HOST file from someonewhocares: It might break the advertising links at the top of a Google search if this is going to be a problem.
  • Ensure AV is up to date: Use a paid AV if possible (I recommend ESET) If paid is not possible ensure Windows Defender is enabled on Windows 10.
  • Create a new Administrator account: and set the main user as a Standard user. Don’t keep the password to yourself, otherwise you will get many phone calls from your family and friends every time they need to change a setting. Running as a standard users prevents malware or other nasties from running as administrator as they stealthily try to execute in the background.

MacOS

  • Install latest patches for macOS: If device is too old and slow (or running an unsupported OS) consider installing Lubuntu.
  • Install Google Chrome (If you are Privacy conscious maybe look at Mozilla Firefox).
  • Install uBlock Origin & HTTPS Everywhere
  • Create a new Administrator account: and set the main user as a Standard user. Don’t keep the password to yourself, otherwise you will get many phone calls from your family and friends every time they need to change a setting. Running as a standard users prevents malware or other nasties from running as administrator as they stealthily try to execute in the background.
  • If using a Mac ensure the inbuilt firewall is enabled.

iOS

Turn on Passcode or Face ID: Settings > Touch ID & Passcode (Face ID & Passcode) > Turn Passcode On.

Turn on Automatic Updates for iOS: Settings > General > Software Update > Automatic Updates = On.

Android

Passwords

Talk about Passwords: Yes Passwords suck, but sucky passwords suck more. Teach them about diceware. Need a gift idea? Buy a family member a diceware password. Help to setup a password manager. KeePassXC works well, and I have had many clients find 1Password nice and easy to use.

Help configure 2FA – You probably don’t want to setup up all their accounts with 2FA but perhaps start with important things like email and banking. If you have a tech savvy friend or family member perhaps a YubiKey would be a good gift.

If I have missed anything feel free to let me know and I’ll add it!

Simple disk repartition and format with diskpart

Disclaimer: These steps will wipe the hard drive permanently. If you need to keep the data create a backup first.

At an elevated Command Prompt/PowerShell type diskpart. This will bring up diskpart utility. Diskpart allows you to manage disks, partitions, and volumes. Diskpart is ready when you can see DISKPART> on the Command Prompt/PowerShell window. We can now repartition and format the hard drive.

  • First identify the disk you wish to repartiton. To do this type list disk. The disk will be identified with a number. i.e. Disk 3.
  • Select the disk by typing select disk 3 or whatever number corresponds with your drive.
  • Type list partition to view the current partitions. Make note of the  partition/s you wish to remove. i.e. partition 1.
  • To remove a partition first type select partition 1, or whatever number corresponds with the partition you wish to remove.
  • Delete the partition by typing delete partition. This will remove the currently selected partition. Repeat for multiple partitions.
  • Create a new partition by typing create partition primary. Your new partition is created and ready to format.
  • Format the partition by typing format fs=ntfs label=”Local Disk”. The drive will now be formatted. If you requrie a qucik format you can type format fs=ntfs label=”Local Disk” quick

Your hard drive is now formatted and ready to use.

Repair Windows Boot Errors

Not being able to boot into Windows can be frustrating and can often lead to the dreaded re-installation of Windows. This doesn’t have to be the case, with some boot troubles easily fixed using tools provided on your installation media.

Disclaimer: Before undertaking any of the steps below create a backup of your files. This can be done by booting from a bootable USB stick (Windows Installation Media or Live Linux Distro) and transferring files to an External Drive. I am not  responsible for data lost undertaking these repairs. If you are unable to access your hard drive using bootable media you might have a dead hard drive.

I have used these steps to troubleshoot the following errors:

  • INACCESSIBLE_BOOT_DEVICE Blue Screen
  • Blinking White Cursor
  • Stuck on Windows 10 Spinning Wheel
  • Booting to Recovery Mode

CHKDSK

Often overlooked, your boot troubles could be something as simple as a errors within the file system or files in bad sectors that are required upon boot.

To run chkdsk (check disk) you will need to boot up from a bootable Windows Installation DVD or USB stick and choose the Command Prompt from recovery options.

At the command prompt run the following command:

  • chkdsk /r

the /r switch fixes errors on the disk and locates bad sectors and attempts to recovery information in those sectors.

CMOS Settings

Sometimes boot issues can be caused by incorrect CMOS settings. With most machines you can access CMOS by pressing DEL when powering up. If this doesn’t work you might need to press ESC first to pause ‘fast boot’. You will then be displayed a menu with the key you need to press to access CMOS (also referred to as BIOS).

Once in CMOS check that SATA mode is set correctly, i.e. IDE, AHCI, or RAID. Be sure to save changes upon exit.

A flat CMOS battery will reset CMOS settings to default. If the settings where changed from default before installing Windows (i.e. from IDE to AHCI), and CMOS is reset, Windows will fail to boot.

A user inadvertently holding the power button in for an extended period of time (or a sticky power button) can also cause CMOS to be reset.

If you are unsure what setting you need, try swapping from the default to IDE or AHCI.

Bootrec

Bootrec is a tool found on Windows Installation Media. To use this tool you will need to boot up from a bootable Windows Installation DVD or USB stick and choose the Command Prompt from recovery options.

At the command prompt run the following commands:

  • bootrec /FixMbr
  • bootrec /FixBoot
  • bootrec /RebuildBcd

bootrec /FixMbr writes a new master boot record to the system partition.

bootrec /FixBoot writes a new boot sector the the drive.

bootrec /RebuildBcd searches available hard drives for installations of Windows and adds them to the boot configuration data. If you have multiple hard drives containing Windows installations these will be added to BCD.

If no installations are found using bootrec /RebuildBcd I have found running the command bootsect /nt60 all /mbr followed by a reboot (returning to a command prompt) and then running bootrec /RebuildBcd will allow Windows Installation to be added to BCD.

bootsect /nt60 all /mbr applies the master boot code compatible with BOOTMGR. BOOTMGR is used by Windows Vista, Windows 7, Windows 8, and Windows 10.

Windows XP uses NTLDR which requires the switch /nt52 rather than /nt60.

Also if you are still using Window XP it might be time to upgrade!

Diskpart

Diskpart can be found on Windows Installation Media. To use this boot up from a bootable Windows DVD or USB and choose the Command Prompt from recovery options.

Steps for Diskpart will differ from machine to machine, but I usually find Disk 0 is the main hard drive, and the largest partition (Possibly Partition 1) is the Windows Installation partition.

At the command prompt run the following commands, taking into account your differing Disk and Partition numbers:

  • Diskpart
    Opens the Diskpart Utility
  • List Disk
    This allows you to view the number of a disk
  • Select Disk #
    Replace # with the number of the disk shown above, possibly ‘0’
  • List Partition
    This allows you to view partition numbers
  • Select Partition #
    Replace # with the number of the partition shown above, possibly ‘1’
  • Active
    This marks the partition as an active system partition.  
  • Exit
    Quits Diskpart Utility
  • Bootrec /RebuildBcd
    Searches available hard drives for installations of Windows and adds them to the boot configuration data.

For some machines I have also had to run Extend after running Active in the steps above. Extend is used to extend a freshly created partition, so I am not sure 100% while this has been successful for me.

I hope this post is able to help someone out!