0

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.

Joe Blow
  • 101
  • 1
    Have you tried running it yourself? If so what are the errors? Or are you deliberately sending us on a goose hunt? – j-money Dec 04 '18 at 16:37
  • In the statement 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:39
  • This seems to try to split the difference between cloning-backup and a simple file-backup. It's neither, so it may not work as you intend. Curious to see if you can restore the system from a backup tarball, regardless of the dropbox element. – user535733 Dec 04 '18 at 17:31
  • @j-money lol not deliberately. It's my first script and I'm afraid to run it. Wanted to make sure it looked alright.@Stefan thank you very much, i lifted that line directly from somewhere online. Definitely three one i was last sure about. – Joe Blow Dec 04 '18 at 18:54
  • my advice though would be, rather than running to ask ubuntu, just try it out first :) you might find that when you run into errors, they're easier to solve than maybe you were thinking! One extra note, you ask read -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:39
  • @j-money I'm not familiar enough with Linux to be able to debug. I'd rather not run into a fatal error cause by a half completed script when it can be avoided. You have a good point though. If there's no response after 1 min I'll have the script continue anyways. – Joe Blow Dec 04 '18 at 21:29
  • That's why they made vm's and conatainers ;) if you're apprehensive to running things on bare metal!!! As far as your edit, you may want to take a look into rsync https://wiki.archlinux.org/index.php/Rsync#As_a_backup_utility – j-money Dec 04 '18 at 21:36
  • @j-money I looked into it, as well as rclone, clonezilla, and several others. The only problem is they require you back up to a live cd or something equivalent. I guess I could mount a symlink to my dropbox or something, but seems easier to do this. – Joe Blow Dec 05 '18 at 04:47
  • You know Ubuntu is usually very easy to download & install, why not just keep a home backup and maybe the few custom settings in /etc or wherever, and forget about the gigs of easy to reinstall (and perhaps updated weekly or monthly anyway) programs and libraries? (PS there's some things in / that don't really "exist" on disk, like /sys...) (PPS there's a bash setting to help see what a script is doing) – Xen2050 Dec 05 '18 at 09:01
  • Hi, here are two examples that you can investigate: https://github.com/pa4080/simple-backup-solutions – pa4080 Dec 05 '18 at 09:04
  • @Xen2050 very good point. I'd follow it, but I have so many packages I may not remember to install and I have no idea where in the file system these things are stored. If i were to just back up '/home/' i may not get packages installed outside this directory. I could use aptik to backup this list. In which case is my script above still correct? Thanks – Joe Blow Dec 05 '18 at 09:57
  • There's several fairly easy ways to see all the packages installed on a system, like https://askubuntu.com/questions/17823/how-to-list-all-installed-packages or https://askubuntu.com/questions/9135/how-to-backup-settings-and-list-of-installed-packages – Xen2050 Dec 05 '18 at 10:26
  • @Xen2050 maybe we could DM as opposed to a long comments thread. I tried the list command and output a very long file. I could backup /home/ and just keep this list, but it doesn't save applications. For example I use MatLab. If my drive is lost and I try to reinstall on another system, not only will I have to get all the packages, but one by one go and reinstall each application (this also means dealing with mathworks, which is annoying). It'd be so much simpler to just untar everything at once. – Joe Blow Dec 06 '18 at 04:40
  • The package manager (apt) handles the details of what dependent packages are needed, the only trick is knowing which main or "key" packages you want to install that will pull all the required ones with them. Starting from a fresh install it should be easy to compare, or track what main packages you've installed, but trickier now (I think some answers in the Q's I liked have suggestions) - Unless your programs aren't in regular sources. [SE doesn't actually have private messages, just the @ tagging that alerts a user, and chat rooms somewhere, but this seems appropriate to the Q here] – Xen2050 Dec 06 '18 at 10:52
  • @Xen2050 weeeeeeellllpp this is why i asked this here. I gave the above script a try and can no longer get into my computer. It ran for about 10 minutes then crashed, after which my / directory had exactly 0 bytes left on it. Now when i try to log in I see https://imgur.com/gallery/07D1yrZ – Joe Blow Dec 08 '18 at 02:18
  • The screenshot shows all green OK's... maybe it wasn't the script but the apt upgrade...? Possibly ran out of space? The script basically made a tar, then an openssl enc (fyi enc might be somewhat insecure, it's key derivation function's iteration count is fixed at 1, among other issues; you can pipe tar into gpg), and sent it to dropbox. (Having a good errorlog helps sometimes, can redirect stdout &/or stderr to their own file (tee)). – Xen2050 Dec 08 '18 at 11:14
  • Figured it out. The backup included /proc so it created a loop and filled up the hdd. – Joe Blow Dec 11 '18 at 00:39

0 Answers0