Featured Post

My favorite links (ranked)

#1  Linux laptop powertuning painlessly (almost) #2 hard-drive-recovery #3 Chromebook Specifications #4 Internet BMW riders   #5 Findagra...

Thursday, December 4, 2025

Backing up partitions or home directory on a linux system

Handling partition backup archives

Using ntfsclone...

  

Create compressed archive:

sudo ntfsclone --save-image -o - /dev/sdXn | gzip -c > /path/to/archive/backup.img.tgz

Notes: Source device should not be mounted, destination path must point to another mounted device.

Restoring from backup above:

gunzip -c backup.img.gz | ntfsclone --restore-image --overwrite /dev/hda1 -

Notes: See notes just above... same status recommended. Also target partition

must be large enough to hold the unzipped data content. 

Using dd...

Create compressed archive:

sudo dd if=/dev/sdXN | gzip -c > /path/to/backup/filename_img.tgz

Notes: source device should not be mounted, destination path must point to another mounted device.

Restoring from backup above: 

sudo gunzip -c /path/to/backup/filename_img.tgz | dd of=/dev/sdXN
Notes: See notes just above... same status recommended. Also the target
partition must be large enough to hold all unzipped data content.

Ultimate Solution... Automate

Now that my home tree resides on a mounted separate partition from my SSD boot
device, I can now backup my data Using a shell script envoked by cron to keep my 
data securely backed up without human intervention.

#!/bin/sh
#
# shell script to run as root via cron scheduling which will keep my home
# directory backed up to device:
#      bd52d6a2-176c-482b-8c6b-164599a59882 (usb 3.0 ext. drive-500mb)
#
# rsync ${SWITCH} ${srcPath} ${dstPath} >> ${LOGPATH}backup${TIMESTAMP}.log
#
# (See static data settings below for required switches to efficiently sync
# my backup copy and remove deleted files etc.)

# I have added "power on/power off" commands to the external USB device before
# and after the backup rsync commands respectively to minimize the power on
# time incurred on that backup device. It, the device, is a 500GB 2.5" Western
# Digital HDD that has spent an unknown number of power on hours in a laptop
# computer in its unknown past.

# I may replace current backup device with a SSD eventually.

# Here is the crontab entry I employ to backup my home directory. Once a week
# currently (11pm on Sunday):
# 0 23 * * 0 /home/tim/bin/auto_home_backup.sh > /dev/null 2>&1 

# Note the "SHELLYPLUGIP" (ipaddress below) must match network address of device: In my case its a 4th generation Shelly plug device. Of course your mileage may vary here if you use another type of device for power switching.

# static data
CMD="/usr/bin/rsync "
SWITCHLIVE="-avW --progress --delete "
SWITCHTEST="-avnW --progress --delete "
TIMESTAMP=$(date +"%Y-%m-%d_%H:%M:%S")
SRCPATH=/home/tim/
DSTPATH=/media/tim/bd52d6a2-176c-482b-8c6b-164599a59882/home/tim
LOGPATH=/var/log/timbo/
SHELLYPLUGIP=192.168.1.123

# 1. Output startup timestamp to log file.
echo -n "Backup beginning at " > ${LOGPATH}backup${TIMESTAMP}.log 2>&1
date >> ${LOGPATH}backup${TIMESTAMP}.log 2>&1

# 2. Send external drive power on command.
curl -X POST -d '{"id":1, "method":"Switch.Set", "params":{"id":0, "on":true}}' http://${SHELLYPLUGIP}/rpc

# Optional: Add error checking for above curl command
#if [ $? -eq 0 ]; then
#    echo "Command sent successfully."
#else
#    echo "Failed to send command."
#fi 
# I probably won't care about this testing.

# 3. Wait 10 seconds for disk power up and auto mounting of external drive.
sleep 10

# 4. Show command structure to be used in log file.
echo "Running this command: ${CMD}${SWITCHLIVE}${SRCPATH} ${DSTPATH} >> ${LOGPATH}backup${TIMESTAMP}.log" >> ${LOGPATH}backup${TIMESTAMP}.log  2>&1

# 5. Test if mount point exists
if test -d "${DSTPATH}" ; then
# 6a. Backup live command !!!!
    ${CMD}${SWITCHLIVE}${SRCPATH} ${DSTPATH} >> ${LOGPATH}backup${TIMESTAMP}.log 2>&1
else
# 6b. Skip and notify rsync problem to log file.
    echo "Destination path not mounted rsync skipped!" >> ${LOGPATH}backup${TIMESTAMP}.log  2>&1
fi

# 7. Wait 5 seconds before power down for external drive.
sleep 5

# 8. Send external drive power down command.
curl -X POST -d '{"id":1, "method":"Switch.Set", "params":{"id":0, "on":false}}' http://${SHELLYPLUGIP}/rpc

# 9. Output shutdown timestamp to end of log file.
echo -n "Backup ending at " >> ${LOGPATH}backup${TIMESTAMP}.log 2>&1
date >> ${LOGPATH}backup${TIMESTAMP}.log 2>&1