Scripting the daily rsync backup job
|
|
1 éve | |
|---|---|---|
| .gitignore | 1 éve | |
| Readme.md | 1 éve | |
| getRemoteBackup.sh | 1 éve |
This script uses rsync to backup files from any source (e.g. Remote) to any target (usually the host, the script runs on). Once the
sendmail needs to be installed and configured for sending backup notifications.
Pull the repository to a location of your choice on the host.
Configure your ssh client, such that rsync neither requires passwords, ports, etc. Use the config file under your .ssh folder.
Host sebastianvendt.de
HostName sebastianvendt.de
Port 1234
IdentityFile /home/sebastian/sshKeys/rsyncOnSebastianvendt.de.pem
Create a configuration file, which holds the source and destination paths. An example configuration file is given in the following. Please note, this is also a shell script and it also must be executable!
#!/bin/bash
declare -A backup_folders #Do not change this name
backup_folders["rsync@sebastianvendt.de:/var/lib/psa/dumps/domains/sebastianvendt.de/"]="/home/sebastian/ARCHIVE/sebastian/3000_BACKUP/sebastianvendt.de/configuration"
backup_folders["backup@serles.local:/var/lib/openhab/backup"]="/home/sebastian/ARCHIVE/sebastian/3000_BACKUP/serles.local/backup"
Add the script with the path to your configuration file to your crontab-file. I suggest to run it in a screen. You can then always reattach to the screen and check the current status for long backup jobs.
10 2 * * * sebastian screen -d -m /home/sebastian/scripts/backup/getRemoteBackup.sh /home/sebastian/scripts/backup/backupFolders.conf
The heart of the script is an rsync command:
rsync --delete --out-format="%t %f %''b" -avz $sourceFolder ${backup_folders[$sourceFolder]} | tee -a $tmpLogFilePath 2>&1
sending side), but only for the directories that are being synchronized. You must have asked
rsync to send the whole directory (e.g. "dir" or "dir/") without using a wildcard for the
directory’s contents (e.g. "dir/*") since the wildcard is expanded by the shell and rsync thus
gets a request to transfer individual files, not the files’ parent directory. Files that are
excluded from the transfer are also excluded from being deleted unless you use the
**--delete-excluded** option or mark the rules as only matching on the sending side (see the
include/exclude modifiers in the FILTER RULES section).
-a, --archive```: This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the above equivalence is when --files-from is specified, in which case -r is not implied. Note that -a does not preserve hardlinks, because finding multiply-linked files is expensive. You must separately specify -H.
compressing remote shell or a compressing transport because it takes advantage of the implicit
information in the matching data blocks that are not explicitly sent over the connection.
See the **--skip-compress** option for the default list of file suffixes that will not be compressed.
-v, --verbose```: Be verbose :)