Hacksenkessel's Blog

Hacking, Pentesting, Algorithms, Computer Science

Simple backup script for linux —

Sometimes you just need a simple backup script so that you can backup and restore your entire operating system. In some situations this is inevitable, like when you choose to encrypt your entire existing operating system including your root mountpoint. You might want to backup your plain/unencrypted harddisk, encrypt it and restore its contents to the encrypted mountpoint.

It’s not advisable to backup a running operating system, unless you know exactly what you’re doing. A consistent backup can be created in Single User Mode.

backup script:


typeset    dir=${1:?"usage: $0 backupdir"}

[ $(id -u) -ne 0 ] && echo "you need to be root" >&2 && exit 1
[ -d ${dir}/ ] || { echo "$dir is not a directory"; exit 1; }

echo "Starting $(hostname -f)'s backup at $(date) ..."
time tar --exclude=/dev/*                    \
         --exclude=/lost+found/*             \
         --exclude=/media/*                  \
         --exclude=/mnt/*                    \
         --exclude=/proc/*                   \
         --exclude=/var/cache/apt/archives/* \
         --exclude=/sys/*                    \
         --exclude=/tmp/*                    \
         --exclude=/usr/ports/*              \
         --exclude=/${dir}/*                 \
         -cpf - / | gzip --best > ${dir}/backup_root_$(hostname)_$(date +"%Y%m%d").tar.gz

[ $? -ne 0 ] && {
  echo "Unable to compress root directory"
	exit 1
echo "$(hostname -f)\'s backup ended successfully at $(date)"
exit 0

Backup your linux operating system via:

./backup_root.sh <mountpoint for backupfile>


cd <your empty root mountpoint>
tar xzf <backupfile>.tar.gz



Categorised as: Shellscript

Comments are disabled on this post