/bak

bak up and restore files

Primary LanguagePythonMIT LicenseMIT

bak

A utility for simple back up and restore of files and directories.

Introduction

How many times have you done mv file file.old, or mv file.txt file.txt.backup2, with ad-hoc filenames, and you later go back and you can't be sure which the most recent file is, or when you made that backup? Or maybe you're about to do a complicated git operation on a repository and you're not that confident in your git skills, so you cp -r myrepo myrepo-before-i-messed-it-up.

bak creates a backup of files or directories you tell it, puts a timestamp in the filename, and it can restore a file or directory based on its original name (it restores the most recently-backed-up file/directory by the timestamp in its name).

bak is designed to never lose data. If you try to restore over an existing file, it won't let you. Or if you give it a flag, it'll back up the current the file there first before restoring.

Installation

Download bak, mark it executable, and stick it somewhere on your path. It's Python 3 with no other dependencies.

Usage

Simple back up:

$ ls
file
$ bak file
Moving 'file' to 'file.bak.20191115T093728'
$ ls
file.bak.20191115T093728

Restore:

$ bak -r file
Moving 'file.bak.20191115T093728' to 'file'
$ ls
file

Back up, keep original:

$ bak -k file
Copying 'file' to 'file.bak.20191115T094849'

Restore the back up, when there's a file still at the original location. Read -f as "flip" or "force":

$ bak -f file
Moving 'file' to 'file.bak.20191115T094858'
Moving 'file.bak.20191115T094849' to 'file'

Multiple files:

$ touch file2 file3
$ bak file file2 file3
Moving 'file' to 'file.bak.20191115T094944'
Moving 'file2' to 'file2.bak.20191115T094944'
Moving 'file3' to 'file3.bak.20191115T094944'
$ bak -r file file2 file3
Moving 'file.bak.20191115T094944' to 'file'
Moving 'file2.bak.20191115T094944' to 'file2'
Moving 'file3.bak.20191115T094944' to 'file3'

It automatically finds the most recent back up for each file to restore.

Notes

This program is pretty trivial. It doesn't do anything you couldn't already do with cp, mv, and your watch. But it's convenient and makes a common use-case case better.

It's not meant to replace a full backup program like Arq or Time Machine.

If you use bak, I recommend adding *.bak.* to your global git ignore.

FAQ

Q. Why doesn't bak allow for deleting backup files?

A. bak should be extremely safe and never delete your data. It even refuses to overwrite a file, choosing to make a backup first if you choose to restore over an existing file (bak -k file, bak -rf file).

If you want to delete all files created by bak, you can find all bak files and pipe to xargs rm.

fd -uu -F '.bak.' | xargs rm