DEV Community

Cover image for Formatting an external drive and Implementing Automatic Backup
Ethan
Ethan

Posted on

Formatting an external drive and Implementing Automatic Backup

Introduction

Hello! Today I decided to finally start backing up my files to a remote external drive, just in case something went wrong and it's important to periodically backup your data. ๐Ÿ˜€

The OS I'm currently using is Arch Linux but it should work on almost any flavor of Linux.


Formatting the external drive

Before we can format the device we need to actually locate it.

sudo fdisk -l
Enter fullscreen mode Exit fullscreen mode

This command will list all the devices that are connected to your machine and they will all be located under /dev (dev being short for device). The location should be noted as it will be used in the next commands. (in my case /dev/sda).

Next we need to make sure the device is not mounted before any formatting takes places, this can be done via:

sudo umount /dev/sda
Enter fullscreen mode Exit fullscreen mode

The device may not have been mounted but it's best to make sure.
Now we can safe-fully format the device, giving the option ext4 as I'm using Linux.

sudo mkfs -t ext4 /dev/sda
Enter fullscreen mode Exit fullscreen mode

You can check to see if it's been formatted correctly via:

lsblk -f
Enter fullscreen mode Exit fullscreen mode

Finally we need to mount the backup device by creating a mount point and then mounting the device to that location.

sudo mkdir /mnt/backup
sudo mount /dev/sda /mnt/backup
Enter fullscreen mode Exit fullscreen mode

Creating the backup script

#!/bin/bash
# Script to backup directories to external disk

backup_files="/home /etc /root /boot /opt"
backup_location="/mnt/backup"

# Create the filename
day=$(date +%A)
file_name="backup-$day.tgz"

tar -Pczf $backup_location/$file_name $backup_files
Enter fullscreen mode Exit fullscreen mode

What this script basically does is backup a number of directories to an external device (the one that was mounted in the previous step).

Finally make it executable. ๐Ÿ˜Ž

chmod +x backup.sh
Enter fullscreen mode Exit fullscreen mode

The script can also be found below:

https://gist.github.com/ethand91/d164602f1c298178cfb2b51894045a61

Feel free to add any other directories etc that you want to backup. ๐Ÿ˜‰


Automating the backup process

Backups should be taken periodically, crontab can be used to make sure the script is run daily.

Make sure cron is installed on the system, after which you just need to add the task.
Caution: if you already use cron make sure not to mistake -e with -r. (I learned the hard way) ๐Ÿฅฒ

sudo crontab -e

# Add the following one liner
0 13 * * * bash /root/backup.sh
Enter fullscreen mode Exit fullscreen mode

Here I set it up so that the script runs daily at 13:00, which is my lunch break. ๐Ÿค“

Feel free to change the backup time/script location.


Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

โ€œBuy Me A Coffeeโ€

Top comments (2)

Collapse
 
webreflection profile image
Andrea Giammarchi

It'd be great to have a script that does incremental backup, instead of keep growing in size each day with a whole backup of every single folder ... I mean, I get the "time-machine" like approach, but this easily won't scale if you deal with big files, databases, and so on, as you grow by X GB of data each day ... at lest I'd wipe X days/months ago to preserve space in the external drive.

Collapse
 
ethand91 profile image
Ethan

This script was mainly for my personal PC.
Yes if you're dealing with big files, databases etc, the backup destination will eventually run out of space. In that scenario you will definitely need to delete old backups before creating new ones. :)