Useful Git Commands

I’ve recently started using the git file versioning system and have to say it’s extremely useful and effective, and comes highly recommended.  These are some useful commands that I’ve started to need to use that aren’t always covered in the basic tutorials / chapters in books…

Revert changes to a local file (be careful this will lose all changes made!)

git checkout -- file.txt

Reverting a git pull

git reflog #to get the id of the head before the last pull
git reset --hard [id]

Showing the files changed, and the changes in the last commit

git log --name-status -1
git log -p -1

Show tracking branch information

git remote show origin

Revert a merge

git revert -m 1 

Show files changed in a commit

git show --stat 

Setting up Command Aliases in Linux

Aliases are a great way to run a command or script from the command window without having to either remember, or type everything in each time. This example is for the bash shell…

Open the file ~/.bashrc for editing:

vi ~/.bashrc

Add a new alias at the bottom of the file, for example:

alias connectServer='ssh -lusername'

Then install the .bashrc to make it available:

source ~/.bashrc

Now you can just type the name of the alias in the command window and it will perform the action defined in the .bashrc file:


Encrypting files in a directory

It can be useful to encrypt data, for example when moving backups on storage devices in case of loss/theft.

This can be easily done using ecrypt, it’s installed through adding the package ecryptfs-utils.

Then create a new folder:

mkdir ~/testFolder/

Now mount the folder using the command:

mount -t ecryptfs ~/testFolder/ ~/testFolder/

You will be asked to choose a key type…

Select key type to use for newly created files:
 1) openssl
 2) pkcs11-helper
 3) passphrase
 4) tspi

Choose option 3 (passphrase)

Enter a passphrase

Select cipher:
 1) aes: blocksize = 16; min keysize = 16; max keysize = 32
 2) blowfish: blocksize = 8; min keysize = 16; max keysize = 56
 3) des3_ede: blocksize = 8; min keysize = 24; max keysize = 24
 4) twofish: blocksize = 16; min keysize = 16; max keysize = 32
 5) cast6: blocksize = 16; min keysize = 16; max keysize = 32
 6) cast5: blocksize = 8; min keysize = 5; max keysize = 16
Selection [aes]:

Press enter to choose default (aes)

Select key bytes:
 1) 16
 2) 32
 3) 24
Selection [16]:

Press enter to choose default (16)

Enable plaintext passthrough (y/n) [n]:

Press enter to choose default (no)

Enable filename encryption (y/n) [n]:

Press enter to choose default (no)

Attempting to mount with the following options:
WARNING: Based on the contents of [/root/.ecryptfs/sig-cache.txt],
it looks like you have never mounted with this key
before. This could mean that you have typed your
passphrase wrong.

Would you like to proceed with the mount (yes/no)? :

Type yes, and press enter

Would you like to append sig [fccaf6dcf92c9c51] to
in order to avoid this warning in the future (yes/no)? :

Type yes, and press enter

Create a new file in ~/testFolder/ and enter some contents in your new file.

If you open or cat the file you will be able to view its contents.

Now umount the folder

umount ~/testFolder/

Try viewing the file again, and you should see lots of junk characters rather than your file in plain text.

To unlock the directory/files, you just need to mount the folder again using exactly the same procedure above.

mount -t ecryptfs ~/testFolder/ ~/testFolder/

Basic MySQL Backup Script

A basic bash script to backup all databases a few times a day, and compress the backup file. It stores the backup file using the name mysql_DAYOFTHEWEEK.sql.gz…


# Script to backup MySQL databases to a nas drive
# @author David C Boyce
# @created 2016-05-16

# Set up variables
FILE=/mnt/NASDRIVE/mysql_`date +"%A"`.sql

# Remove the old GZ File
rm ${FILE}.gz 2> /dev/null

# Make the backup
nice mysqldump --user=${USER} --all-databases > ${FILE}

# gzip the backup file
nice gzip ${FILE}

The backup can be scheduled to run at 9.30, 12.30, 17:30 and 20:30 by adding the following to crontab…

30 9,12,17,20 * * * /locationofscript/ >/dev/null 2>&1

There is a useful tool here to check/confirm what a crontab string will do…

Anti-Virus Scanning a Linux installation

ClamAV is a good tool for virus scanning linux workstations and servers.

It can be installed using the command:

sudo apt-get install clamav

The definitions can be updated with:

sudo freshclam

The whole system can be scanned using:

clamscan -r --bell -i /

This will only display infected files and rings a bell if any are found during the scan.

If lots of output is produced, or you need to store a log the output can be redirected to file…

clamscan -r --bell -i / > ~/scan.log

GNUCash sync open reports between workstations

GNUCash is one of my favourite pieces of opensource software, and I have been using it for many years for doing accounts. Just in case it helps anyone else using it across multiple different devices, one thing I really like about the firefox browser is that you can now sync the open tabs across all your devices. So when you get home you have the same set-up on screen as on the computer you left earlier at work. I wanted to do the same thing with GNUCash so the same reports would be open on my workstation at home without having to recreate them and get the same set-up.

It’s very easy to do, you basically just need to copy the gcm file from the most recent device you’ve worked on. On my computers these are stored in:

c:\users\[username]\.gnucash\books\ (windows)
/home/[username]/.gnucash/books (linux)

Overwrite the existing file on the device you want to start working on, and you should have all the reports open, just as you had working on the previous computer.


London Marathon 2016

A bit slower this year, just dipping inside 3 hours, with 2:58:52. It’s my slowest time, but I really can’t complain as I’ve only been running once or twice a week with our new arrival at home, and lots on at work. Hopefully next year will be quicker, and maybe an Autumn marathon in 2017 too.