A simple backup script to copy a local directory via sftp protocol to a remote server, using the program lftp.
Have fun, but use this script at your own risk! No warranties at all.
It is possible to have the sourcedir's basename be created on the target side too. Or to just copy the contents of sourcedir to the target server. See the example below (TGTCREATEBASEDIR).
- no versioning, one target directory
- one subdirectory for every date (YYYY-MM-DD)
- one subdirectory for every day of week (1..7)
- one subdirectory for every day of week (Mon..Sun, by locale)
- one subdirectory per week (W01..W53) with sub-subdirectories per day of week (1..7)
- one subdirectory per week (W01..W53) with sub-subdirectories per day of week (Mon..Sun, by locale)
- one subdirectory per month (01..12) with sub-subdirectories per day (01..31)
The above mentioned different behaviors can be configured right within the scripts sourceode. There are some more details explained.
With two files in 'SRCDIR' '.../data' to be saved on 'Sunday Mar 6 2022' the above types of VERSIONDIR would produce the following structures in the target directory 'TGTROOTDIR':
with
TGTCREATEBASEDIR=no TGTCREATEBASEDIR=yes
βββ file1.txt βββ data
βββ file2.txt βββ file1.txt
βββ file2.txt
βββ 2022-03-06 βββ 2022-03-06
βΒ Β βββ file1.txt β βββ data
βΒ Β βββ file2.txt β βββ file1.txt
β Β Β βββ file2.txt
βββ 7 βββ 7
βΒ Β βββ file1.txt β βββ data
βΒ Β βββ file2.txt β βββ file1.txt
βΒ Β βββ file2.txt
βββ Sun βββ Sun
βΒ Β βββ file1.txt β βββ data
βΒ Β βββ file2.txt β βββ file1.txt
βΒ Β βββ file2.txt
βββ W09 βββ W09
β βββ 7 β βββ 7
β βββ file1.txt β βββ data
βββ file2.txt β βββ file1.txt
β βββ file2.txt
βββ W09 βββ W09
β βββ Sun β βββ Sun
β βββ file1.txt β βββ data
βββ file2.txt β βββ file1.txt
β βββ file2.txt
βββ M03 βββ M03
βΒ Β βββ 06 βΒ Β βββ 06
βΒ Β βββ file1.txt β βββ data
βΒ Β βββ file2.txt β βββ file1.txt
βΒ Β βββ file2.txt
-
The possible final message from lftp, "To be removed: ..." denotes some older/other data in the target directory LFTP_DIR. That data could be automatically deleted by lftp with the additional option
--delete
in the scripts variable LFTP_CMD.BUT BE VERY CAREFUL or all other parallel backups/data are/is gone when done wrong! It's like an
rm -r *
in the current LFTP_DIR and then copying the new data there. When changing target pathes the rating what is current and what is "old" might not always be obvious - at least for the commands... -
When connecting to a remote server for the first time, lftp might complain or hang.
Please use an initial manualssh user@server
to complete the "known-hosts" dialog.
That should be fixed now, but I'll keep this note here.