I have the following script in /.backup with a symlink to cron.weekly. The goal is to have my entire system encrypted, compressed, and saved to dropbox every week. This is my first bash script and I'm sure i've made a hundred mistakes. I was hoping someone on here could give it a once over before I actually implement it. I'm most unsure about the wildcards, the use of dropbox_uploader, and file path/permissions.
#!/bin/bash
# backup script
# script needs to maintain root privleges
read -p "Begin Backup? This may take several minutes... (Y/n)" value
if [ ! $value =~ ^(Y|y|yes|Yes|YES) ]; then
read -p "Reschedule backup for tomorrow (Y/n)?" schedDate
if [ $schedDate =~ ^(Y|y|yes|Yes|YES) ]; then [ at tomorrow -f '/.backup/backup'; exit ]; else; exit; fi # reschedule for tomorrow
fi
if dpkg-query -W curl; then apt-get install curl; fi # if curl dne, install it
if dpkg-query -W dropbox_uploader; then
curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o '/bin/dropbox_uploader.sh' # if dropbox_uploader dne, install it
chmod +x '/bin/dropbox_uploader.sh'
fi
DBuploader = '/bin/dropbox_uploader.sh'
mkdir -p '/.backup/temp' # if dependent directories dne, create them. what is $HOME for cron job?
Tarball='/.backup/temp/backup-$(date +"%Y-%m-%d").tar.gz'
apt-get update
apt-get upgrade
if [tar -zcvpf $Tarball --directory='/' --exclude='/.backup/temp' --exclude='/home/*/Dropbox' . ]; then
if [ ! -f '/.backup/passfile.txt' ]; then
read -sp 'Password: ' passvar > '/.backup/passfile.txt'
fi
openssl enc -e -aes-256-cbc -salt -in $Tarball -out $Tarball -pass file:'/.backup/passfile.txt'
if [ $DBuploader -qpf '/home/*/bin/.dropbox_uploader' upload $Tarball '/.PCbackup']; then # .dropbox_uploader is user specific. will upload to dropbox of every user
rm $Tarball
else
echo 'check /home/*/bin/.dropbox_uploader exists'
fi
# remove old backups
oldFiles = $DBuploader list '/.PCbackup' | awk '{print $2}' | sort -nr | awk 'NR>2'
$DBuploader delete '/.PCbackup/$oldFiles'
else
echo 'Failed creating backup.'
fi
# write decrypt script/instructions
# /etc/cron.weekly this has root privleges and is controlled by anacron
# ln -s /.backup/backup /etc/cron.weekly/backup
Thanks so much!
EDIT: to answer a comment, I am trying to use tar to make a clone backup. Basically I want a complete snapshot of my system backed up. If my computer dies in a fire, i want to be able to get my backup from Dropbox and be back up and running ASAP. Every app, every file, every repository in one shot.
if [ ! $value =~ ^(Y|y|yes|Yes|YES) ]
, you should use the bash keyword[[
instead of the command[
as the later does not understand the=~
operator. Also, the^
is superfluous. – Stefan Hamcke Dec 04 '18 at 16:39read -p...
but it is a cron job. Are you expecting to always be at your computer when it runs? – j-money Dec 04 '18 at 19:39rsync
https://wiki.archlinux.org/index.php/Rsync#As_a_backup_utility – j-money Dec 04 '18 at 21:36/
that don't really "exist" on disk, like /sys...) (PPS there's a bashset
ting to help see what a script is doing) – Xen2050 Dec 05 '18 at 09:01