将Linux配置,脚本和文档备份到Gmail

在gmail.com上,我看到了这个:

使用了0.38 GB(2%)的15 GB

我有20 GB的30 GB分区,里面装满了Ubuntu,但可能只有1 GB的重要文件,为什么不使用这个免费的云存储进行备份呢?

是否有一个程序要做到这一点? 我可能会写一个但是在我开始之前想问一下。

我还想要文件压缩和分段,因为有些系统有10 MB的附件限制(比如工作中的那个)。

谢谢 :)

编辑2018年4月3日

下一节的历史文章

答案的原始部分在下一部分保持不变,以供历史参考试验和错误

用于创建.tar文件的备份脚本

这是当前的备份脚本:

 #!/bin/bash # NAME: daily-backup.sh # PATH: /mnt/e/bin # DESC: Backup scripts, documents and configuration files to .tar # DATE: July 11, 2017. Modified Oct 20, 2017. # PARM: 1=backup file name. Extension .tar automatically appended. # NOTE: To include MBR (Master Boot Record) in backup create an image using: # sudo dd if=/dev/sda of="$HOME/.mbr.sav" bs=512 count=1 # NOTE: CLONE CURRENT INSTALLATION TO NEW MACHINE # ========================================= # To restore use Live USB to install Ubuntu alongside Windows 10 # Connect to network with password xxxxxxxxx # Install Google Chrome # (https://askubuntu.com/questions/510056/how-to-install-google-chrome): # wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub # | sudo apt-key add # echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ # stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list # sudo apt update # sudo apt install google-chrome-stable # Open gmail.com and download attachment `$1` which is usually called # Backup-yymmdd-DayOfWeekName.tar # Make missing home/bin directory which tar doesn't create automatically: # mkdir ~/bin # Restore the daily backup using: # sudo tar -xvf Backup-yymmdd-DayFfWeekName.tar -C / # yar -xvf Backup-yymmdd-DayFfWeekName.tar -C / # Patch /etc/default/grub with new machine parameters, ie for nvme use: # acpiphp.disable=1 # Use `sudo apt install aptitude-common` # Clone packages using `aptitude-create-state-bundle` on Source # Copy state-bundle.tar file from Source to Target machine # Restore packages using `aptitude-run-state-bundle` on Target # Manually copy ~/Pictures, ~/Videos, etc. not in daily backup. # sudo update-grub # NVMe suspend/resume acpiphp.disable=1 # sudo update-initramfs # to get plymouth sunrise splash screen if [[ $# -ne 1 ]]; then echo 'One argument required for file name, eg "Backup-2017-10-21-Saturday"' echo '.tar will automatically be added as a file extension' exit 1 fi FileName="$1.tar" HomeDir="/home/Me" # Required for cron compatibility EmailAddr="MyEmail@gmail.com" cd $HomeDir || exit 1 dpkg --get-selections > .packages # List of installed applications tar -cvpf "$FileName" bin # create .tar & add user scripts tar -rvpf "$FileName" /usr/local/bin # add global root-based scripts tar -rvpf "$FileName" /etc/cron* # crontab, cron.d, cron.daily, etc tar -rvpf "$FileName" /etc/system* # systemd files: login.conf, etc. tar -rvpf "$FileName" /lib/systemd/system-sleep tar -rvpf "$FileName" /etc/rc.local # Startup script: calls zaprestore. tar -rvpf "$FileName" /etc/sudoers # 120 minute sudo, stars in password tar -rvpf "$FileName" /etc/default/grub # bootstrap loader tar -rvpf "$FileName" /boot/grub # Custom grub fonts and splash... tar -vpf "$FileName" /usr/share/plymouth # ... screen (plymouth) tar -rvpf "$FileName" /usr/share/plymouth/themes/earth-sunrise/ tar -rvpf "$FileName" Desktop # files and links on desktop tar -rvpf "$FileName" Documents/*.od* # Libre Office: *.ods, *.odt, etc. # Trusted keys to install from third party PPAs tar -rvpf "$FileName" /etc/apt/trusted.gpg tar -rvpf "$FileName" /etc/apt/trusted.gpg.d # Sources for repositories - 1) Main single file - 2) directory of files tar -rvpf "$FileName" /etc/apt/sources.list tar -rvpf "$FileName" /etc/apt/sources.list.d # find all $HOME/.config files and add to .tar find .* -maxdepth 0 -type f -exec tar -rvf "$FileName" {} + # Nautilus custom scripts tar -rvpf "$FileName" .local/share/nautilus/scripts # /etc/udev rules tar -rvpf "$FileName" /etc/udev/rules.d # /etc/rc.local tar -rvpf "$FileName" /etc/rc.local # /etc/X11/xorg.conf.d tar -rvpf "$FileName" /etc/X11/xorg.conf.d # /mnt/e - shared WSL + Linux tar -rvpf "$FileName" /mnt/e/bin tar -rvpf "$FileName" /mnt/e/Documents echo "Wait a minute... Emailing: $EmailAddr" # From: https://internetlifeforum.com/gmail/2251-gmail-some-file-types-blocked-fix-how-go-around/ # cat archive.tar.gz | base64 > file # then i sent the file via email: # echo "Base64 encoded file" | mutt -a file -s subject -- mymail@gmail.com # then mail was delivered properly! # then when one need to get readable archive again, he need to decode it by base64. In my case i do it via linux command line: # cat file | base64 -d > decodedarchive.tar.gz FileName64="$FileName".64 cat "$FileName" | base64 > "$FileName64" echo -e "to: $EmailAddr\nsubject: $FileName64\n" | \ (cat - && uuencode "$FileName64" "$FileName64") | ssmtp "$EmailAddr" #echo -e "to: $EmailAddr\nsubject: $FileName\n" | \ # (cat - && uuencode "$FileName" "$FileName") | ssmtp "$EmailAddr" ls -la $FileName ls -la $FileName64 rm "$FileName" rm "$FileName64" exit 0 

用您的用户名替换/home/Me以上。 用您的实际Gmail地址替换MyEmail@gamil.com 。 将目录/mnt/e/bin更改为存储bash脚本的目录。 保存文件并退出。 然后使用:

 chmod a+x /mnt/e/bin/backup 

这使得脚本可执行。

注意MBR(主引导记录)如何保存到备份中。 如脚本注释中所述,需要使用sudo dd ...创建~/.mbr.sav单独的早期步骤。

注意dpkg --get-selections行。 这将创建备份所有已安装应用程序名称的列表。

自动发送电子邮件的最简单方法

从使用ssmtp发送电子邮件警报,我们找到了从终端或脚本自动发送电子邮件的最简单方法。 安装步骤很简单:

 sudo apt install ssmtp sudo nano /etc/ssmtp/ssmtp.conf # Change "MyEmailAddress" and "MyPassword" to your own. 

没有提到一步; Google会向您发送一封电子邮件,确认您要允许“安全性较低”的应用程序通过您的帐户发送邮件:

gmail打开不那么安全的电子邮件应用程序

安装和配置ssmpt后,需要再ssmpt一个软件包才能将.tar备份文件附加到电子邮件中:

 sudo apt install sharutils 

该软件包包含程序uuencode ,需要转换二进制文件进行传输。

每天设置cron来调用备份脚本

创建文件/etc/cron.daily/daily-backup其中包含:

 #!/bin/sh # # NAME: daily-backup # DESC: A .tar backup file is created, emailed and removed. # DATE: Nov 25, 2017. # CALL: WSL or Ubuntu calls from /etc/cron.daily/daily-backup # PARM: No parameters but /etc/ssmtp/ssmtp.conf must be setup # NOTE: Backup file name contains machine name + Distro # Same script for user with multiple dual boot laptops # Single machine should remove $HOSTNAME from name # Single distribution should remove $Distro sleep 30 # Wait 30 seconds after boot # Running under WSL (Windows Subsystem for Ubuntu)? if cat /proc/version | grep Microsoft; then Distro="WSL" else Distro="Ubuntu" fi today=$( date +%Y-%m-%d-%A ) /mnt/e/bin/daily-backup.sh Daily-$(hostname)-$Distro-backup-$today 

保存文件,退出并使用:

 chmod a+x /etc/cron.daily/daily-backup 

这使得脚本可执行。

每天早上cron给你发电子邮件

每天早上在/etc/cron.daily/daily-backup运行后, cron会给你发两封电子邮件。 一个是备份Backup-YYYY-MM-DD.tar文件,在我的情况下是5.2 MB,我无法告诉你。 另一个列出了tar命令向cron报告的备份中的所有文件:

 Anacron  6:58 AM (1 hour ago) to root, bcc: me /etc/cron.daily/daily-backup: bin/ bin/.websync.new bin/log-gsu-del bin/now (... SNIP ...) .xscreensaver .xsession-errors .xsession-errors.old 

摘要

花了一个月的时间等待答案然后一个月写了一个答案,但现在项目已经完成了。 outlook未来,只需在备份脚本中添加其他目录即可。

下一个项目将是一个完整备份,但它大6 GB,将被复制到gdrive(谷歌驱动器),因为gmail限制为25 MB。 该脚本称为/usr/local/bin/full-backup ,如果您感兴趣,则包含在此处:

 #!/bin/bash # NAME: full-backup # PATH: $HOME/bin # DESC: Full system backup - must call with SUDO # DATE: July 16, 2017. Modified July 26, 2017. apt autoclean # reduces size of /var/cache/apt/archives cd /tmp # tar must be created in directory not backed up. time tar -cvpzf backup.tar.gz \ --exclude=/backup.tar.gz \ --exclude=/proc \ --exclude=/tmp \ --exclude=/mnt \ --exclude=/dev \ --exclude=/sys \ --exclude=/media \ --exclude=/usr/src/linux-headers* \ --exclude=/home/Me/.cache \ --exclude=/var/log \ --exclude=/var/run/ \ --exclude=/run \ --exclude=/var/cache/apt/archives / 

历史部分

随着可用选项的探索,这将是一个“旅程”而不是答案。

首先备份最重要的东西

自2016年8月以来,我有两个目录投入了大部分时间:

 /home/rick/bin /usr/local/bin 

当我第一次使用这两个目录创建一个tar文件(磁带存档)并尝试通过电子邮件发送给我自己时收到此错误:

gmail 25MB限制

gmail.com不接受> 25 MB的文件

如何在10个月内编写的两个脚本目录大于25 MB? 仔细检查它们是> 190 MB。 Whhaaatttt?

结果是为测试目的而创建的单个文件:

 -rw-rw-r-- 1 rick rick 191143744 Dec 23 17:27 log-gsu-gedit.tst 

因此,删除此测试文件并重新运行命令:

 tar -cvf scripts-2017-06-05.tar /home/rick/bin tar -rvf scripts-2017-06-05.tar /usr/local/bin 

第一个命令使用一个脚本文件目录创建.tar文件,第二个命令使用脚本文件的第二个目录附加到.tar文件。

.tar文件现在是一个更可观的1.3 MB大小:

 -rw-rw-r-- 1 rick rick 1341440 Jun 5 17:27 scripts-2017-06-05.tar 

最简单的方法是将电子邮件作为附件发送

现在创建了.tar文件,只需进入gmail.com并将文件作为附件通过电子邮件发送给自己。 在下一步中,我们需要一个每天创建文件的cron作业,并使用MTA (邮件传输代理)自动发送电子邮件。 需要在gmail.com中设置一个选项,以删除超过30天的所有这些电子邮件。 这样,只存储400 MB左右的总脚本备份。


编辑2017年6月25日

我发现今晚一些配置文件难以备份,直到我偶然发现这个post 。 有问题的文件在我的主目录中:

 .bashrc .conkyrc .websync # one of my own databases .bafman* # Another one of my own databases 

使用上面的链接,我创建了一个名为~/bin/backup的脚本:

 #!/bin/bash if [[ $1 == "" ]] ; then echo 'Parameter required for file name, ie "Backup-2017-06-26"' echo ".tar will automatically be added as a file extension" exit fi tar -cvf $1.tar /home/rick/bin tar -rvf $1.tar /usr/local/bin find .* -maxdepth 0 -type f -exec tar -rvf $1.tar {} + 

要查看.tar存档中的内容,请使用以下命令:

 tar -tvf Backup-2017-06-26.tar 

请记住将“Backup-2017-06-26”替换为创建备份时使用的参数。


编辑2017年7月1日

类似的问答在2014年11月发布: 通过电子邮件发送备份与crontab 。 接受的答案如下:

当我在机器上测试时,以下命令对我有用。

 echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com 

所以可能接下来的方法是这样的,

 tar -zcf /home/blah/backup.tgz /home/blah/ echo "Please find attached the backup file" | mutt -a "/home/blah/backup.tgz" -s "File attached" -- recipient@domain.com 

我将上面的脚本保存为backup_email.sh并安排cron作为,

 0 1 * * * /path/to/backup_email.sh 

参考

https://stackoverflow.com/a/9524359/1742825