|
Guides All Guides and HOWTO's. |
|
Thread Tools | Display Modes |
|
|||
Using rsync to synchronize or mirror your home directory files to an USB stick
Using rsync to synchronize or mirror your home directory files to an USB stick
Before you modify and start using the script please consider the following:
My USB stick details:
Things that you will have to modify
Script structure
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump Last edited by J65nko; 15th April 2021 at 02:14 AM. Reason: Fixed rsnapshot,org link |
|
|||
The script
The script:
Code:
#!/bin/sh # $Id: USB-rsync.sh,v 1.5 2013/06/30 18:11:05 adriaan Exp $ # Copyright (c) 2013 J65nko - administrator daemonforums.org # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. USB_STICK=/dev/sd0a LOGFILE=/var/log/USB-rsync.log LOGFILE_DRY=/var/log/USB-rsync_dry_run.log # --- directory to exclude from rsync: FIREFOX_CACHE='.mozilla/firefox/chmj5t7f.default/Cache' # ---- source and destination for rysnc # --- this will create a directory 'adriaan' on '/mnt' SOURCE_DIR=/home/adriaan MOUNT_DIR=/mnt # alternative specification with identical result # SOURCE_DIR=/home/adriaan/ # MOUNT_DIR=/mnt/adriaan # here no directory will not be created, giving you the choice to # specify an alternative destination directory e.g. : # MOUNT_DIR=/mnt/j65nko # Need root privileges to mount stick on '/mnt' # and to create logfile in '/var/log' if [ "$(id -u)" -ne 0 ]; then echo $0 : you are not ROOT !! echo $0 : aborting ....... exit 1 fi # --- echo deleting old .serverauth files find ${SOURCE_DIR} -name '.serverauth*' -maxdepth 1 ! -ctime 1 -ls find ${SOURCE_DIR} -name '.serverauth*' -maxdepth 1 ! -ctime 1 -exec rm {} \; # --- echo Mounting USB stick ....... if mount -o noatime,softdep ${USB_STICK} ${MOUNT_DIR} ; then echo USB has been been mounted df -h echo ---------- else echo Could not mount USB stick! exit 2 fi # -------- rsync options # -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X) # -v, --verbose increase verbosity # -z, --compress compress file data during the transfer # -n, --dry-run perform a trial run with no changes made # --delete delete extraneous files from dest dirs # --delete-excluded also delete excluded files from dest dirs echo ----- dry-run rsync -avz \ --delete \ --exclude=${FIREFOX_CACHE} \ --dry-run \ ${SOURCE_DIR} \ ${MOUNT_DIR} 2>&1 | tee ${LOGFILE_DRY} | less printf "Do you want to perform the actual, real rsync now? (Y/N): " read ANSWER case ${ANSWER} in [Yy] ) echo OK doing the rsync ..... ;; * ) echo "$0: You decided not to do the actual rsync from ${SOURCE_DIR} to ${MOUNT_DIR}" exit 3 ;; esac # ---- perform the real rsync rsync -avz \ --delete \ --exclude=${FIREFOX_CACHE} \ ${SOURCE_DIR} \ ${MOUNT_DIR} 2>&1 | tee ${LOGFILE} # ----- message to user cat <<END $0 : has finished ======================================================= $(df -h) The log file of the dry run : ${LOGFILE_DRY} The log file of the actual run : ${LOGFILE} Please do not forget to umount your USB stick ......... ======================================================= END # ---- end of script ---
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Backup using rsnapshot (1)
Placeholder
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Backup using rsnapshot (2)
Placeholder
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Thank you so much , j65nko !!
Quite a useful script. Quote:
|
|
|||
This paraphrase of jggimi's statement out of context is misleading. It takes a careful reading of this tutorial, I think, as well as some interpretation or inference of the intended meaning, to avoid walking away with the impression that rsync used alone (even without one of the --delete arguments) will delete files in the destination directory and that rsnapshot is the (as opposed to a) solution to that.
I believe it doesn't intend to say that at all. Your first phrasing is clear enough in only saying rsync --delete falls into jggimi's case of backing up your deletions: "The rsync '--delete' option makes this script unsuitable for backing up purposes." Then to me it would be clearer to follow up in your next tutorial with a modification of this script with different rsync options that make it a backup script rather than only a mirroring script (no --delete, but with --link-dest?) rather than proceeding immediately to an rsnapshot tutorial. Myself, I don't want to go to rsnapshot but like to code my back up scripts myself. This lets me do something a little weird: I have a small disk laptop that I snapshot to an old Macintosh with a larger harddrive and some older files that never fit on the laptop. After I rsync from laptop to a subdirectory on the powermac I rsync from the powermac the full set of files minus some exclusions to a USB drive. Not sure this will fit into someone else's idea of a backup or snapshotting program. Then writing it myself also helps me to remember better what my "back up" (I'm still foggy on the rigorous definition of a back up -- sysadmins seem to have a stronger definition than I do) does exactly. I like this phrasing of jggimi's too, but have to admit I've had occasion to want to backup some of my deletions, not to mention my moves. First, there are some things you genuinely do not want to exist anywhere. Second, in my half baked "backup" schemes I sometimes run out of space on the back up media. Yes, I suppose disks are cheap but not necessarily cheap enough for my taste. My back up space is sufficient again now, but I expect whenever I get around to using --link-dest in my script this may change eventually. Hmmm, I wonder how --link-dest and --delete interact, if at all. Maybe I will only have to delete selective big, old, no longer extant on the source drive files directly off the back up medium manually. Maybe you could argue that's a safer bet than rsync --delete in the first place. Last edited by thirdm; 1st January 2014 at 07:26 PM. Reason: correction and clarification after re-reading OP |
|
|||
Oh, one other thing. I found it useful in syncing to usb to first check that the right drive was mounted. You know, you can have a lot of different things you might stick in a usb port. I was doing this before on slackware by using fstab to mount to a certain directory based on disk identifier somehow IIRC and checking in script that that directory was mounted to an sdaX or something before proceeding. I think now that I'm back to OpenBSD I can check with the disk uuid from disklabel: e.g. disklabel sd0a | grep '^duid: 2319282e388bd9dc', but haven't added this back in yet.
Last edited by thirdm; 1st January 2014 at 07:02 PM. |
Tags |
rsync |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Slow rsync backup from zfs pool with 99% of files | Septic | FreeBSD General | 10 | 29th May 2013 09:45 PM |
HOWTO: Find Duplicated Files in Directory Tree | vermaden | Guides | 22 | 27th April 2010 07:43 PM |
strange "~" directory in home directory | gosha | OpenBSD General | 5 | 23rd February 2009 06:12 PM |
FTP users sharing same home directory | phreud | FreeBSD General | 6 | 11th November 2008 10:11 PM |
Jails - mount: /usr/home: No such file or directory | chris | FreeBSD General | 6 | 6th August 2008 10:47 PM |