The simplest backup solution ever...
root
… not for everyone maybe, but for me as a linux/java/groovy person it is!
you’ll need
- java
- groovy
- rsync
- ssh
- cron
I want to regularly copy files from a bunch of paths from several remote hosts to the localhost. I know nothing about arrays etc in bash to configure this stuff in a simple way but the solution I’ve come up with is simple and elegant:
I wrote a little groovy script that generates the shell commands to be executed. the output of the groovy script is piped into bash, which executes the commands. That call of the groovy script with the piping to the bash is in a little shell script which is called from cron.
Simple, right? Here’s the code:
backup.groovy
class Task {
String user
String host
List<String> paths
}
tasks = [
new Task(user: "user1", host: "domain1.com", paths: [
"/home/user1",
"/other/path"
]),
new Task(user: "user2", host: "domain2.com", paths: [
"/home/user2",
"/other/path"
])
]
base = "/home/stackmagic/BACKUPS"
tasks.each { t ->
t.paths.each { p ->
lpath = "${base}/${t.host}/${p}".replaceAll("//", "/")
fromto = "${t.user}@${t.host}:${p} ${lpath}"
println "mkdir -p ${lpath}"
println "rsync -avz --delete -e ssh ${fromto}"
println "echo \"\""
}
}
The output of the above script looks like this:
mkdir -p /home/stackmagic/BACKUPS/domain1.com/home/user1
rsync -avz -e ssh user1@domain1.com:/home/user1 /home/stackmagic/BACKUPS/domain1.com/home/user1
mkdir -p /home/stackmagic/BACKUPS/domain1.com/other/path
rsync -avz -e ssh user1@domain1.com:/other/path /home/stackmagic/BACKUPS/domain1.com/other/path
mkdir -p /home/stackmagic/BACKUPS/domain2.com/home/user2
rsync -avz -e ssh user2@domain2.com:/home/user2 /home/stackmagic/BACKUPS/domain2.com/home/user2
mkdir -p /home/stackmagic/BACKUPS/domain2.com/other/path
rsync -avz -e ssh user2@domain2.com:/other/path /home/stackmagic/BACKUPS/domain2.com/other/path
backup.sh
#!/bin/bash
groovy backup.groovy | bash
crontab (executes every morning at 0700)
0 7 * * * /home/stackmagic/BACKUPS/backup.sh
And that’s it already!!
Read this for some security hints regarding rsync over ssh: http://troy.jdmz.net/rsync/index.html