第一个 bash 脚本:计算机到 dropbox 备份帮助

第一个 bash 脚本:计算机到 dropbox 备份帮助

我在 /.backup 中有一个带有指向 cron.weekly 的符号链接的以下脚本。目标是每周加密、压缩并保存整个系统到 dropbox。这是我的第一个 bash 脚本,我肯定犯了上百个错误。我希望这里的某个人可以在我实际实施之前检查一下。我最不确定的是通配符、dropbox_uploader 的使用以及文件路径/权限。

#!/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

非常感谢!

编辑:为了回答一条评论,我正在尝试使用 tar 进行克隆备份。基本上,我想要备份我系统的完整快照。如果我的电脑在火灾中坏了,我希望能够从 Dropbox 获取备份并尽快恢复运行。每个应用程序、每个文件、每个存储库都一次性备份。

相关内容