Backup script using incremental rsync and hardlinks.
This script creates a backup using rsync of a local or a remote directory.
Environment variables are used to configure the script.
TARGET_DIR= # path where the backups are stored
SOURCE_DIR= # local or remote path to backup eg. /home/foobar OR [email protected]:/home/foobar
SSH_OPTIONS= # ssh options to pass into rsync eg. -o UserKnownHostsFile=/dev/null
RSYNC_OPTIONS= # additional rsync options
This script cleans up old backups older than MAX_AGE
Environment variables are used to configure the script.
TARGET_DIR= # path where the backups are stored
MAX_AGE= # oldest backup to keep in days, anything older will be deleted
docker run --rm \
--env "SOURCE_DIR=/source" \
--env "TARGET_DIR=/target" \
--volume "/apps:/source:ro" \
--volume "/backups/apps:/target" \
weikinhuang/rsync-backup:latest
crontab
:
30 9 * * * /usr/bin/docker run --rm --env "SOURCE_DIR=/source" --env "TARGET_DIR=/target" --volume "/apps:/source:ro" --volume "/backups/apps:/target" weikinhuang/rsync-backup:latest
rsync-backup.apps.timer
:
[Unit]
Description=Timer for rsync backup of "/apps" directory
Requires=docker.service
[Timer]
OnCalendar=*-*-* 09:30:00
[Install]
WantedBy=timers.target
rsync-backup.apps.service
:
[Unit]
Description=rsync backup of "/apps" directory
Requires=docker.service
[Service]
TimeoutStartSec=0
Type=oneshot
Environment=DOCKER_IMAGE=weikinhuang/rsync-backup:latest
Environment=CONTAINER_NAME=%n
Environment="SOURCE_DIR=/apps"
Environment="TARGET_DIR=/backups/apps"
ExecStartPre=-/usr/bin/docker pull "${DOCKER_IMAGE}"
ExecStart=/usr/bin/docker run --rm \
--name "${CONTAINER_NAME}" \
--env "SOURCE_DIR=/source" \
--env "TARGET_DIR=/target" \
--volume "${SOURCE_DIR}:/source:ro" \
--volume "${TARGET_DIR}:/target" \
"${DOCKER_IMAGE}"
backup.yaml
:
kind: CronJob
apiVersion: batch/v2alpha1 # batch/v1beta1 for k8s >= 1.8
metadata:
name: backup-apps
namespace: default
spec:
schedule: "30 9 * * *"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 5
failedJobsHistoryLimit: 5
jobTemplate:
spec:
template:
spec:
containers:
- name: rsync-backup
image: weikinhuang/rsync-backup:latest
imagePullPolicy: Always
env:
- name: SOURCE_DIR
value: "/source"
- name: TARGET_DIR
value: "/target"
volumeMounts:
- name: source
mountPath: /source
readOnly: true
- name: target
mountPath: /target
# restartPolicy: OnFailure # retry until success
volumes:
- name: source
hostPath:
path: /apps
- name: target
hostPath:
path: /backups/apps
Build container with:
docker build -t rsync-backup .
Run container with:
docker run -it --rm \
-v $(pwd)/container/usr/local/bin/disk-backup.sh:/usr/local/bin/disk-backup.sh:ro \
-v $(pwd)/container/usr/local/bin/disk-backup-cleanup.sh:/usr/local/bin/disk-backup-cleanup.sh:ro \
rsync-backup bash
Testing scripts:
env SOURCE_DIR=/etc/profile.d TARGET_DIR=/tmp disk-backup.sh