How to hack the filesystem

From ChengLab
Revision as of 06:12, 21 April 2021 by Kevin (talk | contribs) (How to mount an external disk on Ubuntu)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

How to mount an external disk on Ubuntu

fdisk -l
parted /dev/sdd
mklabel > gpt > yes
mkpart primary 2048s 100%
print
quit
mkfs.ext4 /dev/sda1
vi /etc/fstab
/dev/sda1 /media/flash1 ext4 defaults 0 0
mount /media/flash1

How to do simple backups

tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system / 

To understand what is going on, we will dissect each part of the command.

tar - is the command that creates the archive. It is modified by each letter immediately following, each is explained bellow.

c - create a new backup archive.

v - verbose mode, tar will print what it's doing to the screen.

p - preserves the permissions of the files put in the archive for restoration later.

z - compress the backup file with 'gzip' to make it smaller.

f <filename> - specifies where to store the backup, backup.tar.gz is the filename used in this example. It will be stored in the current working directory, the one you set when you used the cd command.

--exclude=/example/path - The options following this model instruct tar what directories NOT to backup. We don't want to backup everything since some directories aren't very useful to include. The first exclusion rule directs tar not to back itself up, this is important to avoid errors during the operation.

--one-file-system - Do not include files on a different filesystem. If you want other filesystems, such as a /home partition, or external media mounted in /media backed up, you either need to back them up separately, or omit this flag. If you do omit this flag, you will need to add several more --exclude= arguments to avoid filesystems you do not want. These would be /proc, /sys, /mnt, /media, /run and /dev directories in root. /proc and /sys are virtual filesystems that provide windows into variables of the running kernel, so you do not want to try and backup or restore them. /dev is a tmpfs whose contents are created and deleted dynamically by udev, so you also do not want to backup or restore it. Likewise, /run is a tmpfs that holds variables about the running system that do not need backed up.

Scripts

#!/bin/bash 
dir="$1"
BAK_DIR=/media/disk2/kevin/Backup
[ $# -eq 0 ] && { echo "> Usage: $0 dir-name"; echo "> The backup directory is $BAK_DIR"; exit 1; }
if [ ! -d $dir ]
then
   echo "Directory $dir not exist!"
   exit 1
else
   tar -cvpzf ${BAK_DIR}/BAK_${dir}.tar.gz ${dir}
fi