Rename Debian packages according to version

This is a small bash utility function to rename deb archives according to their version.

rename-deb () 
{ 
    base="${1%.deb}";
    version="$(dpkg-deb -f $1 Version)" || return 1;
    new="$base-$version.deb";
    mv -i "$1" "$new";
    echo "$1 -> $new"
}

You can either run it one time in your shell or define it in your ~/.bash_aliases.

Example:

$ rename-deb zoom_amd64.deb
zoom_amd64.deb -> zoom_amd64-5.5.7011.0206.deb

Generating Secure Passphrases on the Command Line

The following snippet should work on every system that has coreutils.

$ shuf /usr/share/dict/words --repeat --random-source /dev/random -n 5

You can swap /usr/share/dict/words with any good word list, like EFF’s or Arnold Reinhold’s Diceware list.

You can also add it to your ~/.bash_aliases for ease of use:

alias passphrase="shuf ~/dotfiles/misc/diceware8k.txt --repeat --random-source /dev/random -n"

Then you can easily use it in bash:

$ passphrase 5
notch
kane
to
drag
cater

Temporarily Disabling Bash History

Say that you’ve got to pass some password as a command-line argument to something. It would probably be a bad idea to store it in your ~/.bash_history, but clearing the file isn’t desired either. So you need to temporarily disable the command history for the current session. You can do it by unsetting the HISTFILE environment variable.

unset HISTFILE

The result is that while the session is active, you can access the history as usual, but it won’t be saved to disk. History for other sessions will behave as usual.

Django Backup Script

This is a backup script for Django projects. It’s able to automate backups of both the database and files to a local folder and a remote FTP server. It is somewhat old and has a few limitations, mainly supporting only MySQL and not supporting the new way of specifying databases introduced in Django 1.2.

It’s loosely based on my WordPress backup script and inspired the database settings auto-detection found in the newer WordPress backup script.

Usage is simple:

$ django_backup /path/to/my/proj
$ django_backup --db-only /path/to/my/proj

The latter command only backs up the database.

The script uses a few configuration variables at the top of the script to set the folder where the local backups are kept and the remote FTP server settings. The database settings are extracted directly from the settings.py of the backed-up project.
Continue reading Django Backup Script

Improved FTP Backup for WordPress

This script backs up both the database and files of a WordPress blog to a remote FTP server (while keeping a local copy). It’s an update of my WordPress Backup to FTP script. The main changes are auto-detecting database settings and better support for caching plugins (specifically WP-Cache). The new version makes it easier to back up multiple WordPress blogs to the same FTP server.
Continue reading Improved FTP Backup for WordPress

Batch Renaming Using sed

I was reorganizing my music library and decided to change the naming convention I’d used. This task was just asking to be automated. Since the filename change could be described using a regular expression, I looked for a way to use sed for the renaming process.

The files I had followed the filename pattern ARTIST – SONG – TRACK – ALBUM

James Brown - I Got You (I Feel Good).ogg  - 01 - Classic James Brown

I wanted to rename them to ARTIST – ALBUM – TRACK – NAME

James Brown - Classic James Brown - 01 - I Got You (I Feel Good).ogg

Describing the change as a sed program is easy:

s/(.*) - (.*) - (.*) - (.*).ogg/1 - 4 - 3 - 2.ogg/

Now all that has to be done is to pass each filename to mv and pass it again after it has gone through the sed script. This can be done like this:

for i in *; do
  mv "$i" "`echo $i | sed "s/(.*) - (.*) - (.*) - (.*).ogg/1 - 4 - 3 - 2.ogg/"`";
done

The important part is

`echo $i | sed "s/(.*) - (.*) - (.*) - (.*).ogg/1 - 4 - 3 - 2.ogg/"`

which pipes the filename to sed and returns it as an argument for mv.

To see what renaming will be done, one can alter the above command a bit and get

for i in *; do
  echo "$i" "->" "`echo $i | sed "s/(.*) - (.*) - (.*) - (.*).ogg/1 - 4 - 3 - 2.ogg/"`";
done

which will effectively print a list of lines in the form oldname -> newname.

Of course, this technique isn’t limited to the renaming I’ve done. By changing the pattern given to sed, one can do any kind of renaming that can be described as a regular expression replacement. Also, one can change the globbing (the *) in the for loop to operate only on specific files that match a given pattern in the directory, instead of all of them.

WordPress Backup to FTP

Update: A newer version of the script is available.

This script allows you to easily back up your WordPress blog to an FTP server. It’s actually a modification of my WordPress Backup to Amazon S3 Script, but instead of saving the backup to Amazon S3, it uploads it to an FTP server. Another update is that now the SQL dump includes the database creation instructions, so you don’t need to create it manually before restoring from the backup.

Although I’ve written it with WordPress in mind (to create backups of my blog), it isn’t WordPress-specific. It can be used to back up any website that consists of a MySQL database and files. I’ve successfully used it to back up a MediaWiki installation.
Continue reading WordPress Backup to FTP

Back Up a SourceForge-Hosted SVN Repository – sf-svn-backup

SourceForge urges its users to back up their projects’ code repositories. As I have several projects hosted on SourceForge, I should do it too. Making the backups isn’t complicated at all, but because it isn’t properly automated, I’ve been lazy about it.

sf-svn-backup was written to simply automate the process. The script is pretty simple to use: just pass the project name as the first argument, and the script will write the dump file to stdout.

For example:

sf-svn-backup openyahtzee > openyahtzee.dump

The project name should be its UNIX name (e.g. openyahtzee and not Open Yahtzee). Because the script writes the dump file directly to stdout, it’s easy to pipe the output through a compression program such as gzip to create compressed SVN dump files.

tarsum – Calculate Checksums for Files inside a Tar Archive

Update: I’ve released tarsum-0.2, a new version of tarsum.

Some time ago, I got a hard disk back from data recovery. One of the annoying issues I encountered with the recovered data was corrupted files. Some files looked like they were recovered successfully, but their content was corrupted. The ones that were configuration files were usually easy to detect, as they raised errors in programs that tried to use them. But when such an error occurs in some general text file (or inside the data of an SQL dump), the file may seem perfectly fine unless closely inspected.

I have a habit of storing old backups on CDs (they are initially made to online storage). I do it in order to reduce backup costs. But the recovered/corrupted data issue raised some concerns about my ability to recover using these disks. Assuming that I have a disk failure, and I couldn’t recover from my online backups for some reason, how can I check the integrity of my CD backups?

Only storing and comparing a hash signature for the whole archive is almost useless. It allows you to validate whether all the files are probably fine, but it can’t tell apart one corrupted file in the archive from a completely corrupted archive. My idea was to calculate a checksum (hash) for each file in the data and store the signature in a way that would allow me to see which individual files are corrupted.

This is where tarsum comes to the rescue. As its name implies, it calculates a checksum for each file in the archive. You can download tarsum from here.

Using tarsum is pretty straightforward.

tarsum backup.tar > backup.tar.md5

Calculates the MD5 checksums of the files. You can specify other hashes as well, by passing a tool that calculates them (it must work like md5sum).

tarsum --checksum=sha256sum backup.tar > backup.tar.sha256

To verify the integrity of the files inside the archive, we use the diff command:

tarsum backup.tar | diff backup.tar.md5 -

where backup.tar.md5 is the original signature file we created. This is possible because the signatures are sorted alphabetically by the file name inside the archive, so the order of the files is always the same.

Note that if you use an updated version of GNU tar, tarsum can also operate directly on compressed archives (e.g. tar.bz2, tar.gz).