linux:backup
Differences
This shows you the differences between two versions of the page.
| Previous revision | |||
| — | linux:backup [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== A savvy space backup system for linux ====== | ||
| + | |||
| + | ===== The idea ===== | ||
| + | |||
| + | |||
| + | Recently, in Linux Journal I came across with a backup system that really shocked me: the first time, it uses rsync to make a copy of all the data. | ||
| + | |||
| + | The second day, it uses cp to create a copy of all the data by creating a new link to all the files that existed. | ||
| + | |||
| + | After this, it uses rsync again: because rsync not delete the changed files, but unlink them, only the changed files were copied -and therefore consume disk space-. | ||
| + | |||
| + | Conclusion: you have some copies of day-1, day-2, etc keeping only links to the files that have changed. | ||
| + | |||
| + | ===== Let's go for it ===== | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | # backup : a backup script who saves space using links when files doesn' | ||
| + | # | ||
| + | # | ||
| + | |||
| + | # configure source and destination | ||
| + | # properly | ||
| + | echo " | ||
| + | exit 0 | ||
| + | |||
| + | |||
| + | source=/ | ||
| + | dest=/ | ||
| + | |||
| + | |||
| + | # safe settings | ||
| + | umask 077 | ||
| + | |||
| + | PATH="/ | ||
| + | |||
| + | # write a message (to a file, for screen, etc.) | ||
| + | function msg | ||
| + | { | ||
| + | echo $1 | ||
| + | } #msg | ||
| + | |||
| + | function finish | ||
| + | { | ||
| + | # remove possibly open files | ||
| + | echo ' | ||
| + | } | ||
| + | |||
| + | function fail | ||
| + | { | ||
| + | msg $0" | ||
| + | error_result=$? | ||
| + | finish | ||
| + | } | ||
| + | |||
| + | # | ||
| + | # Here starts the program | ||
| + | # ----------------------- | ||
| + | # | ||
| + | |||
| + | msg " | ||
| + | trap " | ||
| + | |||
| + | |||
| + | rm -rf " | ||
| + | mv " | ||
| + | mv " | ||
| + | mv " | ||
| + | mv " | ||
| + | mv " | ||
| + | mv " | ||
| + | |||
| + | # -a: preserves dates and times, permissions, | ||
| + | # -l: create hard links instead of copying | ||
| + | cp -al " | ||
| + | |||
| + | |||
| + | rsync -avz --delete-after \ | ||
| + | " | ||
| + | " | ||
| + | |||
| + | finish | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
