Skip to content

Instantly share code, notes, and snippets.

@qtaped
Last active January 9, 2024 21:53
Show Gist options
  • Save qtaped/b705907a65cda2ccb4e4c4fd73ad9703 to your computer and use it in GitHub Desktop.
Save qtaped/b705907a65cda2ccb4e4c4fd73ad9703 to your computer and use it in GitHub Desktop.
backup-tm
#!/bin/sh
# rsync Apple TimeMachine-like backup script by Q.
# thanks to:
# http://habrahabr.ru/post/149059/
# http://www.jan-muennich.de/linux-backups-time-machine-rsyn
#uncomment to use notification
#export DISPLAY=:0
#export XAUTHORITY=/home/qntn/.Xauthority
#export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
# datetime of backup
date=$( date "+%Y-%m-%d-%H-%M-%S" )
# SETTINGS
src="/" # source to backup, / for all
dst="/run/media/external_drive/folder/" # backup source to destination dont forget / at the end of path
log="/home/user/.local/share/lastbackup"
lockfile_pathname="${dst}lock"
max_backup_count=5 # maximum backups keep
exclude_list="${dst}exclude.lst" # excluse list
# welcome screen
echo " _ _ "
echo "| |__ __ _ ___| | ___ _ _ __ "
echo "| '_ \\ / _\` |/ __| |/ | | | | '_ \\ "
echo "| |_) | (_| | (__| <| |_| | |_) |"
echo "|_.__/ \\__,_|\\___|_|\\_\\\\__,_| .__/ "
echo " |_| "
echo -e "\n:: Running time-machine backup...\n"
if [ ! -d "${dst}" ]
then
echo -e "Destination ${dst} do not exists.\nAborting…"
exit 2
elif [ -f "${lockfile_pathname}" ]
then
echo -e "Lockfile ${lockfile_pathname} exists.\nAborting…"
exit 2
elif [ ! -f "${exclude_list}" ]
then
echo -e "Exclude list ${exclude_list} do not exists.\nAborting…"
exit 2
fi
echo -e " - Source: \t${src}"
echo -e " - Destination: \t${dst}"
if [ -f "${log}" ]
then
echo -e " - Last backup:\t$(cat ${log})"
else
echo -e " - Last backup:\tfile log not found."
fi
echo
read -p "→ Continue? [y/n] " -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
# first remove all but max_backup_count number of backups & logs
# then run rsync
sudo sh -c "
echo 'Found backups:'
find ${dst} -maxdepth 1 -type d -name '20*' | xargs -n1 basename | sort
find ${dst} -maxdepth 1 -type d -name '20*' | sort | head -n -${max_backup_count} | xargs -i -n1 sh -c 'echo Removing {}...; rm -fr {};echo Done.;'
find ${dst} -maxdepth 1 -type f -name '20*.log' | sort | head -n -${max_backup_count} | xargs -i -n1 sh -c 'rm -f {}; echo {} deleted.'
touch ${lockfile_pathname} &&
time rsync \
--archive \
--xattrs \
--verbose \
--human-readable \
--inplace \
--delete \
--delete-excluded \
--exclude-from=${exclude_list} \
--link-dest=${dst}latest \
${src} \
${dst}${date} | tee ${dst}${date}.log ;
rm -f ${dst}latest &&
ln --symbolic --relative --verbose ${dst}${date} ${dst}latest &&
echo -e '\nBackup done.' &&
echo ${date} | tee ${log} &&
rm ${lockfile_pathname};
sync"
# List actual installed packages (archlinux / pacman)
# echo "List of installed packages ( ${date} )" > ${dst}installed_pkg_list
# pacman -Q >> ${dst}installed_pkg_list
# Notify end of backup
# dunstify -i folder-backup "Backup done." "${date}"
# Include
+ /dev/console
#+ /dev/initctl
+ /dev/null
+ /dev/zero
# Exclude
- /dev/*
- /proc/*
- /sys/*
- /tmp/*
- *lost+found
- /media/*
- /run/*
- /mnt/*
- /var/tmp/*
- /var/cache/*
- /var/log/*
- /var/lib/dhcpcd/*
- /usr/src/*
- /home/user/.cache/*
- /home/user/downloads/*
- /home/user/.local/share/Trash/*
- /home/user/.gvfs
@qtaped
Copy link
Author

qtaped commented Jan 3, 2022

Edit and copy exclude.lst to backup destination folder:

vim exclude.lst
cp exclude.lst /run/media/external_drive/folder/

Give executable rights and run backup-tm.sh:

chmod +x backup-tm.sh
sh backup-tm.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment