Monday, April 15, 2013

Vacuum cleaning of $HOME to back it up


I've started with a simple script to compress Firefox's sqlite databases before creating a backup of my $HOME directory

alex@xubuntu:~$ cat bin/vacuumFirefox.sh
#!/bin/bash

[ -n "$(pidof firefox)" ] && {
  echo "ERROR: firefox is still running, close it first!"
  exit 1
}

counterBefore=0
counterAfter=0

for i in ~/.mozilla/firefox/*/*.sqlite; do
  fileSizeBefore=$(du -b $i|awk '{ print $1 }')
  counterBefore=$((counterBefore + fileSizeBefore))
  sqlite3 "$i" vacuum
  fileSizeAfter=$(du -b $i|awk '{ print $1 }')
  counterAfter=$((counterAfter + fileSizeAfter))
  echo "$fileSizeBefore $fileSizeAfter - $(basename $i)"
done
echo "Bytes saved: $(( counterBefore - counterAfter ))"

Here is a sample output:

alex@xubuntu:~$ bin/vacuumFirefox.sh 
589824 589824 - addons.sqlite
98304 98304 - chromeappsstore.sqlite
7168 7168 - content-prefs.sqlite
524288 4096 - cookies.sqlite
6144 3072 - downloads.sqlite
589824 589824 - extensions.sqlite
196608 163840 - forecastfox.sqlite
128000 8192 - formhistory.sqlite
5120 4096 - permissions.sqlite
10485760 344064 - places.sqlite
2048 2048 - search.sqlite
112640 111616 - signons.sqlite
262144 262144 - stylish.sqlite
34938880 34938880 - urlclassifier3.sqlite
3072 3072 - webappsstore.sqlite
524288 524288 - addons.sqlite
229376 229376 - content-prefs.sqlite
524288 131072 - cookies.sqlite
98304 98304 - downloads.sqlite
458752 458752 - extensions.sqlite
196608 196608 - formhistory.sqlite
65536 65536 - permissions.sqlite
10485760 1179648 - places.sqlite
327680 327680 - signons.sqlite
98304 98304 - webappsstore.sqlite
Bytes saved: 20518912

Later this script became a part of a bigger script to backup my $HOME:

alex@xubuntu:~$ cat bin/backupHome.sh
#!/bin/bash

homeDir=$HOME
destDir=/media/nas/Alex/backup
archName="$(whoami).$(hostname)"

# files or directories to exclude
printExcludes()
{
awk '{printf("--exclude '\''%s'\'' ",$1)}'  ‹‹EOEXCL
.encfs
.davfs2
.smbcredentials
.cache
.eagle
.dbus
.pulse*
.thumbnails
Downloads
EOEXCL
}

# vacuum firefox's sqlite files
vacuumFirefox()
{
  pidof firefox && {
    echo "ERROR: firefox is still running, close it first!"
    return 1
  }
  which sqlite3 &>/dev/null || {
    echo "ERROR: sqlite3 is not installed. Do 'sudo apt-get install sqlite3' first"
    return 1
  }

  local counterBefore=0
  local counterAfter=0
  local fileSizeBefore fileSizeAfter
  local hDir=$homeDir
  [ -z "$hDir" ] && hDir=~/

  for i in "$hDir"/.mozilla/firefox/*/*.sqlite; do
    fileSizeBefore=$(du -b $i|awk '{ print $1 }')
    counterBefore=$((counterBefore + fileSizeBefore))
    sqlite3 "$i" vacuum
    fileSizeAfter=$(du -b $i|awk '{ print $1 }')
    counterAfter=$((counterAfter + fileSizeAfter))
    echo "$fileSizeBefore $fileSizeAfter - $(basename $i)"
  done
  echo "Bytes saved: $(( counterBefore - counterAfter ))"
  return 0
}

# cleaning phase
vacuumFirefox || exit 1
cd "$homeDir"
xargs -d \\n -I {} bash -c "rm -rf {}" ‹‹EOLIST
.thumbnails/*
.cache/*
.local/share/Trash/*
.adobe/Flash_Player/AssetCache/*
.mozilla/firefox/*/Cache/*
EOLIST

# creating an archive
if [ -d "$destDir" ]; then
  exe=$(echo "tar -czf \"${destDir}/${archName}.$(date +%Y-%m-%d).tgz\" $(printExcludes) -C $homeDir ./")
  echo "$exe" 
  eval "$exe" 
else
  echo "ERROR: Directory $destDir doesn't exist. Canno't create an archive"
  exit 1
fi

Inside of the printExcludes I'm also excluding files or directories that contain sensitive informations like passwords in open text.

Note: one can also install the BleachBit utility that can perform a similar job

No comments: